-
-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add last drawn circle radius and polygon to output (#59)
Co-authored-by: Zachary Blackwood <[email protected]>
- Loading branch information
1 parent
1a5912c
commit ce297c7
Showing
3 changed files
with
72 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const earthRadius = 6378137; // equatorial Earth radius in m | ||
|
||
function toRadians(angleInDegrees: number) { | ||
return (angleInDegrees * Math.PI) / 180; | ||
} | ||
|
||
function toDegrees(angleInRadians: number) { | ||
return (angleInRadians * 180) / Math.PI; | ||
} | ||
|
||
function offset( | ||
c1: [number, number], | ||
distance: number, | ||
earthRadius: number, | ||
bearing: number): [number, number] { | ||
var lat1 = toRadians(c1[1]); | ||
var lon1 = toRadians(c1[0]); | ||
var dByR = distance / earthRadius; | ||
var lat = Math.asin( | ||
Math.sin(lat1) * Math.cos(dByR) + Math.cos(lat1) * Math.sin(dByR) * Math.cos(bearing) | ||
); | ||
var lon = | ||
lon1 + | ||
Math.atan2( | ||
Math.sin(bearing) * Math.sin(dByR) * Math.cos(lat1), | ||
Math.cos(dByR) - Math.sin(lat1) * Math.sin(lat) | ||
); | ||
return [toDegrees(lon), toDegrees(lat)]; | ||
} | ||
|
||
export function circleToPolygon(center: [number, number], radius: number, sides: number = 32) { | ||
var coordinates = []; | ||
for (var i = 0; i < sides; ++i) { | ||
coordinates.push( | ||
offset( | ||
center, radius, earthRadius, (2 * Math.PI * i) / sides | ||
) | ||
); | ||
} | ||
coordinates.push(coordinates[0]); | ||
|
||
return { | ||
type: "Polygon", | ||
coordinates: [coordinates], | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters