From 0bc8b36c9a4c5704da99176c8cd89c602e225717 Mon Sep 17 00:00:00 2001 From: Remco Ros Date: Fri, 22 Jul 2022 17:48:13 +0200 Subject: [PATCH] Determine minX/minY/maxX/maxY by iterating over points/paths only once instead of using Linq --- src/ImageSharp.Drawing/Shapes/InternalPath.cs | 19 ++++++++++++++----- .../Shapes/PathCollection.cs | 14 ++++++++++---- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/ImageSharp.Drawing/Shapes/InternalPath.cs b/src/ImageSharp.Drawing/Shapes/InternalPath.cs index 03b85e17..11da1f44 100644 --- a/src/ImageSharp.Drawing/Shapes/InternalPath.cs +++ b/src/ImageSharp.Drawing/Shapes/InternalPath.cs @@ -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 { diff --git a/src/ImageSharp.Drawing/Shapes/PathCollection.cs b/src/ImageSharp.Drawing/Shapes/PathCollection.cs index 7ddc6c9a..cb5121b9 100644 --- a/src/ImageSharp.Drawing/Shapes/PathCollection.cs +++ b/src/ImageSharp.Drawing/Shapes/PathCollection.cs @@ -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); }