Skip to content

Commit 1cd0234

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

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

src/hxGeomAlgo/PolyTools.hx

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -173,20 +173,31 @@ 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) {
192+
dupIndices.push(i);
193+
}
194+
if (consecutiveOnly || (foundDup && !consecutiveOnly)) break;
195+
j++;
186196
}
187197
}
188-
189-
return res;
198+
if (wrapAround && consecutiveOnly && poly[0].equals(poly[len - 1])) dupIndices.push(len - 1);
199+
200+
return dupIndices;
190201
}
191202

192203
/** 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)