SEG Class Reference

`#include <seg.h>`

## Public Types

using ecoord = VECTOR2I::extended_type

## Public Member Functions

SEG ()
Create an empty (0, 0) segment. More...

SEG (int aX1, int aY1, int aX2, int aY2)
Create a segment between (aX1, aY1) and (aX2, aY2). More...

SEG (const VECTOR2I &aA, const VECTOR2I &aB)
Create a segment between (aA) and (aB). More...

SEG (const VECTOR2I &aA, const VECTOR2I &aB, int aIndex)
Create a segment between (aA) and (aB), referenced to a multi-segment shape. More...

SEG (const SEG &aSeg)
Copy constructor. More...

SEGoperator= (const SEG &aSeg)

bool operator== (const SEG &aSeg) const

bool operator!= (const SEG &aSeg) const

VECTOR2I LineProject (const VECTOR2I &aP) const
Compute the perpendicular projection point of aP on a line passing through ends of the segment. More...

int Side (const VECTOR2I &aP) const
Determine on which side of directed line passing via segment ends point aP lies. More...

int LineDistance (const VECTOR2I &aP, bool aDetermineSide=false) const
Return the closest Euclidean distance between point aP and the line defined by the ends of segment (this). More...

double AngleDegrees (const SEG &aOther) const
Determine the smallest angle between two segments (result in degrees) More...

const VECTOR2I NearestPoint (const VECTOR2I &aP) const
Compute a point on the segment (this) that is closest to point aP. More...

const VECTOR2I NearestPoint (const SEG &aSeg) const
Compute a point on the segment (this) that is closest to any point on aSeg. More...

const VECTOR2I ReflectPoint (const VECTOR2I &aP) const
Reflect a point using this segment as axis. More...

OPT_VECTOR2I Intersect (const SEG &aSeg, bool aIgnoreEndpoints=false, bool aLines=false) const
Compute intersection point of segment (this) with segment aSeg. More...

bool Intersects (const SEG &aSeg) const

OPT_VECTOR2I IntersectLines (const SEG &aSeg) const
Compute the intersection point of lines passing through ends of (this) and aSeg. More...

SEG PerpendicularSeg (const VECTOR2I &aP) const
Compute a segment perpendicular to this one, passing through point aP. More...

SEG ParallelSeg (const VECTOR2I &aP) const
Compute a segment parallel to this one, passing through point aP. More...

bool Collide (const SEG &aSeg, int aClearance, int *aActual=nullptr) const

ecoord SquaredDistance (const SEG &aSeg) const

int Distance (const SEG &aSeg) const
Compute minimum Euclidean distance to segment aSeg. More...

ecoord SquaredDistance (const VECTOR2I &aP) const

int Distance (const VECTOR2I &aP) const
Compute minimum Euclidean distance to point aP. More...

void CanonicalCoefs (ecoord &qA, ecoord &qB, ecoord &qC) const

bool Collinear (const SEG &aSeg) const
Check if segment aSeg lies on the same line as (this). More...

bool ApproxCollinear (const SEG &aSeg) const

bool ApproxParallel (const SEG &aSeg, int aDistanceThreshold=1) const

bool ApproxPerpendicular (const SEG &aSeg) const

bool Overlaps (const SEG &aSeg) const

bool Contains (const SEG &aSeg) const

int Length () const
Return the length (this). More...

ecoord SquaredLength () const

ecoord TCoef (const VECTOR2I &aP) const

int Index () const
Return the index of this segment in its parent shape (applicable only to non-local segments). More...

bool Contains (const VECTOR2I &aP) const

void Reverse ()

SEG Reversed () const
Returns the center point of the line. More...

VECTOR2I Center () const

## Static Public Member Functions

static SEG::ecoord Square (int a)

VECTOR2I A

VECTOR2I B

## Private Member Functions

bool ccw (const VECTOR2I &aA, const VECTOR2I &aB, const VECTOR2I &aC) const

bool intersects (const SEG &aSeg, bool aIgnoreEndpoints=false, bool aLines=false, VECTOR2I *aPt=nullptr) const

## Private Attributes

int m_index
< index within the parent shape (used when m_is_local == false) More...

## Friends

std::ostream & operator<< (std::ostream &aStream, const SEG &aSeg)

## Detailed Description

Definition at line 40 of file seg.h.

## ◆ ecoord

 using SEG::ecoord = VECTOR2I::extended_type

Definition at line 43 of file seg.h.

## ◆ SEG() [1/5]

 SEG::SEG ( )
inline

Create an empty (0, 0) segment.

Definition at line 54 of file seg.h.

55  {
56  m_index = -1;
57  }
int m_index
< index within the parent shape (used when m_is_local == false)
Definition: seg.h:399

References m_index.

Referenced by ParallelSeg(), PerpendicularSeg(), and Reversed().

