Skip to content

Commit

Permalink
Refactored deeply nested loop (#324)
Browse files Browse the repository at this point in the history
* Refactored deeply nested loop

* Fixed formatting

---------

Co-authored-by: Lehonti Ramos <john@doe>
  • Loading branch information
Lehonti and Lehonti Ramos authored Aug 10, 2023
1 parent 4870371 commit ac9d70c
Showing 1 changed file with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions Pinta.Tools/Editable/Shapes/OrganizedPointCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

namespace Pinta.Tools
{
public class OrganizedPointCollection
public sealed class OrganizedPointCollection
{
//Must be an integer.
public const double SectionSize = 15;
Expand Down Expand Up @@ -151,35 +151,38 @@ public static void FindClosestPoint (
Dictionary<int, List<OrganizedPoint>>? 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<OrganizedPoint>? 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<OrganizedPoint>? 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;
Expand Down

0 comments on commit ac9d70c

Please sign in to comment.