Просмотр исходного кода

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
vanilla_orig
Gasim Gasimzada 5 лет назад
Родитель
Сommit
e3eef04e00
3 измененных файлов: 59 добавлений и 58 удалений
  1. 3
    58
      src/index.tsx
  2. 38
    0
      src/math.ts
  3. 18
    0
      src/random.ts

+ 3
- 58
src/index.tsx Просмотреть файл

@@ -5,7 +5,10 @@ import { RoughCanvas } from "roughjs/bin/canvas";
5 5
 import { TwitterPicker } from "react-color";
6 6
 
7 7
 import { moveOneLeft, moveAllLeft, moveOneRight, moveAllRight } from "./zindex";
8
+import { LCG, randomSeed, withCustomMathRandom } from "./random";
9
+import { distanceBetweenPointAndSegment } from "./math";
8 10
 import { roundRect } from "./roundRect";
11
+
9 12
 import EditableText from "./components/EditableText";
10 13
 
11 14
 import "./styles.scss";
@@ -55,64 +58,6 @@ function restoreHistoryEntry(entry: string) {
55 58
   skipHistory = true;
56 59
 }
57 60
 
58
-// https://stackoverflow.com/questions/521295/seeding-the-random-number-generator-in-javascript/47593316#47593316
59
-const LCG = (seed: number) => () =>
60
-  ((2 ** 31 - 1) & (seed = Math.imul(48271, seed))) / 2 ** 31;
61
-
62
-function randomSeed() {
63
-  return Math.floor(Math.random() * 2 ** 31);
64
-}
65
-
66
-// Unfortunately, roughjs doesn't support a seed attribute (https://github.com/pshihn/rough/issues/27).
67
-// We can achieve the same result by overriding the Math.random function with a
68
-// pseudo random generator that supports a random seed and swapping it back after.
69
-function withCustomMathRandom<T>(seed: number, cb: () => T): T {
70
-  const random = Math.random;
71
-  Math.random = LCG(seed);
72
-  const result = cb();
73
-  Math.random = random;
74
-  return result;
75
-}
76
-
77
-// https://stackoverflow.com/a/6853926/232122
78
-function distanceBetweenPointAndSegment(
79
-  x: number,
80
-  y: number,
81
-  x1: number,
82
-  y1: number,
83
-  x2: number,
84
-  y2: number
85
-) {
86
-  const A = x - x1;
87
-  const B = y - y1;
88
-  const C = x2 - x1;
89
-  const D = y2 - y1;
90
-
91
-  const dot = A * C + B * D;
92
-  const lenSquare = C * C + D * D;
93
-  let param = -1;
94
-  if (lenSquare !== 0) {
95
-    // in case of 0 length line
96
-    param = dot / lenSquare;
97
-  }
98
-
99
-  let xx, yy;
100
-  if (param < 0) {
101
-    xx = x1;
102
-    yy = y1;
103
-  } else if (param > 1) {
104
-    xx = x2;
105
-    yy = y2;
106
-  } else {
107
-    xx = x1 + param * C;
108
-    yy = y1 + param * D;
109
-  }
110
-
111
-  const dx = x - xx;
112
-  const dy = y - yy;
113
-  return Math.hypot(dx, dy);
114
-}
115
-
116 61
 function hitTest(element: ExcalidrawElement, x: number, y: number): boolean {
117 62
   // For shapes that are composed of lines, we only enable point-selection when the distance
118 63
   // of the click is less than x pixels of any of the lines that the shape is composed of

+ 38
- 0
src/math.ts Просмотреть файл

@@ -0,0 +1,38 @@
1
+// https://stackoverflow.com/a/6853926/232122
2
+export function distanceBetweenPointAndSegment(
3
+  x: number,
4
+  y: number,
5
+  x1: number,
6
+  y1: number,
7
+  x2: number,
8
+  y2: number
9
+) {
10
+  const A = x - x1;
11
+  const B = y - y1;
12
+  const C = x2 - x1;
13
+  const D = y2 - y1;
14
+
15
+  const dot = A * C + B * D;
16
+  const lenSquare = C * C + D * D;
17
+  let param = -1;
18
+  if (lenSquare !== 0) {
19
+    // in case of 0 length line
20
+    param = dot / lenSquare;
21
+  }
22
+
23
+  let xx, yy;
24
+  if (param < 0) {
25
+    xx = x1;
26
+    yy = y1;
27
+  } else if (param > 1) {
28
+    xx = x2;
29
+    yy = y2;
30
+  } else {
31
+    xx = x1 + param * C;
32
+    yy = y1 + param * D;
33
+  }
34
+
35
+  const dx = x - xx;
36
+  const dy = y - yy;
37
+  return Math.hypot(dx, dy);
38
+}

+ 18
- 0
src/random.ts Просмотреть файл

@@ -0,0 +1,18 @@
1
+// https://stackoverflow.com/questions/521295/seeding-the-random-number-generator-in-javascript/47593316#47593316
2
+export const LCG = (seed: number) => () =>
3
+  ((2 ** 31 - 1) & (seed = Math.imul(48271, seed))) / 2 ** 31;
4
+
5
+export function randomSeed() {
6
+  return Math.floor(Math.random() * 2 ** 31);
7
+}
8
+
9
+// Unfortunately, roughjs doesn't support a seed attribute (https://github.com/pshihn/rough/issues/27).
10
+// We can achieve the same result by overriding the Math.random function with a
11
+// pseudo random generator that supports a random seed and swapping it back after.
12
+export function withCustomMathRandom<T>(seed: number, cb: () => T): T {
13
+  const random = Math.random;
14
+  Math.random = LCG(seed);
15
+  const result = cb();
16
+  Math.random = random;
17
+  return result;
18
+}

Загрузка…
Отмена
Сохранить