Skip to content

Commit 42054e0

Browse files
Delete Distance(PointF point)
1 parent 747ccef commit 42054e0

8 files changed

Lines changed: 3 additions & 335 deletions

File tree

src/ImageSharp.Drawing/Shapes/ComplexPolygon.cs

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -125,52 +125,6 @@ public ComplexPolygon(params IPath[] paths)
125125
/// <inheritdoc/>
126126
int IPathInternals.MaxIntersections => this.maxIntersections;
127127

128-
/// <inheritdoc/>
129-
/// <remarks>
130-
/// Due to the clipping we did during construction we know that out shapes do not overlap at their edges
131-
/// therefore for a point to be in more that one we must be in a hole of another, theoretically this could
132-
/// then flip again to be in a outline inside a hole inside an outline :)
133-
/// </remarks>
134-
PointInfo IPathInternals.Distance(PointF point)
135-
{
136-
float dist = float.MaxValue;
137-
PointInfo pointInfo = default;
138-
bool inside = false;
139-
foreach (IPath shape in this.Paths)
140-
{
141-
PointInfo d;
142-
if (shape is IPathInternals internals)
143-
{
144-
d = internals.Distance(point);
145-
}
146-
else
147-
{
148-
// TODO: How can I solve this?
149-
throw new NotImplementedException();
150-
}
151-
152-
if (d.DistanceFromPath <= 0)
153-
{
154-
// we are inside a poly
155-
d.DistanceFromPath = -d.DistanceFromPath; // flip the sign
156-
inside ^= true; // flip the inside flag
157-
}
158-
159-
if (d.DistanceFromPath < dist)
160-
{
161-
dist = d.DistanceFromPath;
162-
pointInfo = d;
163-
}
164-
}
165-
166-
if (inside)
167-
{
168-
pointInfo.DistanceFromPath = -pointInfo.DistanceFromPath;
169-
}
170-
171-
return pointInfo;
172-
}
173-
174128
/// <inheritdoc />
175129
int IPathInternals.FindIntersections(PointF start, PointF end, Span<PointF> intersections, Span<PointOrientation> orientations)
176130
=> ((IPathInternals)this).FindIntersections(start, end, intersections, orientations, IntersectionRule.OddEven);
@@ -272,12 +226,7 @@ public IPath Transform(Matrix3x2 matrix)
272226
return new ComplexPolygon(shapes);
273227
}
274228

