Reference · Deterministic 2D physics
closestOnSegmentfunction
The point on the segment a to b nearest to (px, py), and how far along the segment it is (0 to 1).
Import it
import { closestOnSegment } from "@johnmorrisdotca/karakuri/physics";
Definition
function closestOnSegment(px: number, py: number, ax: number, ay: number, bx: number, by: number): { x: number; y: number; t: number; }
Parameters
| Name | Type | Description |
|---|---|---|
px | number | The horizontal coordinate of the point to test. |
py | number | The vertical coordinate of the point to test. |
ax | number | The first segment endpoint’s horizontal coordinate. |
ay | number | The first segment endpoint’s vertical coordinate. |
bx | number | The second segment endpoint’s horizontal coordinate. |
by | number | The second segment endpoint’s vertical coordinate. |
Returns
{ x: number; y: number; t: number; }
The point on the segment a to b nearest to (px, py), and how far along the segment it is (0 to 1).
Examples
import { closestOnSegment } from "@johnmorrisdotca/karakuri/physics";
console.log(closestOnSegment(3,4,0,0,10,0));{ x: 3, y: 0, t: 0.3 }What it prints (run when this page was built)