Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/damianc/math-atan2point

Getting point by distance and angle.
https://github.com/damianc/math-atan2point

Last synced: about 1 month ago
JSON representation

Getting point by distance and angle.

Awesome Lists containing this project

README

        

# math-atan2point

A method for getting a point by distance and angle. While `Math.atan2()` returns an angle between the x-axis and a ray, `Math.atan2Point()` returns the point that creates a ray with given length and angle.

```
Math.atan2Point(
angle, distance, origin = [0,0]
)
```

## Description

- if $\alpha$ equals $\text{atan2}(P_y,P_x)$
- if $d$ equals $\sqrt{P_x^2 + P_y^2}$ (distance from $(0,0)$ to the point $P$)
- then $\text{atan2Point}(\alpha,d)$ equals $[P_x,P_y]$

```
const p = {x:2, y:4};
const q = {x:5, y:3};

const p0Dist = Math.hypot(p.x, p.y);
const pqDist = Math.hypot(p.x-q.x, p.y-q.y);

const p0Angle = Math.atan2(p.y, p.x);
const pqAngle = Math.atan2(
q.y-p.y,
q.x-p.x
);

console.log(
Math.atan2Point(
p0Angle, p0Dist
)
);
// should be: [2,4]
// is: [2,4]

console.log(
Math.atan2Point(
pqAngle, pqDist, [p.x,p.y]
)
);
// should be: [5,3]
// is: [5,3]
```