## ◆ SEG() [2/5]

 SEG::SEG ( int aX1, int aY1, int aX2, int aY2 )
inline

Create a segment between (aX1, aY1) and (aX2, aY2).

Definition at line 62 of file seg.h.

62  :
63  A( VECTOR2I( aX1, aY1 ) ),
64  B( VECTOR2I( aX2, aY2 ) )
65  {
66  m_index = -1;
67  }
VECTOR2< int > VECTOR2I
Definition: vector2d.h:622
VECTOR2I A
Definition: seg.h:48
int m_index
< index within the parent shape (used when m_is_local == false)
Definition: seg.h:399
VECTOR2I B
Definition: seg.h:49

References m_index.

## ◆ SEG() [3/5]

 SEG::SEG ( const VECTOR2I & aA, const VECTOR2I & aB )
inline

Create a segment between (aA) and (aB).

Definition at line 72 of file seg.h.

72  :
73  A( aA ),
74  B( aB )
75  {
76  m_index = -1;
77  }
VECTOR2I A
Definition: seg.h:48
int m_index
< index within the parent shape (used when m_is_local == false)
Definition: seg.h:399
VECTOR2I B
Definition: seg.h:49

References m_index.

## ◆ SEG() [4/5]

 SEG::SEG ( const VECTOR2I & aA, const VECTOR2I & aB, int aIndex )
inline

Create a segment between (aA) and (aB), referenced to a multi-segment shape.

Parameters
 aA reference to the start point in the parent shape aB reference to the end point in the parent shape aIndex index of the segment within the parent shape

Definition at line 86 of file seg.h.

86  :
87  A( aA ),
88  B( aB )
89  {
90  m_index = aIndex;
91  }
VECTOR2I A
Definition: seg.h:48
int m_index
< index within the parent shape (used when m_is_local == false)
Definition: seg.h:399
VECTOR2I B
Definition: seg.h:49

References m_index.

## ◆ SEG() [5/5]

 SEG::SEG ( const SEG & aSeg )
inline

Copy constructor.

Definition at line 96 of file seg.h.

96  :
97  A( aSeg.A ),
98  B( aSeg.B ),
99  m_index( aSeg.m_index )
100  {
101  }
VECTOR2I A
Definition: seg.h:48
int m_index
< index within the parent shape (used when m_is_local == false)
Definition: seg.h:399
VECTOR2I B
Definition: seg.h:49

## ◆ AngleDegrees()

 double SEG::AngleDegrees ( const SEG & aOther ) const

Determine the smallest angle between two segments (result in degrees)

Parameters
 aOther point to determine the orientation wrs to self
Returns
smallest angle between this and aOther (degrees)

Definition at line 94 of file seg.cpp.

95 {
96  VECTOR2I thisVec = A - B;
97  VECTOR2I otherVec = aOther.A - aOther.B;
98
99  double thisVecAngle = NormalizeAngle180( RAD2DECIDEG( thisVec.Angle() ) );
100  double otherVecAngle = NormalizeAngle180( RAD2DECIDEG( otherVec.Angle() ) );
101  double angleDegrees = std::abs( NormalizeAngle180( thisVecAngle - otherVecAngle ) ) / 10.0;
102
103  return std::min( 180.0 - angleDegrees, angleDegrees );
104 }
Define a general 2D-vector/point.
Definition: vector2d.h:61
Definition: trigo.h:234
T NormalizeAngle180(T Angle)
Normalize angle to be in the -180.0 .. 180.0 range.
Definition: trigo.h:387
double Angle() const
Compute the angle of the vector.
Definition: vector2d.h:307
VECTOR2I A
Definition: seg.h:48
VECTOR2I B
Definition: seg.h:49

References A, VECTOR2< T >::Angle(), B, NormalizeAngle180(), and RAD2DECIDEG().

## ◆ ApproxCollinear()

 bool SEG::ApproxCollinear ( const SEG & aSeg ) const
inline

Definition at line 279 of file seg.h.

280  {
281  ecoord p, q, r;
282  CanonicalCoefs( p, q, r );
283
284  ecoord dist1 = ( p * aSeg.A.x + q * aSeg.A.y + r ) / sqrt( p * p + q * q );
285  ecoord dist2 = ( p * aSeg.B.x + q * aSeg.B.y + r ) / sqrt( p * p + q * q );
286
287  return std::abs( dist1 ) <= 1 && std::abs( dist2 ) <= 1;
288  }
void CanonicalCoefs(ecoord &qA, ecoord &qB, ecoord &qC) const
Definition: seg.h:255
VECTOR2I::extended_type ecoord
E_SERIE r
Definition: eserie.cpp:41
VECTOR2I A
Definition: seg.h:48
VECTOR2I B
Definition: seg.h:49

References A, B, CanonicalCoefs(), r, VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by PCB_TRACK::ApproxCollinear().

