Skip to content

Commit

Permalink
Fix GeometryExtensions.Contains() when rectangle area is 0
Browse files Browse the repository at this point in the history
  • Loading branch information
BobLd committed Jul 20, 2024
1 parent 65c644f commit a99c0d2
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions src/UglyToad.PdfPig/Geometry/GeometryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -389,45 +389,49 @@ static double polarAngle(in PdfPoint point1, in PdfPoint point2)
/// <param name="includeBorder">If set to false, will return false if the point belongs to the border.</param>
public static bool Contains(this PdfRectangle rectangle, PdfPoint point, bool includeBorder = false)
{
if (Math.Abs(rectangle.Area) < epsilon)
{
return false;
}

if (Math.Abs(rectangle.Rotation) < epsilon)
{
if (includeBorder)
{
return point.X >= rectangle.Left &&
point.X <= rectangle.Right &&
point.Y >= rectangle.Bottom &&
point.Y <= rectangle.Top;
point.X <= rectangle.Right &&
point.Y >= rectangle.Bottom &&
point.Y <= rectangle.Top;
}

return point.X > rectangle.Left &&
point.X < rectangle.Right &&
point.Y > rectangle.Bottom &&
point.Y < rectangle.Top;
}
else
{
static double area(in PdfPoint p1, PdfPoint p2, PdfPoint p3)
{
return Math.Abs((p2.X * p1.Y - p1.X * p2.Y) + (p3.X * p2.Y - p2.X * p3.Y) + (p1.X * p3.Y - p3.X * p1.Y)) / 2.0;
}

var area1 = area(rectangle.BottomLeft, point, rectangle.TopLeft);
var area2 = area(rectangle.TopLeft, point, rectangle.TopRight);
var area3 = area(rectangle.TopRight, point, rectangle.BottomRight);
var area4 = area(rectangle.BottomRight, point, rectangle.BottomLeft);
static double area(in PdfPoint p1, PdfPoint p2, PdfPoint p3)
{
return Math.Abs((p2.X * p1.Y - p1.X * p2.Y) + (p3.X * p2.Y - p2.X * p3.Y) +
(p1.X * p3.Y - p3.X * p1.Y)) / 2.0;
}

var sum = area1 + area2 + area3 + area4; // sum is always greater or equal to area
var area1 = area(rectangle.BottomLeft, point, rectangle.TopLeft);
var area2 = area(rectangle.TopLeft, point, rectangle.TopRight);
var area3 = area(rectangle.TopRight, point, rectangle.BottomRight);
var area4 = area(rectangle.BottomRight, point, rectangle.BottomLeft);

if (sum - rectangle.Area > epsilon) return false;
var sum = area1 + area2 + area3 + area4; // sum is always greater or equal to area

if (area1 < epsilon || area2 < epsilon || area3 < epsilon || area4 < epsilon)
{
// point is on the rectangle
return includeBorder;
}
if (sum - rectangle.Area > epsilon) return false;

return true;
if (area1 < epsilon || area2 < epsilon || area3 < epsilon || area4 < epsilon)
{
// point is on the rectangle
return includeBorder;
}

return true;
}

/// <summary>
Expand Down

0 comments on commit a99c0d2

Please sign in to comment.