Skip to content

Commit

Permalink
Partial fix for issue #1
Browse files Browse the repository at this point in the history
* Implemented (part of) the fix as @obi068 suggested

* Fixed bugs introduce in commit 422b577; Initial initial PR#13 created by @BartWaardenburg should be re-reviewed such that it can be merged in Leaflet.PolylineOffset code
  • Loading branch information
BartWaardenburg authored and bbecquet committed Jun 28, 2017
1 parent dd3c5dd commit 2a38674
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions leaflet.polylineoffset.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,34 @@ L.PolylineOffset = {
throw new Error('Line should be defined by at least 2 points');
}

var a = points[0], b;
var a = points[0], b, xs ,ys, dist;
var offsetAngle, segmentAngle;
var offsetSegments = [];

for(var i=1; i < l; i++) {
b = points[i];
xs = b.x - a.x;
ys = b.y - a.y;
dist = Math.sqrt(xs * xs + ys * ys);
// angle in (-PI, PI]
segmentAngle = Math.atan2(a.y - b.y, a.x - b.x);
// angle in (-1.5 * PI, PI/2]
offsetAngle = segmentAngle - Math.PI/2;

// store offset point and other information to avoid recomputing it later
offsetSegments.push({
angle: segmentAngle,
offsetAngle: offsetAngle,
distance: distance,
original: [a, b],
offset: [
this.translatePoint(a, distance, offsetAngle),
this.translatePoint(b, distance, offsetAngle)
]
});
a = b;
if (dist > distance) {
offsetSegments.push({
angle: segmentAngle,
offsetAngle: offsetAngle,
distance: distance,
original: [a, b],
offset: [
this.translatePoint(a, distance, offsetAngle),
this.translatePoint(b, distance, offsetAngle)
]
});
a = b;
}
}

return offsetSegments;
Expand Down Expand Up @@ -152,14 +157,16 @@ L.PolylineOffset = {
var l = segments.length;
var joinedPoints = [];
var s1 = segments[0], s2 = segments[0];
joinedPoints.push(s1.offset[0]);
if (s1 && s2) {
joinedPoints.push(s1.offset[0]);

for(var i=1; i<l; i++) {
s2 = segments[i];
joinedPoints = joinedPoints.concat(this.joinSegments(s1, s2, offset, joinStyle));
s1 = s2;
for(var i=1; i<l; i++) {
s2 = segments[i];
joinedPoints = joinedPoints.concat(this.joinSegments(s1, s2, offset, joinStyle));
s1 = s2;
}
joinedPoints.push(s2.offset[1]);
}
joinedPoints.push(s2.offset[1]);

return joinedPoints;
},
Expand Down

0 comments on commit 2a38674

Please sign in to comment.