Skip to content

Commit

Permalink
Merge pull request #240 from remcoros/reduce_loops
Browse files Browse the repository at this point in the history
Determine minX/minY/maxX/maxY by iterating over points/paths only onc…
  • Loading branch information
JimBobSquarePants authored Jul 23, 2022
2 parents 4149a1b + 0bc8b36 commit dc838dc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
19 changes: 14 additions & 5 deletions src/ImageSharp.Drawing/Shapes/InternalPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,22 @@ private InternalPath(PointData[] points, bool isClosedPath)

if (this.points.Length > 0)
{
float minX = this.points.Min(x => x.Point.X);
float maxX = this.points.Max(x => x.Point.X);
float minY = this.points.Min(x => x.Point.Y);
float maxY = this.points.Max(x => x.Point.Y);
float minX, minY, maxX, maxY, length;
length = 0;
minX = minY = float.MaxValue;
maxX = maxY = float.MinValue;

foreach (var point in this.points)
{
length += point.Length;
minX = Math.Min(point.Point.X, minX);
minY = Math.Min(point.Point.Y, minY);
maxX = Math.Max(point.Point.X, maxX);
maxY = Math.Max(point.Point.Y, maxY);
}

this.Bounds = new RectangleF(minX, minY, maxX - minX, maxY - minY);
this.Length = this.points.Sum(x => x.Length);
this.Length = length;
}
else
{
Expand Down
14 changes: 10 additions & 4 deletions src/ImageSharp.Drawing/Shapes/PathCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,17 @@ public PathCollection(params IPath[] paths)
}
else
{
float minX = this.paths.Min(x => x.Bounds.Left);
float maxX = this.paths.Max(x => x.Bounds.Right);
float minX, minY, maxX, maxY;
minX = minY = float.MaxValue;
maxX = maxY = float.MinValue;

float minY = this.paths.Min(x => x.Bounds.Top);
float maxY = this.paths.Max(x => x.Bounds.Bottom);
foreach (var path in this.paths)
{
minX = Math.Min(path.Bounds.Left, minX);
minY = Math.Min(path.Bounds.Top, minY);
maxX = Math.Max(path.Bounds.Right, maxX);
maxY = Math.Max(path.Bounds.Bottom, maxY);
}

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

0 comments on commit dc838dc

Please sign in to comment.