2020-01-06 13:33:22 +04:00
|
|
|
|
// https://stackoverflow.com/a/6853926/232122
|
|
|
|
|
export function distanceBetweenPointAndSegment(
|
|
|
|
|
x: number,
|
|
|
|
|
y: number,
|
|
|
|
|
x1: number,
|
|
|
|
|
y1: number,
|
|
|
|
|
x2: number,
|
2020-01-24 12:04:54 +02:00
|
|
|
|
y2: number,
|
2020-01-06 13:33:22 +04:00
|
|
|
|
) {
|
|
|
|
|
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);
|
|
|
|
|
}
|
2020-01-06 19:34:22 +04:00
|
|
|
|
|
|
|
|
|
export function rotate(
|
|
|
|
|
x1: number,
|
|
|
|
|
y1: number,
|
|
|
|
|
x2: number,
|
|
|
|
|
y2: number,
|
2020-01-24 12:04:54 +02:00
|
|
|
|
angle: number,
|
2020-01-06 19:34:22 +04:00
|
|
|
|
) {
|
|
|
|
|
// 𝑎′𝑥=(𝑎𝑥−𝑐𝑥)cos𝜃−(𝑎𝑦−𝑐𝑦)sin𝜃+𝑐𝑥
|
|
|
|
|
// 𝑎′𝑦=(𝑎𝑥−𝑐𝑥)sin𝜃+(𝑎𝑦−𝑐𝑦)cos𝜃+𝑐𝑦.
|
|
|
|
|
// https://math.stackexchange.com/questions/2204520/how-do-i-rotate-a-line-segment-in-a-specific-point-on-the-line
|
|
|
|
|
return [
|
|
|
|
|
(x1 - x2) * Math.cos(angle) - (y1 - y2) * Math.sin(angle) + x2,
|
2020-01-24 12:04:54 +02:00
|
|
|
|
(x1 - x2) * Math.sin(angle) + (y1 - y2) * Math.cos(angle) + y2,
|
2020-01-06 19:34:22 +04:00
|
|
|
|
];
|
|
|
|
|
}
|