File tree 1 file changed +21
-10
lines changed 1 file changed +21
-10
lines changed Original file line number Diff line number Diff line change @@ -173,20 +173,31 @@ class PolyTools
173
173
174
174
/**
175
175
* 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
176
180
*/
177
- static public function findDuplicatePoints (poly : Poly ): Array <Int >
181
+ static public function findDuplicatePoints (poly : Poly , consecutiveOnly : Bool = true , wrapAround : Bool = true ): Array <Int >
178
182
{
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 ++ ;
186
196
}
187
197
}
188
-
189
- return res ;
198
+ if (wrapAround && consecutiveOnly && poly [0 ].equals (poly [len - 1 ])) dupIndices .push (len - 1 );
199
+
200
+ return dupIndices ;
190
201
}
191
202
192
203
/** Finds the intersection point between lines extending the segments `p1`-`p2` and `q1`-`q2`. Returns null if they're parallel. */
You can’t perform that action at this time.
0 commit comments