111 int64_t dX_a, dY_a, dX_b, dY_b, dX_ab, dY_ab;
112 int64_t num_a, num_b, den;
120 dX_a = int64_t{ a_p2_l1.
x } - a_p1_l1.
x;
121 dY_a = int64_t{ a_p2_l1.
y } - a_p1_l1.
y;
122 dX_b = int64_t{ a_p2_l2.
x } - a_p1_l2.
x;
123 dY_b = int64_t{ a_p2_l2.
y } - a_p1_l2.
y;
124 dX_ab = int64_t{ a_p1_l2.
x } - a_p1_l1.
x;
125 dY_ab = int64_t{ a_p1_l2.
y } - a_p1_l1.
y;
127 den = dY_a * dX_b - dY_b * dX_a ;
133 num_a = dY_ab * dX_b - dY_b * dX_ab;
134 num_b = dY_ab * dX_a - dY_a * dX_ab;
137 if( aIntersectionPoint )
139 *aIntersectionPoint = a_p1_l1;
140 aIntersectionPoint->
x +=
KiROUND( dX_a * (
double )num_a / (
double )den );
141 aIntersectionPoint->
y +=
KiROUND( dY_a * (
double )num_b / (
double )den );
375 constexpr double kCoincidentRadius = 5.0;
377 auto [minX, maxX] = std::minmax( { aStart.
x, aMid.
x, aEnd.
x } );
378 auto [minY, maxY] = std::minmax( { aStart.
y, aMid.
y, aEnd.
y } );
380 if( maxX - minX < kCoincidentRadius && maxY - minY < kCoincidentRadius )
382 return VECTOR2D( ( aStart.
x + aMid.
x + aEnd.
x ) / 3.0,
383 ( aStart.
y + aMid.
y + aEnd.
y ) / 3.0 );
388 double yDelta_21 = aMid.
y - aStart.
y;
389 double xDelta_21 = aMid.
x - aStart.
x;
390 double yDelta_32 = aEnd.
y - aMid.
y;
391 double xDelta_32 = aEnd.
x - aMid.
x;
396 if( ( ( xDelta_21 == 0.0 ) && ( yDelta_32 == 0.0 ) ) ||
397 ( ( yDelta_21 == 0.0 ) && ( xDelta_32 == 0.0 ) ) )
399 center.x = ( aStart.
x + aEnd.
x ) / 2.0;
400 center.y = ( aStart.
y + aEnd.
y ) / 2.0 ;
405 if( xDelta_21 == 0.0 )
406 xDelta_21 = std::numeric_limits<double>::epsilon();
408 if( xDelta_32 == 0.0 )
409 xDelta_32 = -std::numeric_limits<double>::epsilon();
411 double aSlope = yDelta_21 / xDelta_21;
412 double bSlope = yDelta_32 / xDelta_32;
416 if( yDelta_21 == 0.0 )
417 yDelta_21 = std::numeric_limits<double>::epsilon();
419 if( yDelta_32 == 0.0 )
420 yDelta_32 = std::numeric_limits<double>::epsilon();
425 if( aSlope == bSlope )
431 center.x = ( aStart.
x + aMid.
x ) / 2.0;
432 center.y = ( aStart.
y + aMid.
y ) / 2.0 ;
440 aSlope += std::numeric_limits<double>::epsilon();
441 bSlope -= std::numeric_limits<double>::epsilon();
444#ifdef USE_ALTERNATE_CENTER_ALGO
467 double abSlopeStartEndY = aSlope * bSlope * ( aStart.
y - aEnd.
y );
468 double dabSlopeStartEndY = abSlopeStartEndY *
469 std::sqrt( ( daSlope / aSlope * daSlope / aSlope )
470 + ( dbSlope / bSlope * dbSlope / bSlope )
471 + ( M_SQRT1_2 / ( aStart.
y - aEnd.
y )
472 * M_SQRT1_2 / ( aStart.
y - aEnd.
y ) ) );
474 double bSlopeStartMidX = bSlope * ( aStart.
x + aMid.
x );
475 double dbSlopeStartMidX = bSlopeStartMidX * std::sqrt( ( dbSlope / bSlope * dbSlope / bSlope )
476 + ( M_SQRT1_2 / ( aStart.
x + aMid.
x )
477 * M_SQRT1_2 / ( aStart.
x + aMid.
x ) ) );
479 double aSlopeMidEndX = aSlope * ( aMid.
x + aEnd.
x );
480 double daSlopeMidEndX = aSlopeMidEndX * std::sqrt( ( daSlope / aSlope * daSlope / aSlope )
481 + ( M_SQRT1_2 / ( aMid.
x + aEnd.
x )
482 * M_SQRT1_2 / ( aMid.
x + aEnd.
x ) ) );
484 double twiceBASlopeDiff = 2 * ( bSlope - aSlope );
485 double dtwiceBASlopeDiff = 2 * std::sqrt( dbSlope * dbSlope + daSlope * daSlope );
487 double centerNumeratorX = abSlopeStartEndY + bSlopeStartMidX - aSlopeMidEndX;
488 double dCenterNumeratorX = std::sqrt( dabSlopeStartEndY * dabSlopeStartEndY
489 + dbSlopeStartMidX * dbSlopeStartMidX
490 + daSlopeMidEndX * daSlopeMidEndX );
492 double centerX = ( abSlopeStartEndY + bSlopeStartMidX - aSlopeMidEndX ) / twiceBASlopeDiff;
493 double dCenterX = centerX * std::sqrt( ( dCenterNumeratorX / centerNumeratorX *
494 dCenterNumeratorX / centerNumeratorX )
495 + ( dtwiceBASlopeDiff / twiceBASlopeDiff *
496 dtwiceBASlopeDiff / twiceBASlopeDiff ) );
499 double centerNumeratorY = ( ( aStart.
x + aMid.
x ) / 2.0 - centerX );
500 double dCenterNumeratorY = std::sqrt( 1.0 / 8.0 + dCenterX * dCenterX );
502 double centerFirstTerm = centerNumeratorY / aSlope;
503 double dcenterFirstTermY = centerFirstTerm * std::sqrt(
504 ( dCenterNumeratorY/ centerNumeratorY *
505 dCenterNumeratorY / centerNumeratorY )
506 + ( daSlope / aSlope * daSlope / aSlope ) );
508 double centerY = centerFirstTerm + ( aStart.
y + aMid.
y ) / 2.0;
509 double dCenterY = std::sqrt( dcenterFirstTermY * dcenterFirstTermY + 1.0 / 8.0 );
511 double rounded100CenterX = std::floor( ( centerX + 50.0 ) / 100.0 ) * 100.0;
512 double rounded100CenterY = std::floor( ( centerY + 50.0 ) / 100.0 ) * 100.0;
513 double rounded10CenterX = std::floor( ( centerX + 5.0 ) / 10.0 ) * 10.0;
514 double rounded10CenterY = std::floor( ( centerY + 5.0 ) / 10.0 ) * 10.0;
521 if(
std::abs( rounded100CenterX - centerX ) < dCenterX &&
522 std::abs( rounded100CenterY - centerY ) < dCenterY )
524 center.x = rounded100CenterX;
525 center.y = rounded100CenterY;
527 else if(
std::abs( rounded10CenterX - centerX ) < dCenterX &&
528 std::abs( rounded10CenterY - centerY ) < dCenterY )
530 center.x = rounded10CenterX;
531 center.y = rounded10CenterY;