Skip to content

Commit e85396a

Browse files
authored
Merge pull request #49 from SixLabors/sw/zero-length-polygons
Prevent internal path throwing index out of range for an empty path
2 parents a45cb1c + 4f216e5 commit e85396a

2 files changed

Lines changed: 39 additions & 7 deletions

File tree

src/ImageSharp.Drawing/Shapes/InternalPath.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,21 @@ private InternalPath(PointData[] points, bool isClosedPath)
7676
this.points = points;
7777
this.closedPath = isClosedPath;
7878

79-
float minX = this.points.Min(x => x.Point.X);
80-
float maxX = this.points.Max(x => x.Point.X);
81-
float minY = this.points.Min(x => x.Point.Y);
82-
float maxY = this.points.Max(x => x.Point.Y);
79+
if (this.points.Length > 0)
80+
{
81+
float minX = this.points.Min(x => x.Point.X);
82+
float maxX = this.points.Max(x => x.Point.X);
83+
float minY = this.points.Min(x => x.Point.Y);
84+
float maxY = this.points.Max(x => x.Point.Y);
8385

84-
this.Bounds = new RectangleF(minX, minY, maxX - minX, maxY - minY);
85-
this.Length = this.points.Sum(x => x.Length);
86+
this.Bounds = new RectangleF(minX, minY, maxX - minX, maxY - minY);
87+
this.Length = this.points.Sum(x => x.Length);
88+
}
89+
else
90+
{
91+
this.Bounds = RectangleF.Empty;
92+
this.Length = 0;
93+
}
8694
}
8795

8896
/// <summary>
@@ -684,10 +692,14 @@ private static PointData[] Simplify(IEnumerable<ILineSegment> segments, bool isC
684692
private static PointData[] Simplify(IEnumerable<PointF> vectors, bool isClosed)
685693
{
686694
PointF[] points = vectors.ToArray();
687-
var results = new List<PointData>();
688695

689696
int polyCorners = points.Length;
697+
if (polyCorners == 0)
698+
{
699+
return Array.Empty<PointData>();
700+
}
690701

702+
var results = new List<PointData>();
691703
Vector2 lastPoint = points[0];
692704

693705
if (!isClosed)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Drawing;
4+
using System.Text;
5+
using Xunit;
6+
7+
namespace SixLabors.ImageSharp.Drawing.Tests.Issues
8+
{
9+
// https://github.com/SixLabors/ImageSharp.Drawing/issues/48
10+
// index out of rang error if zero length ChildPath of a ComplexPolygon
11+
public class Issues_48
12+
{
13+
[Fact]
14+
public void DoNotThowIfPolygonHasZeroLength()
15+
{
16+
var emptyPoly = new ComplexPolygon(new Polygon());
17+
emptyPoly.FindIntersections(new PointF(0, 0), new PointF(100, 100));
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)