Skip to content

Commit 3c44bb8

Browse files
committed
Fix and improve PolyTools.findDuplicatePoints()
1 parent fb78e4f commit 3c44bb8

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

src/hxGeomAlgo/PolyTools.hx

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -173,20 +173,29 @@ class PolyTools
173173

174174
/**
175175
* Returns indices of duplicate points in `poly` (or an empty array if none are found).
176+
* NOTE: indices in the result are guaranteed to be in ascending order.
177+
*
178+
* @param consecutiveOnly if true only equal adjacent points are reported
179+
* @param wrapAround if true also first vs last point will be checked
176180
*/
177-
static public function findDuplicatePoints(poly:Poly):Array<Int>
181+
static public function findDuplicatePoints(poly:Poly, consecutiveOnly:Bool = true, wrapAround:Bool = true):Array<Int>
178182
{
179-
var len:Int = poly.length;
180-
if (len <= 1) return null;
181-
var res = new Array<Int>();
182-
183-
for (i in 0...len) {
184-
for (j in i + 1...len) {
185-
if (poly[i].equals(poly[j])) res.push(j);
183+
var len = poly.length;
184+
if (len <= 1) return [];
185+
var dupIndices = [];
186+
187+
for (i in 0...len - 1) {
188+
var j = i + 1;
189+
while (j < len) {
190+
var foundDup = poly[i].equals(poly[j]);
191+
if (foundDup) dupIndices.push(i);
192+
if (consecutiveOnly || (foundDup && !consecutiveOnly)) break;
193+
j++;
186194
}
187195
}
188-
189-
return res;
196+
if (wrapAround && consecutiveOnly && poly[0].equals(poly[len - 1])) dupIndices.push(len - 1);
197+
198+
return dupIndices;
190199
}
191200

192201
/** Finds the intersection point between lines extending the segments `p1`-`p2` and `q1`-`q2`. Returns null if they're parallel. */

0 commit comments

Comments
 (0)