Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/damianc/math-distantpoint
Getting a point by distance from other point.
https://github.com/damianc/math-distantpoint
Last synced: about 1 month ago
JSON representation
Getting a point by distance from other point.
- Host: GitHub
- URL: https://github.com/damianc/math-distantpoint
- Owner: damianc
- Created: 2023-10-17T00:40:13.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2023-10-17T00:52:49.000Z (about 1 year ago)
- Last Synced: 2023-10-17T10:50:56.679Z (about 1 year ago)
- Language: JavaScript
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# math-distantPoint
The `Math.distantPoint()` method is meant to get a point by distance from other point along a line segment.
```
distantPoint(
[x1,y1],
[x2,y2],
dist,
relative = false
)
```It may be interpreted as follows:
- get a line $f$ that passes through both $(x_1,y_1)$ and $(x_2,y_2)$ points
- go given distance along the line $f$ from $(x_1,y_1)$ towards $(x_2,y_2)$
- if the `relative` parameter is `true`, then it is a percent of a full distance between the points
- return `[x,y]`, i.e., coordinates of the point found## Examples
```
const p = {x:2, y:4};
const q = {x:8, y:3};
const distance = 2;const [rx,ry] = Math.distantPoint(
[p.x,p.y], [q.x,q.y], distance
);
const prDist = Math.hypot(
rx-p.x, ry-p.y
);
console.log(
distance === Math.round(prDist)
);
// trueconst h = [
(p.x+q.x)/2,
(p.y+q.y)/2
];
const half = Math.distantPoint(
[p.x,p.y], [q.x,q.y], 50, true
);
console.log(h, half);
// [5, 3.5]
// [5, 3.5]
```