## ◆ ApproxParallel()

 bool SEG::ApproxParallel ( const SEG & aSeg, int aDistanceThreshold = `1` ) const
inline

Definition at line 290 of file seg.h.

291  {
292  ecoord p, q, r;
293  CanonicalCoefs( p, q, r );
294
295  ecoord dist1 = ( p * aSeg.A.x + q * aSeg.A.y + r ) / sqrt( p * p + q * q );
296  ecoord dist2 = ( p * aSeg.B.x + q * aSeg.B.y + r ) / sqrt( p * p + q * q );
297
298  return std::abs( dist1 - dist2 ) <= aDistanceThreshold;
299  }
void CanonicalCoefs(ecoord &qA, ecoord &qB, ecoord &qC) const
Definition: seg.h:255
VECTOR2I::extended_type ecoord
E_SERIE r
Definition: eserie.cpp:41
VECTOR2I A
Definition: seg.h:48
VECTOR2I B
Definition: seg.h:49

References A, B, CanonicalCoefs(), r, VECTOR2< T >::x, and VECTOR2< T >::y.

## ◆ ApproxPerpendicular()

 bool SEG::ApproxPerpendicular ( const SEG & aSeg ) const
inline

Definition at line 301 of file seg.h.

302  {
303  SEG perp = PerpendicularSeg( A );
304
305  return aSeg.ApproxParallel( perp );
306  }
SEG PerpendicularSeg(const VECTOR2I &aP) const
Compute a segment perpendicular to this one, passing through point aP.
Definition: seg.cpp:198
bool ApproxParallel(const SEG &aSeg, int aDistanceThreshold=1) const
Definition: seg.h:290
Definition: seg.h:40

References ApproxParallel(), and PerpendicularSeg().

Referenced by SegPerpendicularCorrect().

## ◆ CanonicalCoefs()

 void SEG::CanonicalCoefs ( ecoord & qA, ecoord & qB, ecoord & qC ) const
inline

Definition at line 255 of file seg.h.

256  {
257  qA = ecoord{ A.y } - B.y;
258  qB = ecoord{ B.x } - A.x;
259  qC = -qA * A.x - qB * A.y;
260  }
VECTOR2I::extended_type ecoord

Referenced by ApproxCollinear(), ApproxParallel(), and Collinear().

## ◆ ccw()

 bool SEG::ccw ( const VECTOR2I & aA, const VECTOR2I & aB, const VECTOR2I & aC ) const
private

Definition at line 216 of file seg.cpp.

217 {
218  return (ecoord) ( aC.y - aA.y ) * ( aB.x - aA.x ) > (ecoord) ( aB.y - aA.y ) * ( aC.x - aA.x );
219 }
VECTOR2I::extended_type ecoord
Definition: seg.h:43
VECTOR2I::extended_type ecoord

References VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by Collide().

## ◆ Center()

 VECTOR2I SEG::Center ( ) const
inline

Definition at line 386 of file seg.h.

387  {
388  return A + ( B - A ) / 2;
389  }
VECTOR2I A
Definition: seg.h:48

References A.

Referenced by Collide(), PCB_GRID_HELPER::computeAnchors(), and SHAPE_ARC::SHAPE_ARC().

## ◆ Collide()

 bool SEG::Collide ( const SEG & aSeg, int aClearance, int * aActual = `nullptr` ) const

Definition at line 222 of file seg.cpp.

223 {
224  // check for intersection
225  // fixme: move to a method
226  if( ccw( A, aSeg.A, aSeg.B ) != ccw( B, aSeg.A, aSeg.B ) &&
227  ccw( A, B, aSeg.A ) != ccw( A, B, aSeg.B ) )
228  {
229  if( aActual )
230  *aActual = 0;
231
232  return true;
233  }
234
235  ecoord dist_sq = VECTOR2I::ECOORD_MAX;
236
237  dist_sq = std::min( dist_sq, SquaredDistance( aSeg.A ) );
238  dist_sq = std::min( dist_sq, SquaredDistance( aSeg.B ) );
239  dist_sq = std::min( dist_sq, aSeg.SquaredDistance( A ) );
240  dist_sq = std::min( dist_sq, aSeg.SquaredDistance( B ) );
241
242  if( dist_sq == 0 || dist_sq < (ecoord) aClearance * aClearance )
243  {
244  if( aActual )
245  *aActual = isqrt( dist_sq );
246
247  return true;
248  }
249
250  return false;
251 }
T isqrt(T x)
Definition: seg.cpp:58
bool ccw(const VECTOR2I &aA, const VECTOR2I &aB, const VECTOR2I &aC) const
Definition: seg.cpp:216
ecoord SquaredDistance(const SEG &aSeg) const
Definition: seg.cpp:72
static constexpr extended_type ECOORD_MAX
Definition: vector2d.h:79
VECTOR2I::extended_type ecoord
VECTOR2I A
Definition: seg.h:48
VECTOR2I B
Definition: seg.h:49

