SurisDziugas
SurisDziugas

Reputation: 158

Generate coordinates given distance, angle from a center

I have a given center in the map [x1,y1]. From that center I am drawing a circle with a 1 mile radius. I need to generate 8 more points around the circle, the distance between the individual points to center should be 1 mile, so they are on the circle bounds. I do know the formulas to get x2, y2 but the problem is it doesn't apply to earth's map since it isn't a perfect sphere.

I've tried using this, but with no luck.

Could anyone point me somewhere or maybe I got this wrong ?

Edit: solved !

So reading carefully throughout Movable Type Scripts I found this (slightly modified for my use):

   let getPoint = (distance, bearing, center) => {

    let δ = Number(distance) / 6371e3; 
    let θ = Number(bearing).toRadians();

    let φ1 = center[0].toRadians();
    let λ1 = center[1].toRadians();

    let sinφ1 = Math.sin(φ1), cosφ1 = Math.cos(φ1);
    let sinδ = Math.sin(δ), cosδ = Math.cos(δ);
    let sinθ = Math.sin(θ), cosθ = Math.cos(θ);

    let sinφ2 = sinφ1*cosδ + cosφ1*sinδ*cosθ;
    let φ2 = Math.asin(sinφ2);
    let y = sinθ * sinδ * cosφ1;
    let x = cosδ - sinφ1 * sinφ2;
    let λ2 = λ1 + Math.atan2(y, x);

    return [φ2.toDegrees(), (λ2.toDegrees()+540)%360-180]; 
};

It did solved my problem.

Upvotes: 1

Views: 1531

Answers (1)

IvanSanchez
IvanSanchez

Reputation: 19089

You are trying to solve what is known as the first (or direct) geodetic problem. Knowing this name will make your research easier.

As pointed out by the answers to "How to draw polyline perpendicular to another polyline using Leaflet" and "Find destination coordinates given starting coodinates, bearing, and distance", your main options to approach this problem in javascript are cheap-ruler for small(ish) areas and greographiclib for large distances.

cheap-ruler tends to be very fast but inaccurate, and geographiclib tends to be slower but very accurate.

You might find other implementations, each with its own compromises. Geodesy is hard, so there is no "one true way" to calculate distances or azimuths.

Upvotes: 2

Related Questions