mirror of
https://github.com/Mastermindzh/examples.git
synced 2025-09-03 01:54:31 +02:00
consolidation of various example repos/gists
This commit is contained in:
52
typescript/geo/radiusToPolygon/helpers.ts
Normal file
52
typescript/geo/radiusToPolygon/helpers.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { Point } from "./point";
|
||||
|
||||
/**
|
||||
* Converts an angle in radians to an angle in degrees by multiplying with 180 and dividing by 180
|
||||
* @param angleInRadians
|
||||
*/
|
||||
export const radiansToDegrees = (angleInRadians: number) => {
|
||||
return (angleInRadians * 180) / Math.PI;
|
||||
};
|
||||
|
||||
/**
|
||||
* Convert an angle in degrees to an angle in radians by multiplying by PI and dividing the result by 180
|
||||
* https://www.mathwarehouse.com/trigonometry/radians/convert-degee-to-radians.php
|
||||
* @param angleInDegrees
|
||||
*/
|
||||
export const degreesToRadians = (angleInDegrees: number) => {
|
||||
return (angleInDegrees * Math.PI) / 180;
|
||||
};
|
||||
|
||||
/**
|
||||
* Convert a point object to an array of longitude,latitude
|
||||
* @param point
|
||||
*/
|
||||
export const pointToLongLatArray = (point: Point) => {
|
||||
return [point.longitude, point.latitude];
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a new point given a point, distance and bearing
|
||||
* https://www.igismap.com/formula-to-find-bearing-or-heading-angle-between-two-points-latitude-longitude/#:~:text=Here%20is%20the%20formula%20to,%E2%80%93%20sin%20la1%20*%20sin%20la2)
|
||||
*/
|
||||
export const getRelativePointByDistance = (
|
||||
point: Point,
|
||||
distance: number,
|
||||
bearing: number
|
||||
): Point => {
|
||||
const latitude = Math.asin(
|
||||
Math.sin(point.latitude) * Math.cos(distance) +
|
||||
Math.cos(point.latitude) * Math.sin(distance) * Math.cos(bearing)
|
||||
);
|
||||
const longitude =
|
||||
point.longitude +
|
||||
Math.atan2(
|
||||
Math.sin(bearing) * Math.sin(distance) * Math.cos(point.latitude),
|
||||
Math.cos(distance) - Math.sin(point.latitude) * Math.sin(latitude)
|
||||
);
|
||||
|
||||
return {
|
||||
latitude: radiansToDegrees(latitude),
|
||||
longitude: radiansToDegrees(longitude),
|
||||
};
|
||||
};
|
55
typescript/geo/radiusToPolygon/index.ts
Normal file
55
typescript/geo/radiusToPolygon/index.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import {
|
||||
degreesToRadians,
|
||||
getRelativePointByDistance,
|
||||
pointToLongLatArray,
|
||||
} from "./helpers";
|
||||
import { Point } from "./point";
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
const options = {
|
||||
/**
|
||||
* IERS Equatorial Radius of the earth
|
||||
* https://en.wikipedia.org/wiki/Earth_ellipsoid
|
||||
*/
|
||||
equatorialRadiusInMeters: 6378136.6,
|
||||
|
||||
/**
|
||||
* Number of edges to create in the polygon
|
||||
* Use 360 to get a true circle
|
||||
*/
|
||||
numberOfEdges: 64,
|
||||
};
|
||||
|
||||
const input = {
|
||||
center: { latitude: 51.7543453, longitude: 5.5526647 } as Point,
|
||||
radius: 2000,
|
||||
};
|
||||
|
||||
const coordinates = [];
|
||||
for (var i = 0; i < options.numberOfEdges; ++i) {
|
||||
const newPoint = getRelativePointByDistance(
|
||||
{
|
||||
latitude: degreesToRadians(input.center.latitude),
|
||||
longitude: degreesToRadians(input.center.longitude),
|
||||
},
|
||||
input.radius / options.equatorialRadiusInMeters,
|
||||
// find bearing https://www.igismap.com/formula-to-find-bearing-or-heading-angle-between-two-points-latitude-longitude/
|
||||
(2 * Math.PI * -i) / options.numberOfEdges
|
||||
);
|
||||
coordinates.push(pointToLongLatArray(newPoint));
|
||||
}
|
||||
// make sure the circle is closed by pushing a copy of the first element
|
||||
coordinates.push(coordinates[0]);
|
||||
|
||||
console.log(
|
||||
JSON.stringify(
|
||||
{
|
||||
type: "Polygon",
|
||||
coordinates: [coordinates],
|
||||
},
|
||||
null,
|
||||
2
|
||||
)
|
||||
);
|
4
typescript/geo/radiusToPolygon/point.ts
Normal file
4
typescript/geo/radiusToPolygon/point.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export interface Point {
|
||||
latitude: number;
|
||||
longitude: number;
|
||||
}
|
Reference in New Issue
Block a user