References A, B, ccw(), VECTOR2< T >::ECOORD_MAX, isqrt(), and SquaredDistance().

## ◆ Collinear()

 bool SEG::Collinear ( const SEG & aSeg ) const
inline

Check if segment aSeg lies on the same line as (this).

Parameters
 aSeg the segment to check colinearity with
Returns
true, when segments are collinear.

Definition at line 268 of file seg.h.

269  {
270  ecoord qa, qb, qc;
271  CanonicalCoefs( qa, qb, qc );
272
273  ecoord d1 = std::abs( aSeg.A.x * qa + aSeg.A.y * qb + qc );
274  ecoord d2 = std::abs( aSeg.B.x * qa + aSeg.B.y * qb + qc );
275
276  return ( d1 <= 1 && d2 <= 1 );
277  }
void CanonicalCoefs(ecoord &qA, ecoord &qB, ecoord &qC) const
Definition: seg.h:255
VECTOR2I::extended_type ecoord
VECTOR2I A
Definition: seg.h:48
VECTOR2I B
Definition: seg.h:49

References A, B, CanonicalCoefs(), VECTOR2< T >::x, and VECTOR2< T >::y.

## ◆ Contains() [1/2]

 bool SEG::Contains ( const SEG & aSeg ) const
inline

Definition at line 331 of file seg.h.

332  {
333  if( aSeg.A == aSeg.B ) // single point corner case
334  return Contains( aSeg.A );
335
336  if( !Collinear( aSeg ) )
337  return false;
338
339  if( Contains( aSeg.A ) && Contains( aSeg.B ) )
340  return true;
341
342  return false;
343  }
bool Collinear(const SEG &aSeg) const
Check if segment aSeg lies on the same line as (this).
Definition: seg.h:268
VECTOR2I A
Definition: seg.h:48
bool Contains(const SEG &aSeg) const
Definition: seg.h:331
VECTOR2I B
Definition: seg.h:49

References A, B, and Collinear().

## ◆ Contains() [2/2]

 bool SEG::Contains ( const VECTOR2I & aP ) const

Definition at line 254 of file seg.cpp.

255 {
256  return Distance( aP ) <= 1;
257 }
int Distance(const SEG &aSeg) const
Compute minimum Euclidean distance to segment aSeg.
Definition: seg.cpp:318

References Distance().

## ◆ Distance() [1/2]

 int SEG::Distance ( const SEG & aSeg ) const

Compute minimum Euclidean distance to segment aSeg.

Parameters
 aSeg other segment
Returns
minimum distance

Definition at line 318 of file seg.cpp.

319 {
320  return isqrt( SquaredDistance( aSeg ) );
321 }
T isqrt(T x)
Definition: seg.cpp:58
ecoord SquaredDistance(const SEG &aSeg) const
Definition: seg.cpp:72

References isqrt(), and SquaredDistance().

## ◆ Distance() [2/2]

 int SEG::Distance ( const VECTOR2I & aP ) const

Compute minimum Euclidean distance to point aP.

Parameters
 aP the point
Returns
minimum distance

Definition at line 324 of file seg.cpp.

325 {
326  return isqrt( SquaredDistance( aP ) );
327 }
T isqrt(T x)
Definition: seg.cpp:58
ecoord SquaredDistance(const SEG &aSeg) const
Definition: seg.cpp:72

References isqrt(), and SquaredDistance().

## ◆ Index()

 int SEG::Index ( ) const
inline

Return the index of this segment in its parent shape (applicable only to non-local segments).

Returns
index value

Definition at line 368 of file seg.h.

369  {
370  return m_index;
371  }
int m_index
< index within the parent shape (used when m_is_local == false)
Definition: seg.h:399

References m_index.

## ◆ Intersect()

 OPT_VECTOR2I SEG::Intersect ( const SEG & aSeg, bool aIgnoreEndpoints = `false`, bool aLines = `false` ) const

Compute intersection point of segment (this) with segment aSeg.

Parameters
 aSeg segment to intersect with aIgnoreEndpoints don't treat corner cases (i.e. end of one segment touching the other) as intersections. aLines treat segments as infinite lines
Returns
intersection point, if exists

Definition at line 187 of file seg.cpp.

188 {
189  VECTOR2I ip;
190
191  if( intersects( aSeg, aIgnoreEndpoints, aLines, &ip ) )
192  return ip;
193  else
194  return OPT_VECTOR2I();
195 }
Define a general 2D-vector/point.
Definition: vector2d.h:61
OPT< VECTOR2I > OPT_VECTOR2I
Definition: seg.h:38
bool intersects(const SEG &aSeg, bool aIgnoreEndpoints=false, bool aLines=false, VECTOR2I *aPt=nullptr) const
Definition: seg.cpp:149

