From ac9d70c80586f561228a3ff8d44a38e0c2f3242b Mon Sep 17 00:00:00 2001 From: Lehonti Ramos <17771375+Lehonti@users.noreply.github.com> Date: Thu, 10 Aug 2023 06:05:20 +0200 Subject: [PATCH] Refactored deeply nested loop (#324) * Refactored deeply nested loop * Fixed formatting --------- Co-authored-by: Lehonti Ramos --- .../Shapes/OrganizedPointCollection.cs | 53 ++++++++++--------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/Pinta.Tools/Editable/Shapes/OrganizedPointCollection.cs b/Pinta.Tools/Editable/Shapes/OrganizedPointCollection.cs index 08468c1d3..e26cf3492 100644 --- a/Pinta.Tools/Editable/Shapes/OrganizedPointCollection.cs +++ b/Pinta.Tools/Editable/Shapes/OrganizedPointCollection.cs @@ -31,7 +31,7 @@ namespace Pinta.Tools { - public class OrganizedPointCollection + public sealed class OrganizedPointCollection { //Must be an integer. public const double SectionSize = 15; @@ -151,35 +151,38 @@ public static void FindClosestPoint ( Dictionary>? xSection; //If the xSection doesn't exist, move on. - if (oP.TryGetValue (x, out xSection)) { - //Since the mouse and/or shape points can be close to the edge of a section, - //the points in the surrounding sections must also be checked. - for (int y = yMin; y <= yMax; ++y) { - List? ySection; - - //If the ySection doesn't exist, move on. - if (xSection.TryGetValue (y, out ySection)) { - foreach (OrganizedPoint p in ySection) { - currentDistance = p.Position.Distance (currentPoint); - - if (currentDistance < closestDistance) { - closestDistance = currentDistance; - - closestPointIndex = p.Index; - closestShapeIndex = n; - - closestPoint = p.Position; - } - } - } - } //for each organized row - } + if (!oP.TryGetValue (x, out xSection)) + continue; + + //Since the mouse and/or shape points can be close to the edge of a section, + //the points in the surrounding sections must also be checked. + for (int y = yMin; y <= yMax; ++y) { + List? ySection; + + //If the ySection doesn't exist, move on. + if (!xSection.TryGetValue (y, out ySection)) + continue; + + foreach (OrganizedPoint p in ySection) { + currentDistance = p.Position.Distance (currentPoint); + + if (currentDistance >= closestDistance) + continue; + + closestDistance = currentDistance; + + closestPointIndex = p.Index; + closestShapeIndex = n; + + closestPoint = p.Position; + } + } //for each organized row } //for each organized column } //for each ShapeEngine List } //FindClosestPoint } - public class OrganizedPoint + public sealed class OrganizedPoint { //Note: not using get/set because this is used in time-critical code that is sped up without it. public PointD Position;