Reference · Deterministic 2D physics
closestBetweenSegmentsfunction
The nearest points of two segments: where on each, and how far apart they are. Crossing segments are 0 apart.
Import it
import { closestBetweenSegments } from "@johnmorrisdotca/karakuri/physics";
Definition
function closestBetweenSegments(a1x: number, a1y: number, a2x: number, a2y: number, b1x: number, b1y: number, b2x: number, b2y: number): { ax: number; ay: number; bx: number; by: number; distance: number; }
Parameters
| Name | Type | Description |
|---|---|---|
a1x | number | The first segment’s starting horizontal coordinate. |
a1y | number | The first segment’s starting vertical coordinate. |
a2x | number | The first segment’s ending horizontal coordinate. |
a2y | number | The first segment’s ending vertical coordinate. |
b1x | number | The other segment’s starting horizontal coordinate. |
b1y | number | The other segment’s starting vertical coordinate. |
b2x | number | The other segment’s ending horizontal coordinate. |
b2y | number | The other segment’s ending vertical coordinate. |
Returns
{ ax: number; ay: number; bx: number; by: number; distance: number; }
The nearest points of two segments: where on each, and how far apart they are. Crossing segments are 0 apart.
Examples
import { closestBetweenSegments } from "@johnmorrisdotca/karakuri/physics";
console.log(closestBetweenSegments(0,0,10,0,0,5,10,5));{ ax: 0, ay: 0, bx: 0, by: 5, distance: 5 }What it prints (run when this page was built)