References intersects().

## ◆ IntersectLines()

 OPT_VECTOR2I SEG::IntersectLines ( const SEG & aSeg ) const
inline

Compute the intersection point of lines passing through ends of (this) and aSeg.

Parameters
 aSeg segment defining the line to intersect with
Returns
intersection point, if exists

Definition at line 209 of file seg.h.

210  {
211  return Intersect( aSeg, false, true );
212  }
OPT_VECTOR2I Intersect(const SEG &aSeg, bool aIgnoreEndpoints=false, bool aLines=false) const
Compute intersection point of segment (this) with segment aSeg.
Definition: seg.cpp:187

References Intersect().

## ◆ Intersects()

 bool SEG::Intersects ( const SEG & aSeg ) const

Definition at line 181 of file seg.cpp.

182 {
183  return intersects( aSeg );
184 }
bool intersects(const SEG &aSeg, bool aIgnoreEndpoints=false, bool aLines=false, VECTOR2I *aPt=nullptr) const
Definition: seg.cpp:149

References intersects().

Referenced by SquaredDistance().

## ◆ intersects()

 bool SEG::intersects ( const SEG & aSeg, bool aIgnoreEndpoints = `false`, bool aLines = `false`, VECTOR2I * aPt = `nullptr` ) const
private

Definition at line 149 of file seg.cpp.

150 {
151  const VECTOR2I e( B - A );
152  const VECTOR2I f( aSeg.B - aSeg.A );
153  const VECTOR2I ac( aSeg.A - A );
154
155  ecoord d = f.Cross( e );
156  ecoord p = f.Cross( ac );
157  ecoord q = e.Cross( ac );
158
159  if( d == 0 )
160  return false;
161
162  if( !aLines && d > 0 && ( q < 0 || q > d || p < 0 || p > d ) )
163  return false;
164
165  if( !aLines && d < 0 && ( q < d || p < d || p > 0 || q > 0 ) )
166  return false;
167
168  if( !aLines && aIgnoreEndpoints && ( q == 0 || q == d ) && ( p == 0 || p == d ) )
169  return false;
170
171  if( aPt )
172  {
173  *aPt = VECTOR2I( aSeg.A.x + rescale( q, (ecoord) f.x, d ),
174  aSeg.A.y + rescale( q, (ecoord) f.y, d ) );
175  }
176
177  return true;
178 }
Define a general 2D-vector/point.
Definition: vector2d.h:61
VECTOR2< int > VECTOR2I
Definition: vector2d.h:622
VECTOR2I::extended_type ecoord
VECTOR2I A
Definition: seg.h:48
T rescale(T aNumerator, T aValue, T aDenominator)
Scale a number (value) by rational (numerator/denominator).
Definition: util.h:98
VECTOR2I B
Definition: seg.h:49

References A, B, VECTOR2< T >::Cross(), rescale(), VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by Intersect(), and Intersects().

## ◆ Length()

 int SEG::Length ( ) const
inline

Return the length (this).

Returns
length

Definition at line 350 of file seg.h.

351  {
352  return ( A - B ).EuclideanNorm();
353  }

## ◆ LineDistance()

 int SEG::LineDistance ( const VECTOR2I & aP, bool aDetermineSide = `false` ) const

Return the closest Euclidean distance between point aP and the line defined by the ends of segment (this).

Parameters
 aP the point to test aDetermineSide when true, the sign of the returned value indicates the side of the line at which we are (negative = left)
Returns
the distance

Definition at line 330 of file seg.cpp.

331 {
332  ecoord p = ecoord{ A.y } - B.y;
333  ecoord q = ecoord{ B.x } - A.x;
334  ecoord r = -p * A.x - q * A.y;
335  ecoord l = p * p + q * q;
336  ecoord det = p * aP.x + q * aP.y + r;
337  ecoord dist_sq = 0;
338
339  if( l > 0 )
340  {
341  dist_sq = rescale( det, det, l );
342  }
343
344  ecoord dist = isqrt( dist_sq );
345
346  return aDetermineSide ? dist : std::abs( dist );
347 }
T isqrt(T x)
Definition: seg.cpp:58
VECTOR2I::extended_type ecoord
E_SERIE r
Definition: eserie.cpp:41
T rescale(T aNumerator, T aValue, T aDenominator)
Scale a number (value) by rational (numerator/denominator).
Definition: util.h:98

References isqrt(), r, rescale(), VECTOR2< T >::x, and VECTOR2< T >::y.

## ◆ LineProject()

 VECTOR2I SEG::LineProject ( const VECTOR2I & aP ) const

Compute the perpendicular projection point of aP on a line passing through ends of the segment.

Parameters
 aP point to project
Returns
projected point

