Prefer arrow functions and callbacks (#1210)

This commit is contained in:
Lipis
2020-05-20 16:21:37 +03:00
committed by GitHub
parent 33fe223b5d
commit c427aa3cce
64 changed files with 784 additions and 847 deletions

View File

@ -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;
}
};