diff --git a/OpenNest.Engine/Fill/FillLinear.cs b/OpenNest.Engine/Fill/FillLinear.cs
index 674bff3..4a09190 100644
--- a/OpenNest.Engine/Fill/FillLinear.cs
+++ b/OpenNest.Engine/Fill/FillLinear.cs
@@ -110,47 +110,40 @@ namespace OpenNest.Engine.Fill
var pushDir = GetPushDirection(direction);
var opposite = SpatialQuery.OppositeDirection(pushDir);
- // Compute a starting offset large enough that every part-pair in
- // patternB has its offset geometry beyond patternA's offset geometry.
- var maxUpper = double.MinValue;
- var minLower = double.MaxValue;
-
- for (var i = 0; i < patternA.Parts.Count; i++)
- {
- var bb = patternA.Parts[i].BoundingBox;
- var upper = direction == NestDirection.Horizontal ? bb.Right : bb.Top;
- var lower = direction == NestDirection.Horizontal ? bb.Left : bb.Bottom;
-
- if (upper > maxUpper) maxUpper = upper;
- if (lower < minLower) minLower = lower;
- }
-
- var startOffset = System.Math.Max(bboxDim,
- maxUpper - minLower + PartSpacing + Tolerance.Epsilon);
-
+ // bboxDim already spans max(upper) - min(lower) across all parts,
+ // so the start offset just needs to push beyond that plus spacing.
+ var startOffset = bboxDim + PartSpacing + Tolerance.Epsilon;
var offset = MakeOffset(direction, startOffset);
- // Pre-cache edge arrays.
- var movingEdges = new (Vector start, Vector end)[patternA.Parts.Count][];
- var stationaryEdges = new (Vector start, Vector end)[patternA.Parts.Count][];
+ var maxCopyDistance = FindMaxPairDistance(
+ patternA.Parts, boundaries, offset, pushDir, opposite, startOffset);
- for (var i = 0; i < patternA.Parts.Count; i++)
- {
- movingEdges[i] = boundaries[i].GetEdges(pushDir);
- stationaryEdges[i] = boundaries[i].GetEdges(opposite);
- }
+ if (maxCopyDistance < Tolerance.Epsilon)
+ return bboxDim + PartSpacing;
+ return maxCopyDistance;
+ }
+
+ ///
+ /// Tests every pair of parts across adjacent pattern copies and returns the
+ /// maximum copy distance found. Returns 0 if no valid slide was found.
+ ///
+ private static double FindMaxPairDistance(
+ List parts, PartBoundary[] boundaries, Vector offset,
+ PushDirection pushDir, PushDirection opposite, double startOffset)
+ {
var maxCopyDistance = 0.0;
- for (var j = 0; j < patternA.Parts.Count; j++)
+ for (var j = 0; j < parts.Count; j++)
{
- var locationB = patternA.Parts[j].Location + offset;
+ var movingEdges = boundaries[j].GetEdges(pushDir);
+ var locationB = parts[j].Location + offset;
- for (var i = 0; i < patternA.Parts.Count; i++)
+ for (var i = 0; i < parts.Count; i++)
{
var slideDistance = SpatialQuery.DirectionalDistance(
- movingEdges[j], locationB,
- stationaryEdges[i], patternA.Parts[i].Location,
+ movingEdges, locationB,
+ boundaries[i].GetEdges(opposite), parts[i].Location,
pushDir);
if (slideDistance >= double.MaxValue || slideDistance < 0)
@@ -163,9 +156,6 @@ namespace OpenNest.Engine.Fill
}
}
- if (maxCopyDistance < Tolerance.Epsilon)
- return bboxDim + PartSpacing;
-
return maxCopyDistance;
}