From e3eef04e0061d0cd5a376f6a8b5bb058f58d9d10 Mon Sep 17 00:00:00 2001 From: Gasim Gasimzada Date: Mon, 6 Jan 2020 13:33:22 +0400 Subject: [PATCH] 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 --- src/index.tsx | 61 +++------------------------------------------------ src/math.ts | 38 ++++++++++++++++++++++++++++++++ src/random.ts | 18 +++++++++++++++ 3 files changed, 59 insertions(+), 58 deletions(-) create mode 100644 src/math.ts create mode 100644 src/random.ts diff --git a/src/index.tsx b/src/index.tsx index 03390de0..d29567b4 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -5,7 +5,10 @@ import { RoughCanvas } from "roughjs/bin/canvas"; import { TwitterPicker } from "react-color"; import { moveOneLeft, moveAllLeft, moveOneRight, moveAllRight } from "./zindex"; +import { LCG, randomSeed, withCustomMathRandom } from "./random"; +import { distanceBetweenPointAndSegment } from "./math"; import { roundRect } from "./roundRect"; + import EditableText from "./components/EditableText"; import "./styles.scss"; @@ -55,64 +58,6 @@ function restoreHistoryEntry(entry: string) { skipHistory = true; } -// https://stackoverflow.com/questions/521295/seeding-the-random-number-generator-in-javascript/47593316#47593316 -const LCG = (seed: number) => () => - ((2 ** 31 - 1) & (seed = Math.imul(48271, seed))) / 2 ** 31; - -function randomSeed() { - return Math.floor(Math.random() * 2 ** 31); -} - -// Unfortunately, roughjs doesn't support a seed attribute (https://github.com/pshihn/rough/issues/27). -// We can achieve the same result by overriding the Math.random function with a -// pseudo random generator that supports a random seed and swapping it back after. -function withCustomMathRandom(seed: number, cb: () => T): T { - const random = Math.random; - Math.random = LCG(seed); - const result = cb(); - Math.random = random; - return result; -} - -// https://stackoverflow.com/a/6853926/232122 -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); -} - function hitTest(element: ExcalidrawElement, x: number, y: number): boolean { // For shapes that are composed of lines, we only enable point-selection when the distance // of the click is less than x pixels of any of the lines that the shape is composed of diff --git a/src/math.ts b/src/math.ts new file mode 100644 index 00000000..f699fea3 --- /dev/null +++ b/src/math.ts @@ -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); +} diff --git a/src/random.ts b/src/random.ts new file mode 100644 index 00000000..9147c80c --- /dev/null +++ b/src/random.ts @@ -0,0 +1,18 @@ +// https://stackoverflow.com/questions/521295/seeding-the-random-number-generator-in-javascript/47593316#47593316 +export const LCG = (seed: number) => () => + ((2 ** 31 - 1) & (seed = Math.imul(48271, seed))) / 2 ** 31; + +export function randomSeed() { + return Math.floor(Math.random() * 2 ** 31); +} + +// Unfortunately, roughjs doesn't support a seed attribute (https://github.com/pshihn/rough/issues/27). +// We can achieve the same result by overriding the Math.random function with a +// pseudo random generator that supports a random seed and swapping it back after. +export function withCustomMathRandom(seed: number, cb: () => T): T { + const random = Math.random; + Math.random = LCG(seed); + const result = cb(); + Math.random = random; + return result; +}