275-
/// <summary>
276-
/// Converts the <see cref="IPath" /> into a simple linear path..
277-
/// </summary>
278-
/// <returns>
279-
/// Returns the current <see cref="IPath" /> as simple linear path.
280-
/// </returns>
229+
/// <inheritdoc />
281230
public IEnumerable<ISimplePath> Flatten()
282231
{
283232
var paths = new List<ISimplePath>();

src/ImageSharp.Drawing/Shapes/EllipsePolygon.cs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,6 @@ private EllipsePolygon(CubicBezierLineSegment segment)
9090
/// <inheritdoc />
9191
public float Length => this.innerPath.Length;
9292

93-
/// <inheritdoc />
94-
PointInfo IPathInternals.Distance(PointF point)
95-
{
96-
PointInfo dist = this.innerPath.DistanceFromPath(point);
97-
bool isInside = this.innerPath.PointInPolygon(point);
98-
if (isInside)
99-
{
100-
dist.DistanceFromPath *= -1;
101-
}
102-
103-
return dist;
104-
}
105-
10693
/// <summary>
10794
/// Transforms the rectangle using specified matrix.
10895
/// </summary>
@@ -129,12 +116,7 @@ public EllipsePolygon Transform(Matrix3x2 matrix) => matrix.IsIdentity
129116
/// <returns>This polygon as a path</returns>
130117
IPath IPath.AsClosedPath() => this;
131118

132-
/// <summary>
133-
/// Converts the <see cref="IPath" /> into a simple linear path.
134-
/// </summary>
135-
/// <returns>
136-
/// Returns the current <see cref="IPath" /> as simple linear path.
137-
/// </returns>
119+
/// <inheritdoc />
138120
public IEnumerable<ISimplePath> Flatten()
139121
{
140122
yield return this;

src/ImageSharp.Drawing/Shapes/IPathInternals.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,6 @@ int FindIntersections(
5353
Span<PointOrientation> orientations,
5454
IntersectionRule intersectionRule);
5555

56-
/// <summary>
57-
/// Calculates the distance along and away from the path for a specified point.
58-
/// </summary>
59-
/// <param name="point">The point along the path.</param>
60-
/// <returns>
61-
/// Returns details about the point and its distance away from the path.
62-
/// </returns>
63-
PointInfo Distance(PointF point);
64-
6556
/// <summary>
6657
/// Returns information about a point at a given distance along a path.
6758
/// </summary>

src/ImageSharp.Drawing/Shapes/Path.cs

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -88,23 +88,6 @@ public Path(params ILineSegment[] segments)
8888
private InternalPath InnerPath =>
8989
this.innerPath ??= new InternalPath(this.lineSegments, this.IsClosed, this.RemoveCloseAndCollinearPoints);
9090

91-
/// <inheritdoc />
92-
PointInfo IPathInternals.Distance(PointF point)
93-
{
94-
PointInfo dist = this.InnerPath.DistanceFromPath(point);
95-
96-
if (this.IsClosed)
97-
{
98-
bool isInside = this.InnerPath.PointInPolygon(point);
99-
if (isInside)
100-
{
101-
dist.DistanceFromPath *= -1;
102-
}
103-
}
104-
105-
return dist;
106-
}
107-
10891
/// <summary>
10992
/// Transforms the rectangle using specified matrix.
11093
/// </summary>
@@ -145,12 +128,7 @@ public IPath AsClosedPath()
145128
}
146129
}
147130

148-
/// <summary>
149-
/// Converts the <see cref="IPath" /> into a simple linear path..
150-
/// </summary>
151-
/// <returns>
152-
/// Returns the current <see cref="IPath" /> as simple linear path.
153-
/// </returns>
131+
/// <inheritdoc />
154132
public IEnumerable<ISimplePath> Flatten()
155133
{
156134
yield return this;

src/ImageSharp.Drawing/Shapes/RectangularPolygon.cs

Lines changed: 0 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -308,106 +308,6 @@ SegmentInfo IPathInternals.PointAlongPath(float distance)
308308
}
309309

