Skip to content

Commit

Permalink
Optimise method
Browse files Browse the repository at this point in the history
Avoid duplicate calculations
  • Loading branch information
nyalldawson committed May 30, 2019
1 parent e0aa09c commit e227e93
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/core/geometry/qgsgeometryutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,10 @@ bool QgsGeometryUtils::lineCircleIntersection( const QgsPointXY &center, const d
const double dx = x2 - x1;
const double dy = y2 - y1;

const double dr = std::sqrt( std::pow( dx, 2 ) + std::pow( dy, 2 ) );
const double dr2 = std::pow( dx, 2 ) + std::pow( dy, 2 );
const double d = x1 * y2 - x2 * y1;

const double disc = std::pow( radius, 2 ) * std::pow( dr, 2 ) - std::pow( d, 2 );
const double disc = std::pow( radius, 2 ) * dr2 - std::pow( d, 2 );

if ( disc < 0 )
{
Expand All @@ -343,12 +343,14 @@ bool QgsGeometryUtils::lineCircleIntersection( const QgsPointXY &center, const d
// two solutions
const int sgnDy = dy < 0 ? -1 : 1;

const double ax = center.x() + ( d * dy + sgnDy * dx * std::sqrt( std::pow( radius, 2 ) * std::pow( dr, 2 ) - std::pow( d, 2 ) ) ) / ( std::pow( dr, 2 ) );
const double ay = center.y() + ( -d * dx + std::fabs( dy ) * std::sqrt( std::pow( radius, 2 ) * std::pow( dr, 2 ) - std::pow( d, 2 ) ) ) / ( std::pow( dr, 2 ) );
const double sqrDisc = std::sqrt( disc );

const double ax = center.x() + ( d * dy + sgnDy * dx * sqrDisc ) / dr2;
const double ay = center.y() + ( -d * dx + std::fabs( dy ) * sqrDisc ) / dr2;
const QgsPointXY p1( ax, ay );

const double bx = center.x() + ( d * dy - sgnDy * dx * std::sqrt( std::pow( radius, 2 ) * std::pow( dr, 2 ) - std::pow( d, 2 ) ) ) / ( std::pow( dr, 2 ) );
const double by = center.y() + ( -d * dx - std::fabs( dy ) * std::sqrt( std::pow( radius, 2 ) * std::pow( dr, 2 ) - std::pow( d, 2 ) ) ) / ( std::pow( dr, 2 ) );
const double bx = center.x() + ( d * dy - sgnDy * dx * sqrDisc ) / dr2;
const double by = center.y() + ( -d * dx - std::fabs( dy ) * sqrDisc ) / dr2;
const QgsPointXY p2( bx, by );

// snap to nearest intersection
Expand Down

0 comments on commit e227e93

Please sign in to comment.