67#ifdef USE_ALTERNATE_CENTER_ALGO
73 double bc = ( b.
x*b.
x + b.
y*b.
y ) / 2.0;
74 double cd = ( -d.
x*d.
x - d.
y*d.
y ) / 2.0;
75 double det = -b.
x*d.
y + d.
x*b.
y;
77 if( fabs(det) < 1.0e-6 )
81 aCenter->
x = ( -bc*d.
y - cd*b.
y ) * det;
82 aCenter->
y = ( b.
x*cd + d.
x*bc ) * det;
92 VECTOR2I vectSeg = aSegEnd - aSegStart;
93 VECTOR2I vectPoint = aTestPoint - aSegStart;
96 if( (
long long) vectSeg.
x * vectPoint.
y - (
long long) vectSeg.
y * vectPoint.
x )
99 if( ( (
long long) vectSeg.
x * vectPoint.
x + (
long long) vectSeg.
y * vectPoint.
y ) <
100 ( (
long long) vectPoint.
x * vectPoint.
x + (
long long) vectPoint.
y * vectPoint.
y ) )
115 int64_t dX_a, dY_a, dX_b, dY_b, dX_ab, dY_ab;
116 int64_t num_a, num_b, den;
124 dX_a = int64_t{ a_p2_l1.
x } - a_p1_l1.
x;
125 dY_a = int64_t{ a_p2_l1.
y } - a_p1_l1.
y;
126 dX_b = int64_t{ a_p2_l2.
x } - a_p1_l2.
x;
127 dY_b = int64_t{ a_p2_l2.
y } - a_p1_l2.
y;
128 dX_ab = int64_t{ a_p1_l2.
x } - a_p1_l1.
x;
129 dY_ab = int64_t{ a_p1_l2.
y } - a_p1_l1.
y;
131 den = dY_a * dX_b - dY_b * dX_a ;
137 num_a = dY_ab * dX_b - dY_b * dX_ab;
138 num_b = dY_ab * dX_a - dY_a * dX_ab;
141 if( aIntersectionPoint )
143 *aIntersectionPoint = a_p1_l1;
144 aIntersectionPoint->
x +=
KiROUND( dX_a * (
double )num_a / (
double )den );
145 aIntersectionPoint->
y +=
KiROUND( dY_a * (
double )num_b / (
double )den );
185 std::swap( xmax, xmin );
188 std::swap( ymax, ymin );
191 if( ( ymin - aRefPoint.
y > aDist ) || ( aRefPoint.
y - ymax > aDist ) )
194 if( ( xmin - aRefPoint.
x > aDist ) || ( aRefPoint.
x - xmax > aDist ) )
198 if( aStart.
x == aEnd.
x && aRefPoint.
y > ymin && aRefPoint.
y < ymax )
201 if( aStart.
y == aEnd.
y && aRefPoint.
x > xmin && aRefPoint.
x < xmax )
204 SEG segment( aStart, aEnd );
212 VECTOR2I startVector = aStart - aCenter;
213 VECTOR2I endVector = aEnd - aCenter;
217 EDA_ANGLE midPointRotAngle = ( startAngle - endAngle ).Normalize180() / 2;
255 double sinus = angle.
Sin();
256 double cosinus = angle.
Cos();
258 pt.
x =
KiROUND( ( *pY * sinus ) + ( *pX * cosinus ) );
259 pt.
y =
KiROUND( ( *pY * cosinus ) - ( *pX * sinus ) );
321 double sinus = angle.
Sin();
322 double cosinus = angle.
Cos();
324 pt.
x = ( *pY * sinus ) + ( *pX * cosinus );
325 pt.
y = ( *pY * cosinus ) - ( *pX * sinus );
342 std::swap( start, end );
348 std::swap( start, end );
352 double chord = ( start - end ).EuclideanNorm();
353 double r = ( chord / 2.0 ) / ( angle / 2.0 ).
Sin();
354 double d_squared = r * r - chord* chord / 4.0;
357 if( d_squared > 0.0 )
358 d = sqrt( d_squared );
365 return VECTOR2D( start + vc + vec2 );
373 double yDelta_21 = aMid.
y - aStart.
y;
374 double xDelta_21 = aMid.
x - aStart.
x;
375 double yDelta_32 = aEnd.
y - aMid.
y;
376 double xDelta_32 = aEnd.
x - aMid.
x;
381 if( ( ( xDelta_21 == 0.0 ) && ( yDelta_32 == 0.0 ) ) ||
382 ( ( yDelta_21 == 0.0 ) && ( xDelta_32 == 0.0 ) ) )
384 center.
x = ( aStart.
x + aEnd.
x ) / 2.0;
385 center.
y = ( aStart.
y + aEnd.
y ) / 2.0 ;
390 if( xDelta_21 == 0.0 )
391 xDelta_21 = std::numeric_limits<double>::epsilon();
393 if( xDelta_32 == 0.0 )
394 xDelta_32 = -std::numeric_limits<double>::epsilon();
396 double aSlope = yDelta_21 / xDelta_21;
397 double bSlope = yDelta_32 / xDelta_32;
402 if( aSlope == bSlope )
408 center.
x = ( aStart.
x + aMid.
x ) / 2.0;
409 center.
y = ( aStart.
y + aMid.
y ) / 2.0 ;
417 aSlope += std::numeric_limits<double>::epsilon();
418 bSlope -= std::numeric_limits<double>::epsilon();
421#ifdef USE_ALTERNATE_CENTER_ALGO
444 double abSlopeStartEndY = aSlope * bSlope * ( aStart.
y - aEnd.
y );
445 double dabSlopeStartEndY = abSlopeStartEndY *
446 std::sqrt( ( daSlope / aSlope * daSlope / aSlope )
447 + ( dbSlope / bSlope * dbSlope / bSlope )
448 + ( M_SQRT1_2 / ( aStart.
y - aEnd.
y )
449 * M_SQRT1_2 / ( aStart.
y - aEnd.
y ) ) );
451 double bSlopeStartMidX = bSlope * ( aStart.
x + aMid.
x );
452 double dbSlopeStartMidX = bSlopeStartMidX * std::sqrt( ( dbSlope / bSlope * dbSlope / bSlope )
453 + ( M_SQRT1_2 / ( aStart.
x + aMid.
x )
454 * M_SQRT1_2 / ( aStart.
x + aMid.
x ) ) );
456 double aSlopeMidEndX = aSlope * ( aMid.
x + aEnd.
x );
457 double daSlopeMidEndX = aSlopeMidEndX * std::sqrt( ( daSlope / aSlope * daSlope / aSlope )
458 + ( M_SQRT1_2 / ( aMid.
x + aEnd.
x )
459 * M_SQRT1_2 / ( aMid.
x + aEnd.
x ) ) );
461 double twiceBASlopeDiff = 2 * ( bSlope - aSlope );
462 double dtwiceBASlopeDiff = 2 * std::sqrt( dbSlope * dbSlope + daSlope * daSlope );
464 double centerNumeratorX = abSlopeStartEndY + bSlopeStartMidX - aSlopeMidEndX;
465 double dCenterNumeratorX = std::sqrt( dabSlopeStartEndY * dabSlopeStartEndY
466 + dbSlopeStartMidX * dbSlopeStartMidX
467 + daSlopeMidEndX * daSlopeMidEndX );
469 double centerX = ( abSlopeStartEndY + bSlopeStartMidX - aSlopeMidEndX ) / twiceBASlopeDiff;
470 double dCenterX = centerX * std::sqrt( ( dCenterNumeratorX / centerNumeratorX *
471 dCenterNumeratorX / centerNumeratorX )
472 + ( dtwiceBASlopeDiff / twiceBASlopeDiff *
473 dtwiceBASlopeDiff / twiceBASlopeDiff ) );
476 double centerNumeratorY = ( ( aStart.
x + aMid.
x ) / 2.0 - centerX );
477 double dCenterNumeratorY = std::sqrt( 1.0 / 8.0 + dCenterX * dCenterX );
479 double centerFirstTerm = centerNumeratorY / aSlope;
480 double dcenterFirstTermY = centerFirstTerm * std::sqrt(
481 ( dCenterNumeratorY/ centerNumeratorY *
482 dCenterNumeratorY / centerNumeratorY )
483 + ( daSlope / aSlope * daSlope / aSlope ) );
485 double centerY = centerFirstTerm + ( aStart.
y + aMid.
y ) / 2.0;
486 double dCenterY = std::sqrt( dcenterFirstTermY * dcenterFirstTermY + 1.0 / 8.0 );
488 double rounded100CenterX = std::floor( ( centerX + 50.0 ) / 100.0 ) * 100.0;
489 double rounded100CenterY = std::floor( ( centerY + 50.0 ) / 100.0 ) * 100.0;
490 double rounded10CenterX = std::floor( ( centerX + 5.0 ) / 10.0 ) * 10.0;
491 double rounded10CenterY = std::floor( ( centerY + 5.0 ) / 10.0 ) * 10.0;
498 if(
std::abs( rounded100CenterX - centerX ) < dCenterX &&
499 std::abs( rounded100CenterY - centerY ) < dCenterY )
501 center.
x = rounded100CenterX;
502 center.
y = rounded100CenterY;
504 else if(
std::abs( rounded10CenterX - centerX ) < dCenterX &&
505 std::abs( rounded10CenterY - centerY ) < dCenterY )
507 center.
x = rounded10CenterX;
508 center.
y = rounded10CenterY;
523 VECTOR2D dStart(
static_cast<double>( aStart.
x ),
static_cast<double>( aStart.
y ) );
524 VECTOR2D dMid(
static_cast<double>( aMid.
x ),
static_cast<double>( aMid.
y ) );
525 VECTOR2D dEnd(
static_cast<double>( aEnd.
x ),
static_cast<double>( aEnd.
y ) );
530 iCenter.
x =
KiROUND( std::clamp( dCenter.
x,
531 double( std::numeric_limits<int>::min() + 100 ),
532 double( std::numeric_limits<int>::max() - 100 ) ) );
534 iCenter.
y =
KiROUND( std::clamp( dCenter.
y,
535 double( std::numeric_limits<int>::min() + 100 ),
536 double( std::numeric_limits<int>::max() - 100 ) ) );
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
ecoord SquaredDistance(const SEG &aSeg) const
static SEG::ecoord Square(int a)
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
VECTOR2< T > Resize(T aNewLength) const
Return a vector of the same direction, but length specified in aNewLength.
static constexpr EDA_ANGLE ANGLE_0
static constexpr EDA_ANGLE ANGLE_90
static constexpr EDA_ANGLE ANGLE_270
static constexpr EDA_ANGLE ANGLE_360
static constexpr EDA_ANGLE ANGLE_180
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
static VECTOR2D CircleCenterFrom3Points(const VECTOR2D &p1, const VECTOR2D &p2, const VECTOR2D &p3)
const VECTOR2I CalcArcMid(const VECTOR2I &aStart, const VECTOR2I &aEnd, const VECTOR2I &aCenter, bool aMinArcAngle)
Return the middle point of an arc, half-way between aStart and aEnd.
bool TestSegmentHit(const VECTOR2I &aRefPoint, const VECTOR2I &aStart, const VECTOR2I &aEnd, int aDist)
Test if aRefPoint is with aDistance on the line defined by aStart and aEnd.
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Calculate the new point of coord coord pX, pY, for a rotation center 0, 0.
bool SegmentIntersectsSegment(const VECTOR2I &a_p1_l1, const VECTOR2I &a_p2_l1, const VECTOR2I &a_p1_l2, const VECTOR2I &a_p2_l2, VECTOR2I *aIntersectionPoint)
Test if two lines intersect.
bool IsPointOnSegment(const VECTOR2I &aSegStart, const VECTOR2I &aSegEnd, const VECTOR2I &aTestPoint)
Test if aTestPoint is on line defined by aSegStart and aSegEnd.
const VECTOR2D CalcArcCenter(const VECTOR2D &aStart, const VECTOR2D &aEnd, const EDA_ANGLE &aAngle)
VECTOR2< int32_t > VECTOR2I
VECTOR2< double > VECTOR2D