fix index out of range when only single point remains

fixes #16
This commit is contained in:
Scott Williams 2017-04-13 13:30:40 +01:00
Родитель 7ebff77262
Коммит 922b5a61d0
3 изменённых файлов: 37 добавлений и 1 удалений

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

@ -200,6 +200,11 @@ namespace SixLabors.Shapes
/// <returns>number of intersections hit</returns> /// <returns>number of intersections hit</returns>
public int FindIntersections(Vector2 start, Vector2 end, Vector2[] buffer, int count, int offset) public int FindIntersections(Vector2 start, Vector2 end, Vector2[] buffer, int count, int offset)
{ {
if(this.points.Length < 2)
{
return 0;
}
ClampPoints(ref start, ref end); ClampPoints(ref start, ref end);
var target = new Segment(start, end); var target = new Segment(start, end);

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

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using Xunit;
namespace SixLabors.Shapes.Tests
{
/// <summary>
/// see https://github.com/SixLabors/Shapes/issues/16
/// Also for furter details see https://github.com/SixLabors/Fonts/issues/22
/// </summary>
public class Issue_16
{
[Fact]
public void IndexOutoufRangeException()
{
var p = new InternalPath(new[] { new Vector2(0, 0), new Vector2(0.000000001f, 0), new Vector2(0, 0.000000001f) }, true);
var inter = p.FindIntersections(Vector2.One, Vector2.Zero);
// if simplified to single point then we should never have an intersection
Assert.Equal(0, inter.Count());
}
}
}

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

@ -23,19 +23,23 @@ namespace SixLabors.Shapes
/// <returns>The points along the line the intersect with the boundaries of the polygon.</returns> /// <returns>The points along the line the intersect with the boundaries of the polygon.</returns>
internal static IEnumerable<Vector2> FindIntersections(this InternalPath path, Vector2 start, Vector2 end) internal static IEnumerable<Vector2> FindIntersections(this InternalPath path, Vector2 start, Vector2 end)
{ {
List<Vector2> results = new List<Vector2>();
var buffer = ArrayPool<Vector2>.Shared.Rent(path.PointCount); var buffer = ArrayPool<Vector2>.Shared.Rent(path.PointCount);
try try
{ {
var hits = path.FindIntersections(start, end, buffer, path.PointCount, 0); var hits = path.FindIntersections(start, end, buffer, path.PointCount, 0);
for (var i = 0; i < hits; i++) for (var i = 0; i < hits; i++)
{ {
yield return buffer[i]; results.Add(buffer[i]);
} }
} }
finally finally
{ {
ArrayPool<Vector2>.Shared.Return(buffer); ArrayPool<Vector2>.Shared.Return(buffer);
} }
return results;
} }
} }
} }