310310
/// <inheritdoc/>
311-
PointInfo IPathInternals.Distance(PointF point)
312-
{
313-
Vector2 vectorPoint = point;
314-
315-
// Point in rectangle if after its clamped by the extremes its still the same then it must be inside :)
316-
Vector2 clamped = Vector2.Clamp(point, this.topLeft, this.bottomRight);
317-
bool isInside = clamped == vectorPoint;
318-
319-
float distanceFromEdge = float.MaxValue;
320-
float distanceAlongEdge = 0f;
321-
322-
if (isInside)
323-
{
324-
// get the absolute distances from the extreams
325-
Vector2 topLeftDist = Vector2.Abs(vectorPoint - this.topLeft);
326-
Vector2 bottomRightDist = Vector2.Abs(vectorPoint - this.bottomRight);
327-
328-
// get the min components
329-
Vector2 minDists = Vector2.Min(topLeftDist, bottomRightDist);
330-
331-
// and then the single smallest (dont have to worry about direction)
332-
distanceFromEdge = Math.Min(minDists.X, minDists.Y);
333-
334-
// we need to make clamped the closest point
335-
if (this.topLeft.X + distanceFromEdge == point.X)
336-
{
337-
// closer to lhf
338-
clamped.X = this.topLeft.X; // y is already the same
339-
340-
// distance along edge is length minus the amout down we are from the top of the rect
341-
distanceAlongEdge = this.length - (clamped.Y - this.topLeft.Y);
342-
}
343-
else if (this.topLeft.Y + distanceFromEdge == point.Y)
344-
{
345-
// closer to top
346-
clamped.Y = this.topLeft.Y; // x is already the same
347-
348-
distanceAlongEdge = clamped.X - this.topLeft.X;
349-
}
350-
else if (this.bottomRight.Y - distanceFromEdge == point.Y)
351-
{
352-
// closer to bottom
353-
clamped.Y = this.bottomRight.Y; // x is already the same
354-
355-
distanceAlongEdge = (this.bottomRight.X - clamped.X) + this.halfLength;
356-
}
357-
else if (this.bottomRight.X - distanceFromEdge == point.X)
358-
{
359-
// closer to rhs
360-
clamped.X = this.bottomRight.X; // x is already the same
361-
distanceAlongEdge = (clamped.Y - this.topLeft.Y) + this.Size.Width;
362-
}
363-
}
364-
else
365-
{
366-
// clamped is the point on the path thats closest no matter what
367-
distanceFromEdge = (clamped - vectorPoint).Length();
368-
369-
// we need to figure out whats the cloests edge now and thus what distance/poitn is closest
370-
if (this.topLeft.X == clamped.X)
371-
{
372-
// distance along edge is length minus the amout down we are from the top of the rect
373-
distanceAlongEdge = this.length - (clamped.Y - this.topLeft.Y);
374-
}
375-
else if (this.topLeft.Y == clamped.Y)
376-
{
377-
distanceAlongEdge = clamped.X - this.topLeft.X;
378-
}
379-
else if (this.bottomRight.Y == clamped.Y)
380-
{
381-
distanceAlongEdge = (this.bottomRight.X - clamped.X) + this.halfLength;
382-
}
383-
else if (this.bottomRight.X == clamped.X)
384-
{
385-
distanceAlongEdge = (clamped.Y - this.topLeft.Y) + this.Size.Width;
386-
}
387-
}
388-
389-
if (distanceAlongEdge == this.length)
390-
{
391-
distanceAlongEdge = 0;
392-
}
393-
394-
distanceFromEdge = isInside ? -distanceFromEdge : distanceFromEdge;
395-
396-
return new PointInfo
397-
{
398-
SearchPoint = point,
399-
DistanceFromPath = distanceFromEdge,
400-
ClosestPointOnPath = clamped,
401-
DistanceAlongPath = distanceAlongEdge
402-
};
403-
}
404-
405-
/// <summary>
406-
/// Converts the <see cref="IPath" /> into a simple linear path..
407-
/// </summary>
408-
/// <returns>
409-
/// Returns the current <see cref="IPath" /> as simple linear path.
410-
/// </returns>
411311
public IEnumerable<ISimplePath> Flatten()
412312
{
413313
yield return this;

tests/ImageSharp.Drawing.Tests/Shapes/PathTests.cs

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -25,43 +25,6 @@ public void Bounds()
2525
Assert.Equal(5, path.Bounds.Bottom);
2626
}
2727