Definition at line 301 of file seg.cpp.

302 {
303  VECTOR2I d = B - A;
304  ecoord l_squared = d.Dot( d );
305
306  if( l_squared == 0 )
307  return A;
308
309  ecoord t = d.Dot( aP - A );
310
311  int xp = rescale( t, ecoord{ d.x }, l_squared );
312  int yp = rescale( t, ecoord{ d.y }, l_squared );
313
314  return A + VECTOR2I( xp, yp );
315 }
Define a general 2D-vector/point.
Definition: vector2d.h:61
VECTOR2< int > VECTOR2I
Definition: vector2d.h:622
VECTOR2I::extended_type ecoord
extended_type Dot(const VECTOR2< T > &aVector) const
Compute dot product of self with aVector.
Definition: vector2d.h:521
VECTOR2I A
Definition: seg.h:48
T rescale(T aNumerator, T aValue, T aDenominator)
Scale a number (value) by rational (numerator/denominator).
Definition: util.h:98

References A, VECTOR2< T >::Dot(), rescale(), VECTOR2< T >::x, and VECTOR2< T >::y.

## ◆ NearestPoint() [1/2]

 const VECTOR2I SEG::NearestPoint ( const VECTOR2I & aP ) const

Compute a point on the segment (this) that is closest to point aP.

Returns
the nearest point

Definition at line 260 of file seg.cpp.

261 {
262  VECTOR2I d = B - A;
263  ecoord l_squared = d.Dot( d );
264
265  if( l_squared == 0 )
266  return A;
267
268  ecoord t = d.Dot( aP - A );
269
270  if( t < 0 )
271  return A;
272  else if( t > l_squared )
273  return B;
274
275  int xp = rescale( t, (ecoord) d.x, l_squared );
276  int yp = rescale( t, (ecoord) d.y, l_squared );
277
278  return A + VECTOR2I( xp, yp );
279 }
Define a general 2D-vector/point.
Definition: vector2d.h:61
VECTOR2< int > VECTOR2I
Definition: vector2d.h:622
VECTOR2I::extended_type ecoord
extended_type Dot(const VECTOR2< T > &aVector) const
Compute dot product of self with aVector.
Definition: vector2d.h:521
VECTOR2I A
Definition: seg.h:48
T rescale(T aNumerator, T aValue, T aDenominator)
Scale a number (value) by rational (numerator/denominator).
Definition: util.h:98
VECTOR2I B
Definition: seg.h:49

References A, B, VECTOR2< T >::Dot(), rescale(), VECTOR2< T >::x, and VECTOR2< T >::y.

## ◆ NearestPoint() [2/2]

 const VECTOR2I SEG::NearestPoint ( const SEG & aSeg ) const

Compute a point on the segment (this) that is closest to any point on aSeg.

Returns
the nearest point

Definition at line 107 of file seg.cpp.

108 {
109  if( OPT_VECTOR2I p = Intersect( aSeg ) )
110  return *p;
111
112  const VECTOR2I pts_origin[4] =
113  {
114  aSeg.NearestPoint( A ),
115  aSeg.NearestPoint( B ),
116  NearestPoint( aSeg.A ),
117  NearestPoint( aSeg.B )
118  };
119
120
121  const VECTOR2I* pts_out[4] =
122  {
123  &A,
124  &B,
125  &pts_origin[2],
126  &pts_origin[3]
127  };
128
129  const ecoord pts_dist[4] =
130  {
131  ( pts_origin[0] - A ).SquaredEuclideanNorm(),
132  ( pts_origin[1] - B ).SquaredEuclideanNorm(),
133  ( pts_origin[2] - aSeg.A ).SquaredEuclideanNorm(),
134  ( pts_origin[3] - aSeg.B ).SquaredEuclideanNorm()
135  };
136
137  int min_i = 0;
138
139  for( int i = 0; i < 4; i++ )
140  {
141  if( pts_dist[i] < pts_dist[min_i] )
142  min_i = i;
143  }
144
145  return *pts_out[min_i];
146 }
OPT_VECTOR2I Intersect(const SEG &aSeg, bool aIgnoreEndpoints=false, bool aLines=false) const
Compute intersection point of segment (this) with segment aSeg.
Definition: seg.cpp:187
Define a general 2D-vector/point.
Definition: vector2d.h:61
OPT< VECTOR2I > OPT_VECTOR2I
Definition: seg.h:38
VECTOR2I::extended_type ecoord
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Compute a point on the segment (this) that is closest to point aP.
Definition: seg.cpp:260
VECTOR2I A
Definition: seg.h:48
VECTOR2I B
Definition: seg.h:49

References A, B, Intersect(), and NearestPoint().

## ◆ operator!=()

 bool SEG::operator!= ( const SEG & aSeg ) const
inline

Definition at line 117 of file seg.h.

