mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-05-03 10:00:07 -04:00
Move math and random files into their respective modules (#198)
* Move math and random files into their respective modules - Move distanceBetweenPointAndSegment to math module - Move LCG, randomSeed, and withCustomMathRandom to random module * Add everything else back
This commit is contained in:
parent
b3667000e2
commit
e3eef04e00
3 changed files with 59 additions and 58 deletions
38
src/math.ts
Normal file
38
src/math.ts
Normal file
|
@ -0,0 +1,38 @@
|
|||
// https://stackoverflow.com/a/6853926/232122
|
||||
export function distanceBetweenPointAndSegment(
|
||||
x: number,
|
||||
y: number,
|
||||
x1: number,
|
||||
y1: number,
|
||||
x2: number,
|
||||
y2: number
|
||||
) {
|
||||
const A = x - x1;
|
||||
const B = y - y1;
|
||||
const C = x2 - x1;
|
||||
const D = y2 - y1;
|
||||
|
||||
const dot = A * C + B * D;
|
||||
const lenSquare = C * C + D * D;
|
||||
let param = -1;
|
||||
if (lenSquare !== 0) {
|
||||
// in case of 0 length line
|
||||
param = dot / lenSquare;
|
||||
}
|
||||
|
||||
let xx, yy;
|
||||
if (param < 0) {
|
||||
xx = x1;
|
||||
yy = y1;
|
||||
} else if (param > 1) {
|
||||
xx = x2;
|
||||
yy = y2;
|
||||
} else {
|
||||
xx = x1 + param * C;
|
||||
yy = y1 + param * D;
|
||||
}
|
||||
|
||||
const dx = x - xx;
|
||||
const dy = y - yy;
|
||||
return Math.hypot(dx, dy);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue