Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add angle and destinationOnSegment #71

Merged
merged 1 commit into from
Jan 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions spec/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<script src="test.reverse.js"></script>
<script src="test.rotatePoint.js"></script>
<script src="test.startsAtExtremity.js"></script>
<script src="test.angle.js"></script>
<script>
(window.mochaPhantomJS || window.mocha).run();
</script>
Expand Down
26 changes: 26 additions & 0 deletions spec/test.angle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
describe('Angle', function() {

it('It should be 45 degrees', function(done) {
var latlng1 = L.latLng([0.0, 0.0]),
latlng2 = L.latLng([1.0, 1.0]),
result = 45.0;
assert.equal(result, L.GeometryUtil.angle(map, latlng1, latlng2));
done();
});

it('It should be degrees clockwise from east, 0 degrees', function(done) {
var latlng1 = L.latLng([0.0, 0.0]),
latlng2 = L.latLng([1.0, 0.0]),
result = 0.0;
assert.equal(result, L.GeometryUtil.angle(map, latlng1, latlng2));
done();
});

it('It should not be a negative value when the angle is greater than 180', function(done) {
var latlng1 = L.latLng([0.0, 0.0]),
latlng2 = L.latLng([-1.0, 1.0]),
result = -135.0;
assert.notEqual(result, L.GeometryUtil.angle(map, latlng1, latlng2));
done();
});
});
2 changes: 1 addition & 1 deletion spec/test.destination.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ describe('Destination', function() {
assert.latLngEqual(result, L.GeometryUtil.destination(latlng1, heading, dist));
done();
});
});
});
36 changes: 33 additions & 3 deletions src/leaflet.geometryutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -703,8 +703,8 @@ L.GeometryUtil = L.extend(L.GeometryUtil || {}, {
Returns the point that is a distance and heading away from
the given origin point.
@param {L.LatLng} latlng: origin point
@param {float}: heading in degrees, clockwise from 0 degrees north.
@param {float}: distance in meters
@param {float} heading: heading in degrees, clockwise from 0 degrees north.
@param {float} distance: distance in meters
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@returns {L.latLng} the destination point.
Many thanks to Chris Veness at http://www.movable-type.co.uk/scripts/latlong.html
for a great reference and examples.
Expand All @@ -728,7 +728,37 @@ L.GeometryUtil = L.extend(L.GeometryUtil || {}, {
lon2 = lon2 * radInv;
lon2 = lon2 > 180 ? lon2 - 360 : lon2 < -180 ? lon2 + 360 : lon2;
return L.latLng([lat2 * radInv, lon2]);
}
},

/**
Returns the the angle of the given segment and the Equator in degrees,
clockwise from 0 degrees north.
@param {L.Map} map: Leaflet map to be used for this method
@param {L.LatLng} latlngA: geographical point A of the segment
@param {L.LatLng} latlngB: geographical point B of the segment
@returns {Float} the angle in degrees.
*/
angle: function(map, latlngA, latlngB) {
var pointA = map.latLngToContainerPoint(latlngA),
pointB = map.latLngToContainerPoint(latlngB),
angleDeg = Math.atan2(pointB.y - pointA.y, pointB.x - pointA.x) * 180 / Math.PI + 90;
angleDeg += angleDeg < 0 ? 360 : 0;
return angleDeg;
trandaison marked this conversation as resolved.
Show resolved Hide resolved
},

/**
Returns a point snaps on the segment and heading away from the given origin point a distance.
@param {L.Map} map: Leaflet map to be used for this method
@param {L.LatLng} latlngA: geographical point A of the segment
@param {L.LatLng} latlngB: geographical point B of the segment
@param {float} distance: distance in meters
@returns {L.latLng} the destination point.
*/
destinationOnSegment: function(map, latlngA, latlngB, distance) {
var angleDeg = L.GeometryUtil.angle(map, latlngA, latlngB),
latlng = L.GeometryUtil.destination(latlngA, angleDeg, distance);
return L.GeometryUtil.closestOnSegment(map, latlng, latlngA, latlngB);
},
});

return L.GeometryUtil;
Expand Down