118  {
119  return (A != aSeg.A || B != aSeg.B);
120  }
VECTOR2I A
Definition: seg.h:48
VECTOR2I B
Definition: seg.h:49

References A, and B.

## ◆ operator=()

 SEG& SEG::operator= ( const SEG & aSeg )
inline

Definition at line 103 of file seg.h.

104  {
105  A = aSeg.A;
106  B = aSeg.B;
107  m_index = aSeg.m_index;
108
109  return *this;
110  }
VECTOR2I A
Definition: seg.h:48
int m_index
< index within the parent shape (used when m_is_local == false)
Definition: seg.h:399
VECTOR2I B
Definition: seg.h:49

References A, B, and m_index.

## ◆ operator==()

 bool SEG::operator== ( const SEG & aSeg ) const
inline

Definition at line 112 of file seg.h.

113  {
114  return (A == aSeg.A && B == aSeg.B) ;
115  }
VECTOR2I A
Definition: seg.h:48
VECTOR2I B
Definition: seg.h:49

References A, and B.

## ◆ Overlaps()

 bool SEG::Overlaps ( const SEG & aSeg ) const
inline

Definition at line 308 of file seg.h.

309  {
310  if( aSeg.A == aSeg.B ) // single point corner case
311  {
312  if( A == aSeg.A || B == aSeg.A )
313  return false;
314
315  return Contains( aSeg.A );
316  }
317
318  if( !Collinear( aSeg ) )
319  return false;
320
321  if( Contains( aSeg.A ) || Contains( aSeg.B ) )
322  return true;
323
324  if( aSeg.Contains( A ) || aSeg.Contains( B ) )
325  return true;
326
327  return false;
328  }
bool Collinear(const SEG &aSeg) const
Check if segment aSeg lies on the same line as (this).
Definition: seg.h:268
VECTOR2I A
Definition: seg.h:48
bool Contains(const SEG &aSeg) const
Definition: seg.h:331
VECTOR2I B
Definition: seg.h:49

References A, B, Collinear(), and Contains().

## ◆ ParallelSeg()

 SEG SEG::ParallelSeg ( const VECTOR2I & aP ) const

Compute a segment parallel to this one, passing through point aP.

Parameters
 aP Point through which the new segment will pass
Returns
SEG parallel to this passing through point aP

Definition at line 207 of file seg.cpp.

208 {
209  VECTOR2I slope( B - A );
210  VECTOR2I endPoint = slope + aP;
211
212  return SEG( aP, endPoint );
213 }
SEG()
Create an empty (0, 0) segment.
Definition: seg.h:54
Define a general 2D-vector/point.
Definition: vector2d.h:61

References SEG().

Referenced by BOOST_AUTO_TEST_CASE(), and CIRCLE::ConstructFromTanTanPt().

## ◆ PerpendicularSeg()

 SEG SEG::PerpendicularSeg ( const VECTOR2I & aP ) const

Compute a segment perpendicular to this one, passing through point aP.

Parameters
 aP Point through which the new segment will pass
Returns
SEG perpendicular to this passing through point aP

Definition at line 198 of file seg.cpp.

199 {
200  VECTOR2I slope( B - A );
201  VECTOR2I endPoint = slope.Perpendicular() + aP;
202
203  return SEG( aP, endPoint );
204 }
VECTOR2< T > Perpendicular() const
Compute the perpendicular vector.
Definition: vector2d.h:314
SEG()
Create an empty (0, 0) segment.
Definition: seg.h:54
Define a general 2D-vector/point.
Definition: vector2d.h:61

References VECTOR2< T >::Perpendicular(), and SEG().

Referenced by ApproxPerpendicular(), BOOST_AUTO_TEST_CASE(), and CIRCLE::ConstructFromTanTanPt().

## ◆ ReflectPoint()

 const VECTOR2I SEG::ReflectPoint ( const VECTOR2I & aP ) const

Reflect a point using this segment as axis.

Returns
the reflected point

Definition at line 282 of file seg.cpp.

283 {
284  VECTOR2I d = B - A;
285  VECTOR2I::extended_type l_squared = d.Dot( d );
286  VECTOR2I::extended_type t = d.Dot( aP - A );
287  VECTOR2I c;
288
289  if( !l_squared )
290  c = aP;
291  else
292  {
293  c.x = A.x + rescale( t, static_cast<VECTOR2I::extended_type>( d.x ), l_squared );
294  c.y = A.y + rescale( t, static_cast<VECTOR2I::extended_type>( d.y ), l_squared );
295  }
296
297  return 2 * c - aP;
298 }
VECTOR2_TRAITS< int >::extended_type extended_type
Definition: vector2d.h:76
Define a general 2D-vector/point.
Definition: vector2d.h:61
extended_type Dot(const VECTOR2< T > &aVector) const
Compute dot product of self with aVector.
Definition: vector2d.h:521
VECTOR2I A
Definition: seg.h:48
T rescale(T aNumerator, T aValue, T aDenominator)
Scale a number (value) by rational (numerator/denominator).
Definition: util.h:98

