Prefer arrow functions and callbacks (#1210)
This commit is contained in:
39
src/math.ts
39
src/math.ts
@ -2,14 +2,14 @@ import { Point } from "./types";
|
||||
import { LINE_CONFIRM_THRESHOLD } from "./constants";
|
||||
|
||||
// https://stackoverflow.com/a/6853926/232122
|
||||
export function distanceBetweenPointAndSegment(
|
||||
export const 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;
|
||||
@ -38,23 +38,22 @@ export function distanceBetweenPointAndSegment(
|
||||
const dx = x - xx;
|
||||
const dy = y - yy;
|
||||
return Math.hypot(dx, dy);
|
||||
}
|
||||
};
|
||||
|
||||
export function rotate(
|
||||
export const rotate = (
|
||||
x1: number,
|
||||
y1: number,
|
||||
x2: number,
|
||||
y2: number,
|
||||
angle: number,
|
||||
): [number, number] {
|
||||
): [number, number] =>
|
||||
// 𝑎′𝑥=(𝑎𝑥−𝑐𝑥)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,
|
||||
(x1 - x2) * Math.sin(angle) + (y1 - y2) * Math.cos(angle) + y2,
|
||||
];
|
||||
}
|
||||
|
||||
export const adjustXYWithRotation = (
|
||||
side: "n" | "s" | "w" | "e" | "nw" | "ne" | "sw" | "se",
|
||||
@ -233,15 +232,15 @@ export const getPointOnAPath = (point: Point, path: Point[]) => {
|
||||
return null;
|
||||
};
|
||||
|
||||
export function distance2d(x1: number, y1: number, x2: number, y2: number) {
|
||||
export const distance2d = (x1: number, y1: number, x2: number, y2: number) => {
|
||||
const xd = x2 - x1;
|
||||
const yd = y2 - y1;
|
||||
return Math.hypot(xd, yd);
|
||||
}
|
||||
};
|
||||
|
||||
// Checks if the first and last point are close enough
|
||||
// to be considered a loop
|
||||
export function isPathALoop(points: Point[]): boolean {
|
||||
export const isPathALoop = (points: Point[]): boolean => {
|
||||
if (points.length >= 3) {
|
||||
const [firstPoint, lastPoint] = [points[0], points[points.length - 1]];
|
||||
return (
|
||||
@ -250,16 +249,16 @@ export function isPathALoop(points: Point[]): boolean {
|
||||
);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
// Draw a line from the point to the right till infiinty
|
||||
// Check how many lines of the polygon does this infinite line intersects with
|
||||
// If the number of intersections is odd, point is in the polygon
|
||||
export function isPointInPolygon(
|
||||
export const isPointInPolygon = (
|
||||
points: Point[],
|
||||
x: number,
|
||||
y: number,
|
||||
): boolean {
|
||||
): boolean => {
|
||||
const vertices = points.length;
|
||||
|
||||
// There must be at least 3 vertices in polygon
|
||||
@ -281,32 +280,32 @@ export function isPointInPolygon(
|
||||
}
|
||||
// true if count is off
|
||||
return count % 2 === 1;
|
||||
}
|
||||
};
|
||||
|
||||
// Check if q lies on the line segment pr
|
||||
function onSegment(p: Point, q: Point, r: Point) {
|
||||
const onSegment = (p: Point, q: Point, r: Point) => {
|
||||
return (
|
||||
q[0] <= Math.max(p[0], r[0]) &&
|
||||
q[0] >= Math.min(p[0], r[0]) &&
|
||||
q[1] <= Math.max(p[1], r[1]) &&
|
||||
q[1] >= Math.min(p[1], r[1])
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
// For the ordered points p, q, r, return
|
||||
// 0 if p, q, r are collinear
|
||||
// 1 if Clockwise
|
||||
// 2 if counterclickwise
|
||||
function orientation(p: Point, q: Point, r: Point) {
|
||||
const orientation = (p: Point, q: Point, r: Point) => {
|
||||
const val = (q[1] - p[1]) * (r[0] - q[0]) - (q[0] - p[0]) * (r[1] - q[1]);
|
||||
if (val === 0) {
|
||||
return 0;
|
||||
}
|
||||
return val > 0 ? 1 : 2;
|
||||
}
|
||||
};
|
||||
|
||||
// Check is p1q1 intersects with p2q2
|
||||
function doIntersect(p1: Point, q1: Point, p2: Point, q2: Point) {
|
||||
const doIntersect = (p1: Point, q1: Point, p2: Point, q2: Point) => {
|
||||
const o1 = orientation(p1, q1, p2);
|
||||
const o2 = orientation(p1, q1, q2);
|
||||
const o3 = orientation(p2, q2, p1);
|
||||
@ -337,4 +336,4 @@ function doIntersect(p1: Point, q1: Point, p2: Point, q2: Point) {
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
Reference in New Issue
Block a user