Skip to content

Commit dc838dc

Browse files
Merge pull request #240 from remcoros/reduce_loops
Determine minX/minY/maxX/maxY by iterating over points/paths only onc…
2 parents 4149a1b + 0bc8b36 commit dc838dc

2 files changed

Lines changed: 24 additions & 9 deletions

File tree

src/ImageSharp.Drawing/Shapes/InternalPath.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,22 @@ private InternalPath(PointData[] points, bool isClosedPath)
8080

8181
if (this.points.Length > 0)
8282
{
83-
float minX = this.points.Min(x => x.Point.X);
84-
float maxX = this.points.Max(x => x.Point.X);
85-
float minY = this.points.Min(x => x.Point.Y);
86-
float maxY = this.points.Max(x => x.Point.Y);
83+
float minX, minY, maxX, maxY, length;
84+
length = 0;
85+
minX = minY = float.MaxValue;
86+
maxX = maxY = float.MinValue;
87+
88+
foreach (var point in this.points)
89+
{
90+
length += point.Length;
91+
minX = Math.Min(point.Point.X, minX);
92+
minY = Math.Min(point.Point.Y, minY);
93+
maxX = Math.Max(point.Point.X, maxX);
94+
maxY = Math.Max(point.Point.Y, maxY);
95+
}
8796

8897
this.Bounds = new RectangleF(minX, minY, maxX - minX, maxY - minY);
89-
this.Length = this.points.Sum(x => x.Length);
98+
this.Length = length;
9099
}
91100
else
92101
{

src/ImageSharp.Drawing/Shapes/PathCollection.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,17 @@ public PathCollection(params IPath[] paths)
4040
}
4141
else
4242
{
43-
float minX = this.paths.Min(x => x.Bounds.Left);
44-
float maxX = this.paths.Max(x => x.Bounds.Right);
43+
float minX, minY, maxX, maxY;
44+
minX = minY = float.MaxValue;
45+
maxX = maxY = float.MinValue;
4546

46-
float minY = this.paths.Min(x => x.Bounds.Top);
47-
float maxY = this.paths.Max(x => x.Bounds.Bottom);
47+
foreach (var path in this.paths)
48+
{
49+
minX = Math.Min(path.Bounds.Left, minX);
50+
minY = Math.Min(path.Bounds.Top, minY);
51+
maxX = Math.Max(path.Bounds.Right, maxX);
52+
maxY = Math.Max(path.Bounds.Bottom, maxY);
53+
}
4854

4955
this.Bounds = new RectangleF(minX, minY, maxX - minX, maxY - minY);
5056
}

0 commit comments

Comments
 (0)