Skip to content

Commit

Permalink
Optimise method
Browse files Browse the repository at this point in the history
Avoid duplicate calculations

(cherry picked from commit e227e93)
  • Loading branch information
nyalldawson committed Jun 10, 2019
1 parent f298a85 commit 5635c4f
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/core/geometry/qgsgeometryutils.cpp
Expand Up @@ -327,10 +327,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 @@ -342,12 +342,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 5635c4f

Please sign in to comment.