28-
public static TheoryData<TestPoint, float, float> PathDistanceTheoryData =
29-
new TheoryData<TestPoint, float, float>
30-
{
31-
{ new PointF(0, 0), 0f, 0f },
32-
{ new PointF(1, 0), 0f, 1f },
33-
{ new PointF(9, 0), 0f, 9f },
34-
{ new PointF(10, 0), 0f, 10f },
35-
{ new PointF(10, 1), 0f, 11f },
36-
{ new PointF(10, 9), 0f, 19f },
37-
{ new PointF(10, 10), 0f, 20f },
38-
{ new PointF(9, 10), 0f, 21f },
39-
{ new PointF(1, 10), 0f, 29f },
40-
{ new PointF(0, 10), 0f, 30f },
41-
{ new PointF(0, 1), 1f, 0f },
42-
{ new PointF(4, 3), 3f, 4f },
43-
{ new PointF(3, 4), 4f, 3f },
44-
{ new PointF(-1, 0), 1f, 0f },
45-
{ new PointF(1, -1), 1f, 1f },
46-
{ new PointF(9, -1), 1f, 9f },
47-
{ new PointF(11, 0), 1f, 10f },
48-
{ new PointF(11, 1), 1f, 11f },
49-
{ new PointF(11, 9), 1f, 19f },
50-
{ new PointF(11, 10), 1f, 20f },
51-
{ new PointF(9, 11), 1f, 21f },
52-
{ new PointF(1, 11), 1f, 29f }
53-
};
54-
55-
[Theory]
56-
[MemberData(nameof(PathDistanceTheoryData))]
57-
public void DistanceFromPath_Path(TestPoint point, float expectedDistance, float alongPath)
58-
{
59-
IPathInternals path = new Path(new LinearLineSegment(new PointF(0, 0), new PointF(10, 0), new PointF(10, 10), new PointF(0, 10)));
60-
PointInfo info = path.Distance(point);
61-
Assert.Equal(expectedDistance, info.DistanceFromPath);
62-
Assert.Equal(alongPath, info.DistanceAlongPath);
63-
}
64-
6528
[Fact]
6629
public void SimplePath()
6730
{

tests/ImageSharp.Drawing.Tests/Shapes/PolygonTests.cs

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -65,51 +65,6 @@ public void PointInPolygon(TestPoint[] controlPoints, TestPoint point, bool isIn
6565
},
6666
};
6767

68-
[Theory]
69-
[MemberData(nameof(DistanceTheoryData))]
70-
public void Distance(TestPoint[] controlPoints, TestPoint point, float expected)
71-
{
72-
IPathInternals shape = new Polygon(new LinearLineSegment(controlPoints.Select(x => (PointF)x).ToArray()));
73-
Assert.Equal(expected, shape.Distance(point).DistanceFromPath);
74-
}
75-
76-
public static TheoryData<TestPoint, float, float> PathDistanceTheoryData =
77-
new TheoryData<TestPoint, float, float>
78-
{
79-
{ new PointF(0, 0), 0f, 0f },
80-
{ new PointF(1, 0), 0f, 1f },
81-
{ new PointF(9, 0), 0f, 9f },
82-
{ new PointF(10, 0), 0f, 10f },
83-
{ new PointF(10, 1), 0f, 11f },
84-
{ new PointF(10, 9), 0f, 19f },
85-
{ new PointF(10, 10), 0f, 20f },
86-
{ new PointF(9, 10), 0f, 21f },
87-
{ new PointF(1, 10), 0f, 29f },
88-
{ new PointF(0, 10), 0f, 30f },
89-
{ new PointF(0, 1), 0f, 39f },
90-
{ new PointF(4, 3), -3f, 4f },
91-
{ new PointF(3, 4), -3f, 36f },
92-
{ new PointF(-1, 0), 1f, 0f },
93-
{ new PointF(1, -1), 1f, 1f },
94-
{ new PointF(9, -1), 1f, 9f },
95-
{ new PointF(11, 0), 1f, 10f },
96-
{ new PointF(11, 1), 1f, 11f },
97-
{ new PointF(11, 9), 1f, 19f },
98-
{ new PointF(11, 10), 1f, 20f },
99-
{ new PointF(9, 11), 1f, 21f },
100-
{ new PointF(1, 11), 1f, 29f }
101-
};
102-
103-
[Theory]
104-
[MemberData(nameof(PathDistanceTheoryData))]
105-
public void DistanceFromPath_Path(TestPoint point, float expectedDistance, float alongPath)
106-
{
107-
IPathInternals path = new Polygon(new LinearLineSegment(new PointF(0, 0), new PointF(10, 0), new PointF(10, 10), new PointF(0, 10)));
108-
PointInfo info = path.Distance(point);
109-
Assert.Equal(expectedDistance, info.DistanceFromPath);
110-
Assert.Equal(alongPath, info.DistanceAlongPath);
111-
}
112-
11368
[Fact]
11469
public void AsSimpleLinearPath()
11570
{

0 commit comments

Comments
 (0)