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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
25
26#include <algorithm>
27#include <cmath>
28
29
30bool ARC_CHORD_PARAMS::Compute( const VECTOR2I& aStart, const VECTOR2I& aMid, const VECTOR2I& aEnd )
31{
32 m_valid = false;
33
34 const double dx = static_cast<double>( aEnd.x ) - aStart.x;
35 const double dy = static_cast<double>( aEnd.y ) - aStart.y;
36 m_chordLen = std::sqrt( dx * dx + dy * dy );
37
38 if( m_chordLen <= 0.0 )
39 return false;
40
41 const double mx = static_cast<double>( aMid.x ) - aStart.x;
42 const double my = static_cast<double>( aMid.y ) - aStart.y;
43 const double cross = mx * dy - my * dx;
44
45 if( cross == 0.0 )
46 return false;
47
48 m_sagitta = std::abs( cross ) / m_chordLen;
49
50 if( m_sagitta <= 0.0 )
51 return false;
52
53 m_halfChord = m_chordLen / 2.0;
55
56 if( m_radius <= 0.0 )
57 return false;
58
59 m_ux = dx / m_chordLen;
60 m_uy = dy / m_chordLen;
61 m_nx = -m_uy;
62 m_ny = m_ux;
63
64 if( cross < 0.0 )
65 {
66 m_nx = -m_nx;
67 m_ny = -m_ny;
68 }
69
71 m_midx = ( static_cast<double>( aStart.x ) + aEnd.x ) * 0.5;
72 m_midy = ( static_cast<double>( aStart.y ) + aEnd.y ) * 0.5;
73
74 m_valid = true;
75 return true;
76}
77
78
80{
81 // Center is at chord midpoint + center_offset along the normal direction
83}
84
85
87{
88 double sin_half = m_halfChord / m_radius;
89 double cos_half = m_centerOffset / m_radius;
90
91 // Direction from center to start: -sin(half_angle) * u - cos(half_angle) * n
92 return EDA_ANGLE( VECTOR2D( -sin_half * m_ux - cos_half * m_nx,
93 -sin_half * m_uy - cos_half * m_ny ) );
94}
95
96
98{
99 double sin_half = m_halfChord / m_radius;
100 double cos_half = m_centerOffset / m_radius;
101
102 // Direction from center to end: sin(half_angle) * u - cos(half_angle) * n
103 return EDA_ANGLE( VECTOR2D( sin_half * m_ux - cos_half * m_nx,
104 sin_half * m_uy - cos_half * m_ny ) );
105}
106
107
109{
110 double ratio = std::clamp( m_halfChord / m_radius, 0.0, 1.0 );
111 double base_angle = 2.0 * std::asin( ratio );
112
113 if( m_sagitta > m_radius )
114 return 2.0 * M_PI - base_angle;
115
116 return base_angle;
117}
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:695
VECTOR2< double > VECTOR2D
Definition vector2d.h:694