Connect closest node instead of first node below threshold in polygons.

This commit is contained in:
Luc Maisonobe 2014-12-03 10:25:44 +01:00
parent 428a48533c
commit b4fb13bca9
1 changed files with 15 additions and 8 deletions

View File

@ -840,18 +840,25 @@ public class PolygonsSet extends AbstractRegion<Euclidean2D, Euclidean1D> {
private int closeVerticesConnections(final List<ConnectableSegment> segments) { private int closeVerticesConnections(final List<ConnectableSegment> segments) {
int connected = 0; int connected = 0;
for (final ConnectableSegment segment : segments) { for (final ConnectableSegment segment : segments) {
if (segment.getNext() == null) { if (segment.getNext() == null && segment.getEnd() != null) {
final Vector2D end = segment.getEnd(); final Vector2D end = segment.getEnd();
ConnectableSegment selectedNext = null;
double min = Double.POSITIVE_INFINITY;
for (final ConnectableSegment candidateNext : segments) { for (final ConnectableSegment candidateNext : segments) {
if (candidateNext.getPrevious() == null && if (candidateNext.getPrevious() == null && candidateNext.getStart() != null) {
Vector2D.distance(end, candidateNext.getStart()) <= getTolerance()) { final double distance = Vector2D.distance(end, candidateNext.getStart());
// connect the two segments if (distance < min) {
segment.setNext(candidateNext); selectedNext = candidateNext;
candidateNext.setPrevious(segment); min = distance;
++connected; }
break;
} }
} }
if (min <= getTolerance()) {
// connect the two segments
segment.setNext(selectedNext);
selectedNext.setPrevious(segment);
++connected;
}
} }
} }
return connected; return connected;