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 );
373 constexpr double kCoincidentRadius = 5.0;
375 auto [minX, maxX] = std::minmax( { aStart.
x, aMid.
x, aEnd.
x } );
376 auto [minY, maxY] = std::minmax( { aStart.
y, aMid.
y, aEnd.
y } );
378 if( maxX - minX < kCoincidentRadius && maxY - minY < kCoincidentRadius )
380 return VECTOR2D( ( aStart.
x + aMid.
x + aEnd.
x ) / 3.0,
381 ( aStart.
y + aMid.
y + aEnd.
y ) / 3.0 );
386 double yDelta_21 = aMid.
y - aStart.
y;
387 double xDelta_21 = aMid.
x - aStart.
x;
388 double yDelta_32 = aEnd.
y - aMid.
y;
389 double xDelta_32 = aEnd.
x - aMid.
x;
394 if( ( ( xDelta_21 == 0.0 ) && ( yDelta_32 == 0.0 ) ) ||
395 ( ( yDelta_21 == 0.0 ) && ( xDelta_32 == 0.0 ) ) )
397 center.x = ( aStart.
x + aEnd.
x ) / 2.0;
398 center.y = ( aStart.
y + aEnd.
y ) / 2.0 ;
403 if( xDelta_21 == 0.0 )
404 xDelta_21 = std::numeric_limits<double>::epsilon();
406 if( xDelta_32 == 0.0 )
407 xDelta_32 = -std::numeric_limits<double>::epsilon();
409 double aSlope = yDelta_21 / xDelta_21;
410 double bSlope = yDelta_32 / xDelta_32;
415 if( aSlope == bSlope )
421 center.x = ( aStart.
x + aMid.
x ) / 2.0;
422 center.y = ( aStart.
y + aMid.
y ) / 2.0 ;
430 aSlope += std::numeric_limits<double>::epsilon();
431 bSlope -= std::numeric_limits<double>::epsilon();
434#ifdef USE_ALTERNATE_CENTER_ALGO
457 double abSlopeStartEndY = aSlope * bSlope * ( aStart.
y - aEnd.
y );
458 double dabSlopeStartEndY = abSlopeStartEndY *
459 std::sqrt( ( daSlope / aSlope * daSlope / aSlope )
460 + ( dbSlope / bSlope * dbSlope / bSlope )
461 + ( M_SQRT1_2 / ( aStart.
y - aEnd.
y )
462 * M_SQRT1_2 / ( aStart.
y - aEnd.
y ) ) );
464 double bSlopeStartMidX = bSlope * ( aStart.
x + aMid.
x );
465 double dbSlopeStartMidX = bSlopeStartMidX * std::sqrt( ( dbSlope / bSlope * dbSlope / bSlope )
466 + ( M_SQRT1_2 / ( aStart.
x + aMid.
x )
467 * M_SQRT1_2 / ( aStart.
x + aMid.
x ) ) );
469 double aSlopeMidEndX = aSlope * ( aMid.
x + aEnd.
x );
470 double daSlopeMidEndX = aSlopeMidEndX * std::sqrt( ( daSlope / aSlope * daSlope / aSlope )
471 + ( M_SQRT1_2 / ( aMid.
x + aEnd.
x )
472 * M_SQRT1_2 / ( aMid.
x + aEnd.
x ) ) );
474 double twiceBASlopeDiff = 2 * ( bSlope - aSlope );
475 double dtwiceBASlopeDiff = 2 * std::sqrt( dbSlope * dbSlope + daSlope * daSlope );
477 double centerNumeratorX = abSlopeStartEndY + bSlopeStartMidX - aSlopeMidEndX;
478 double dCenterNumeratorX = std::sqrt( dabSlopeStartEndY * dabSlopeStartEndY
479 + dbSlopeStartMidX * dbSlopeStartMidX
480 + daSlopeMidEndX * daSlopeMidEndX );
482 double centerX = ( abSlopeStartEndY + bSlopeStartMidX - aSlopeMidEndX ) / twiceBASlopeDiff;
483 double dCenterX = centerX * std::sqrt( ( dCenterNumeratorX / centerNumeratorX *
484 dCenterNumeratorX / centerNumeratorX )
485 + ( dtwiceBASlopeDiff / twiceBASlopeDiff *
486 dtwiceBASlopeDiff / twiceBASlopeDiff ) );
489 double centerNumeratorY = ( ( aStart.
x + aMid.
x ) / 2.0 - centerX );
490 double dCenterNumeratorY = std::sqrt( 1.0 / 8.0 + dCenterX * dCenterX );
492 double centerFirstTerm = centerNumeratorY / aSlope;
493 double dcenterFirstTermY = centerFirstTerm * std::sqrt(
494 ( dCenterNumeratorY/ centerNumeratorY *
495 dCenterNumeratorY / centerNumeratorY )
496 + ( daSlope / aSlope * daSlope / aSlope ) );
498 double centerY = centerFirstTerm + ( aStart.
y + aMid.
y ) / 2.0;
499 double dCenterY = std::sqrt( dcenterFirstTermY * dcenterFirstTermY + 1.0 / 8.0 );
501 double rounded100CenterX = std::floor( ( centerX + 50.0 ) / 100.0 ) * 100.0;
502 double rounded100CenterY = std::floor( ( centerY + 50.0 ) / 100.0 ) * 100.0;
503 double rounded10CenterX = std::floor( ( centerX + 5.0 ) / 10.0 ) * 10.0;
504 double rounded10CenterY = std::floor( ( centerY + 5.0 ) / 10.0 ) * 10.0;
511 if(
std::abs( rounded100CenterX - centerX ) < dCenterX &&
512 std::abs( rounded100CenterY - centerY ) < dCenterY )
514 center.x = rounded100CenterX;
515 center.y = rounded100CenterY;
517 else if(
std::abs( rounded10CenterX - centerX ) < dCenterX &&
518 std::abs( rounded10CenterY - centerY ) < dCenterY )
520 center.x = rounded10CenterX;
521 center.y = rounded10CenterY;
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.