KiCad PCB EDA Suite
Loading...
Searching...
No Matches
arc_chord_params.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
21
22#include <algorithm>
23#include <cmath>
24
25
26bool ARC_CHORD_PARAMS::Compute( const VECTOR2I& aStart, const VECTOR2I& aMid, const VECTOR2I& aEnd )
27{
28 m_valid = false;
29
30 const double dx = static_cast<double>( aEnd.x ) - aStart.x;
31 const double dy = static_cast<double>( aEnd.y ) - aStart.y;
32 m_chordLen = std::sqrt( dx * dx + dy * dy );
33
34 if( m_chordLen <= 0.0 )
35 return false;
36
37 const double mx = static_cast<double>( aMid.x ) - aStart.x;
38 const double my = static_cast<double>( aMid.y ) - aStart.y;
39 const double cross = mx * dy - my * dx;
40
41 if( cross == 0.0 )
42 return false;
43
44 m_sagitta = std::abs( cross ) / m_chordLen;
45
46 if( m_sagitta <= 0.0 )
47 return false;
48
49 m_halfChord = m_chordLen / 2.0;
51
52 if( m_radius <= 0.0 )
53 return false;
54
55 m_ux = dx / m_chordLen;
56 m_uy = dy / m_chordLen;
57 m_nx = -m_uy;
58 m_ny = m_ux;
59
60 if( cross < 0.0 )
61 {
62 m_nx = -m_nx;
63 m_ny = -m_ny;
64 }
65
67 m_midx = ( static_cast<double>( aStart.x ) + aEnd.x ) * 0.5;
68 m_midy = ( static_cast<double>( aStart.y ) + aEnd.y ) * 0.5;
69
70 m_valid = true;
71 return true;
72}
73
74
76{
77 // Center is at chord midpoint + center_offset along the normal direction
79}
80
81
83{
84 double sin_half = m_halfChord / m_radius;
85 double cos_half = m_centerOffset / m_radius;
86
87 // Direction from center to start: -sin(half_angle) * u - cos(half_angle) * n
88 return EDA_ANGLE( VECTOR2D( -sin_half * m_ux - cos_half * m_nx,
89 -sin_half * m_uy - cos_half * m_ny ) );
90}
91
92
94{
95 double sin_half = m_halfChord / m_radius;
96 double cos_half = m_centerOffset / m_radius;
97
98 // Direction from center to end: sin(half_angle) * u - cos(half_angle) * n
99 return EDA_ANGLE( VECTOR2D( sin_half * m_ux - cos_half * m_nx,
100 sin_half * m_uy - cos_half * m_ny ) );
101}
102
103
105{
106 double ratio = std::clamp( m_halfChord / m_radius, 0.0, 1.0 );
107 double base_angle = 2.0 * std::asin( ratio );
108
109 if( m_sagitta > m_radius )
110 return 2.0 * M_PI - base_angle;
111
112 return base_angle;
113}
EDA_ANGLE GetEndAngle() const
Get the angle from arc center to the end point.
double m_ny
Unit vector perpendicular to chord, toward arc center.
bool Compute(const VECTOR2I &aStart, const VECTOR2I &aMid, const VECTOR2I &aEnd)
Compute arc geometry from three points defining the arc.
double m_midy
Chord midpoint coordinates.
double m_uy
Unit vector along chord (from start to end)
VECTOR2D GetCenterPoint() const
Get the arc center point.
double GetArcAngle() const
Get the arc angle (total angle swept by the arc) in radians.
double m_centerOffset
Distance from chord midpoint to arc center along n.
EDA_ANGLE GetStartAngle() const
Get the angle from arc center to the start point.
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:400
#define M_PI
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683
VECTOR2< double > VECTOR2D
Definition vector2d.h:682