From 47490a82a1bf3cc09ba66bc7526f2d17e9ad8dff Mon Sep 17 00:00:00 2001 From: Michael Oborne Date: Sat, 18 Feb 2023 17:05:29 +1100 Subject: [PATCH] Grid: fix spiral start location --- ExtLibs/Utilities/Grid.cs | 45 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/ExtLibs/Utilities/Grid.cs b/ExtLibs/Utilities/Grid.cs index 8f71bcf441..cd59a42173 100644 --- a/ExtLibs/Utilities/Grid.cs +++ b/ExtLibs/Utilities/Grid.cs @@ -211,12 +211,40 @@ public static List CreateRotary(List polygon, do // utm position list List utmpositions = utmpos.ToList(PointLatLngAlt.ToUTM(utmzone, polygon), utmzone); - // close the loop if its not already - if (utmpositions[0] != utmpositions[utmpositions.Count - 1]) - utmpositions.Add(utmpositions[0]); // make a full loop + // get mins/maxs of coverage area + Rect area = getPolyMinMax(utmpositions); var maxlane = laps;// (Centroid(utmpositions).GetDistance(utmpositions[0]) / distance); + // pick start positon based on initial point rectangle + utmpos startposutm; + + switch (startpos) + { + default: + case StartPosition.Home: + startposutm = new utmpos(HomeLocation); + break; + case StartPosition.BottomLeft: + startposutm = new utmpos(area.Left, area.Bottom, utmzone); + break; + case StartPosition.BottomRight: + startposutm = new utmpos(area.Right, area.Bottom, utmzone); + break; + case StartPosition.TopLeft: + startposutm = new utmpos(area.Left, area.Top, utmzone); + break; + case StartPosition.TopRight: + startposutm = new utmpos(area.Right, area.Top, utmzone); + break; + case StartPosition.Point: + startposutm = new utmpos(StartPointLatLngAlt); + break; + } + + // find the closes polygon point based from our startpos selection + startposutm = findClosestPoint(startposutm, utmpositions); + ClipperLib.ClipperOffset clipperOffset = new ClipperLib.ClipperOffset(); clipperOffset.AddPath(utmpositions.Select(a => { return new ClipperLib.IntPoint(a.x * 1000.0, a.y * 1000.0); }).ToList(), ClipperLib.JoinType.jtMiter, ClipperLib.EndType.etClosedPolygon); @@ -240,7 +268,18 @@ public static List CreateRotary(List polygon, do { ans1 = treeChild.Contour.Select(a => new utmpos(a.X / 1000.0, a.Y / 1000.0, utmzone)) .ToList(); + // rotate points so the start point is close to the previous + { + startposutm = findClosestPoint(startposutm, ans1); + + var startidx = ans1.IndexOf(startposutm); + var firsthalf = ans1.GetRange(startidx, ans1.Count - startidx); + var secondhalf = ans1.GetRange(0, startidx); + + ans1 = firsthalf; + ans1.AddRange(secondhalf); + } if (lane == 0 && clockwise_laps != 1 && match_spiral_perimeter) { ans1.Insert(0, ans1.Last()); // start at the last point of the first calculated lap