fix issue rounding issue loosing crossing point.

This commit is contained in:
Scott Williams 2017-01-31 18:00:38 +00:00
Родитель d2f148db42
Коммит 9b2cb8918f
2 изменённых файлов: 27 добавлений и 11 удалений

Просмотреть файл

@ -16,6 +16,11 @@ namespace SixLabors.Shapes
/// </summary> /// </summary>
internal class InternalPath internal class InternalPath
{ {
/// <summary>
/// The epsilon for float comparison
/// </summary>
private const float Epsilon = 0.00001f;
/// <summary> /// <summary>
/// The maximum vector /// The maximum vector
/// </summary> /// </summary>
@ -324,15 +329,15 @@ namespace SixLabors.Shapes
Vector2 topLeft2 = Vector2.Min(line2Start, line2End); Vector2 topLeft2 = Vector2.Min(line2Start, line2End);
Vector2 bottomRight2 = Vector2.Max(line2Start, line2End); Vector2 bottomRight2 = Vector2.Max(line2Start, line2End);
float left1 = topLeft1.X; float left1 = topLeft1.X - Epsilon;
float right1 = bottomRight1.X; float right1 = bottomRight1.X + Epsilon;
float top1 = topLeft1.Y; float top1 = topLeft1.Y - Epsilon;
float bottom1 = bottomRight1.Y; float bottom1 = bottomRight1.Y + Epsilon;
float left2 = topLeft2.X; float left2 = topLeft2.X - Epsilon;
float right2 = bottomRight2.X; float right2 = bottomRight2.X + Epsilon;
float top2 = topLeft2.Y; float top2 = topLeft2.Y - Epsilon;
float bottom2 = bottomRight2.Y; float bottom2 = bottomRight2.Y + Epsilon;
return left1 <= right2 && right1 >= left2 return left1 <= right2 && right1 >= left2
&& &&
@ -362,7 +367,7 @@ namespace SixLabors.Shapes
Vector2 line2Diff = line2End - line2Start; Vector2 line2Diff = line2End - line2Start;
Vector2 point; Vector2 point;
if (Math.Abs(line1Diff.X) < float.Epsilon) if (Math.Abs(line1Diff.X) < Epsilon)
{ {
float slope = line2Diff.Y / line2Diff.X; float slope = line2Diff.Y / line2Diff.X;
float yinter = line2Start.Y - (slope * line2Start.X); float yinter = line2Start.Y - (slope * line2Start.X);
@ -371,7 +376,7 @@ namespace SixLabors.Shapes
// horizontal and vertical lines // horizontal and vertical lines
} }
else if (Math.Abs(line2Diff.X) < float.Epsilon) else if (Math.Abs(line2Diff.X) < Epsilon)
{ {
float slope = line1Diff.Y / line1Diff.X; float slope = line1Diff.Y / line1Diff.X;
float yInter = line1Start.Y - (slope * line1Start.X); float yInter = line1Start.Y - (slope * line1Start.X);
@ -388,7 +393,7 @@ namespace SixLabors.Shapes
float yInter1 = line1Start.Y - (slope1 * line1Start.X); float yInter1 = line1Start.Y - (slope1 * line1Start.X);
float yInter2 = line2Start.Y - (slope2 * line2Start.X); float yInter2 = line2Start.Y - (slope2 * line2Start.X);
if (Math.Abs(slope1 - slope2) < float.Epsilon && Math.Abs(yInter1 - yInter2) > float.Epsilon) if (Math.Abs(slope1 - slope2) < Epsilon && Math.Abs(yInter1 - yInter2) > Epsilon)
{ {
return MaxVector; return MaxVector;
} }

Просмотреть файл

@ -187,5 +187,16 @@ namespace SixLabors.Shapes.Tests
// with linear polygons its the number of points the segments have // with linear polygons its the number of points the segments have
Assert.Equal(2, poly.MaxIntersections); Assert.Equal(2, poly.MaxIntersections);
} }
[Fact]
public void FindBothIntersections()
{
var poly = new Polygon(new LinearLineSegment(
new Vector2(10, 10),
new Vector2(200, 150),
new Vector2(50, 300)));
var intersections = poly.FindIntersections(new Vector2(float.MinValue, 55), new Vector2(float.MaxValue, 55));
Assert.Equal(2, intersections.Count());
}
} }
} }