References A, VECTOR2< T >::Dot(), rescale(), VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by CornerListToPolygon(), SHAPE_ARC::Mirror(), and SHAPE_LINE_CHAIN::Mirror().

## ◆ Reverse()

 void SEG::Reverse ( )
inline

Definition at line 375 of file seg.h.

376  {
377  std::swap( A, B );
378  }

## ◆ Reversed()

 SEG SEG::Reversed ( ) const
inline

Returns the center point of the line.

Definition at line 380 of file seg.h.

References SEG().

Referenced by PNS::LINE_PLACER::Start().

## ◆ Side()

 int SEG::Side ( const VECTOR2I & aP ) const
inline

Determine on which side of directed line passing via segment ends point aP lies.

Parameters
 aP point to determine the orientation wrs to self
Returns
: < 0: left, 0 : on the line, > 0 : right

Definition at line 142 of file seg.h.

143  {
144  const ecoord det = ( B - A ).Cross( aP - A );
145
146  return det < 0 ? -1 : ( det > 0 ? 1 : 0 );
147  }
VECTOR2I::extended_type ecoord
VECTOR2I A
Definition: seg.h:48

References A.

## ◆ Square()

 static SEG::ecoord SEG::Square ( int a )
inlinestatic

Definition at line 122 of file seg.h.

123  {
124  return ecoord( a ) * a;
125  }
VECTOR2I::extended_type ecoord
Definition: seg.h:43

## ◆ SquaredDistance() [1/2]

 SEG::ecoord SEG::SquaredDistance ( const SEG & aSeg ) const

Definition at line 72 of file seg.cpp.

73 {
74  if( Intersects( aSeg ) )
75  return 0;
76
77  const VECTOR2I pts[4] =
78  {
79  aSeg.NearestPoint( A ) - A,
80  aSeg.NearestPoint( B ) - B,
81  NearestPoint( aSeg.A ) - aSeg.A,
82  NearestPoint( aSeg.B ) - aSeg.B
83  };
84
86
87  for( int i = 0; i < 4; i++ )
88  m = std::min( m, pts[i].SquaredEuclideanNorm() );
89
90  return m;
91 }
bool Intersects(const SEG &aSeg) const
Definition: seg.cpp:181
Define a general 2D-vector/point.
Definition: vector2d.h:61
static constexpr extended_type ECOORD_MAX
Definition: vector2d.h:79
VECTOR2I::extended_type ecoord
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Compute a point on the segment (this) that is closest to point aP.
Definition: seg.cpp:260
VECTOR2I A
Definition: seg.h:48
VECTOR2I B
Definition: seg.h:49

References A, B, VECTOR2< T >::ECOORD_MAX, Intersects(), and NearestPoint().

## ◆ SquaredDistance() [2/2]

 ecoord SEG::SquaredDistance ( const VECTOR2I & aP ) const
inline

Definition at line 242 of file seg.h.

243  {
244  return ( NearestPoint( aP ) - aP ).SquaredEuclideanNorm();
245  }
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Compute a point on the segment (this) that is closest to point aP.
Definition: seg.cpp:260

References NearestPoint().

## ◆ SquaredLength()

 ecoord SEG::SquaredLength ( ) const
inline

Definition at line 355 of file seg.h.

356  {
357  return ( A - B ).SquaredEuclideanNorm();
358  }

## ◆ TCoef()

 SEG::ecoord SEG::TCoef ( const VECTOR2I & aP ) const
inline

Definition at line 402 of file seg.h.

403 {
404  VECTOR2I d = B - A;
405  return d.Dot( aP - A);
406 }
Define a general 2D-vector/point.
Definition: vector2d.h:61
extended_type Dot(const VECTOR2< T > &aVector) const
Compute dot product of self with aVector.
Definition: vector2d.h:521
VECTOR2I A
Definition: seg.h:48

References A, and VECTOR2< T >::Dot().

Referenced by commonParallelProjection(), and PNS::commonParallelProjection().

## ◆ operator<<

 std::ostream& operator<< ( std::ostream & aStream, const SEG & aSeg )
friend

Definition at line 408 of file seg.h.

409 {
410  aStream << "[ " << aSeg.A << " - " << aSeg.B << " ]";
411
412  return aStream;
413 }
VECTOR2I A
Definition: seg.h:48
VECTOR2I B
Definition: seg.h:49

## ◆ m_index

 int SEG::m_index
private

< index within the parent shape (used when m_is_local == false)

Definition at line 399 of file seg.h.

Referenced by Index(), operator=(), and SEG().

The documentation for this class was generated from the following files: