Skip to content

Commit

Permalink
Merge pull request #209 from FriendsOfCADability/Issue208
Browse files Browse the repository at this point in the history
Fix issue 208 - IndexOutOfRange
  • Loading branch information
dsn27 authored Dec 27, 2024
2 parents 54847bb + 2138dd5 commit 3c23e44
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion CADability/Geometry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1678,7 +1678,9 @@ public static GeoPoint2D[] TangentPointCircle(GeoPoint2D p, GeoPoint2D c, double
// PC is distance between P and C, pc2 is PC^2
var pc2 = dx * dx + dy * dy;
var pc = Math.Sqrt(pc2);
if (pc < radius) return new GeoPoint2D[0]; // no solution

if (pc < radius - Precision.eps)
return new GeoPoint2D[0]; // no solution

// R is radius of circle centered in P, r2 is R^2
var r2 = pc2 - radius * radius;
Expand Down Expand Up @@ -1741,6 +1743,9 @@ public static GeoPoint2D[] TangentCC(GeoPoint2D center1, double radius1, GeoPoin
dc.Add(new Circle2D(center2, radius2));
#endif
GeoPoint2D[] tan = TangentPointCircle(center1, center2, radius2 - radius1); // must be two points
if (tan == null || tan.Length != 2)
throw new InvalidOperationException("TangentPointCircle did not return the expected tangent points.");

// must be 2 points
GeoVector2D v = tan[0] - center2;
v.Length = radius1;
Expand All @@ -1759,7 +1764,13 @@ public static GeoPoint2D[] TangentCC(GeoPoint2D center1, double radius1, GeoPoin
v.Length = radius1 * d / (radius1 + radius2);
GeoPoint2D c = center1 + v; // homothetic center
GeoPoint2D[] tan1 = TangentPointCircle(c, center1, radius1);
if (tan1 == null || tan1.Length != 2)
throw new InvalidOperationException("TangentPointCircle did not return the expected tangent points.");

GeoPoint2D[] tan2 = TangentPointCircle(c, center2, radius2);
if (tan2 == null || tan2.Length != 2)
throw new InvalidOperationException("TangentPointCircle did not return the expected tangent points.");

// must be two points for each
if (Geometry.OnSameSide(tan1[0], tan2[0], center1, center2))
{
Expand Down

0 comments on commit 3c23e44

Please sign in to comment.