Skip to content

Commit

Permalink
fix overlapping polygons case
Browse files Browse the repository at this point in the history
  • Loading branch information
w8r committed Oct 14, 2014
1 parent f0b6583 commit 3467c89
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
8 changes: 6 additions & 2 deletions src/driver.leaflet.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,14 @@ function clip(polygonA, polygonB, sourceForwards, clipForwards) {
}

function toLatLngs(poly) {
var result = poly.getPoints()
.slice(0, poly.vertices - 1);
var result = poly.getPoints();

if (result) {
if (result[0][0] === result[result.length - 1][0] &&
result[0][1] === result[result.length - 1][1]) {
result = result.slice(0, result.length - 1);
}

for (var i = 0, len = result.length; i < len; i++) {
result[i] = [result[i][1], result[i][0]];
}
Expand Down
7 changes: 7 additions & 0 deletions src/intersection.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,14 @@ var Intersection = function(s1, s2, c1, c2) {
return;
}

/**
* @type {Number}
*/
this.toSource = ((c2.x - c1.x) * (s1.y - c1.y) - (c2.y - c1.y) * (s1.x - c1.x)) / d;

/**
* @type {Number}
*/
this.toClip = ((s2.x - s1.x) * (s1.y - c1.y) - (s2.y - s1.y) * (s1.x - c1.x)) / d;

if (this.valid()) {
Expand Down
43 changes: 35 additions & 8 deletions src/polygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ var Polygon = function(p) {
*/
this.vertices = 0;

/**
* @type {Vertex}
*/
this._lastUnprocessed = null;

for (var i = 0, len = p.length; i < len; i++) {
this.addVertex(new Vertex(p[i]));
}
Expand Down Expand Up @@ -84,14 +89,17 @@ Polygon.prototype.getNext = function(v) {
* @return {Vertex}
*/
Polygon.prototype.getFirstIntersect = function() {
var v = this.first;
var v = this._firstIntersect || this.first;

do {
if (v._isIntersection && !v._visited) {
break;
}

v = v.next;
} while (!v.equals(this.first));

this._firstIntersect = v;
return v;
};

Expand All @@ -100,14 +108,17 @@ Polygon.prototype.getFirstIntersect = function() {
* @return {Boolean} [description]
*/
Polygon.prototype.hasUnprocessed = function() {
var v = this.first;
var v = this._lastUnprocessed || this.first;
do {
if (v._isIntersection && !v._visited) {
this._lastUnprocessed = v;
return true;
}

v = v.next;
} while (!v.equals(this.first));

this._lastUnprocessed = null;
return false;
};

Expand All @@ -118,10 +129,11 @@ Polygon.prototype.getPoints = function() {
var points = [],
v = this.first;

for (var i = 0; i < this.vertices; i++) {
points[i] = [v.x, v.y];
do {
points.push([v.x, v.y]);
v = v.next;
}
} while (v !== this.first);

return points;
};

Expand All @@ -139,8 +151,10 @@ Polygon.prototype.getPoints = function() {
*/
Polygon.prototype.clip = function(clip, sourceForwards, clipForwards) {
var sourceVertex = this.first,
clipVertex = clip.first;
clipVertex = clip.first,
sourceInClip, clipInSource;

// calculate and mark intersections
do {
if (!sourceVertex._isIntersection) {
do {
Expand Down Expand Up @@ -180,8 +194,12 @@ Polygon.prototype.clip = function(clip, sourceForwards, clipForwards) {
sourceVertex = this.first;
clipVertex = clip.first;

sourceForwards ^= sourceVertex.isInside(clip);
clipForwards ^= clipVertex.isInside(this);
sourceInClip = sourceVertex.isInside(clip);
clipInSource = clipVertex.isInside(this);

sourceForwards ^= sourceInClip;
clipForwards ^= clipInSource;

do {
if (sourceVertex._isIntersection) {
sourceVertex._isEntry = sourceForwards;
Expand Down Expand Up @@ -226,5 +244,14 @@ Polygon.prototype.clip = function(clip, sourceForwards, clipForwards) {
list.push(clipped);
}

if (list.length === 0) {
if (sourceInClip) {
list.push(this);
}
if (clipInSource) {
list.push(this);
}
}

return list;
};

0 comments on commit 3467c89

Please sign in to comment.