source: imaps-frontend/node_modules/svg-pathdata/src/mathUtils.ts@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 6.2 KB
Line 
1import { SVGPathData } from "./SVGPathData";
2import { CommandA, CommandC } from "./types";
3
4export function rotate([x, y]: [number, number], rad: number) {
5 return [
6 x * Math.cos(rad) - y * Math.sin(rad),
7 x * Math.sin(rad) + y * Math.cos(rad),
8 ];
9}
10
11const DEBUG_CHECK_NUMBERS = true;
12export function assertNumbers(...numbers: number[]) {
13 if (DEBUG_CHECK_NUMBERS) {
14 for (let i = 0; i < numbers.length; i++) {
15 if ("number" !== typeof numbers[i]) {
16 throw new Error(
17 `assertNumbers arguments[${i}] is not a number. ${typeof numbers[i]} == typeof ${numbers[i]}`);
18 }
19 }
20 }
21 return true;
22}
23
24const PI = Math.PI;
25
26/**
27 * https://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes
28 * Fixes rX and rY.
29 * Ensures lArcFlag and sweepFlag are 0 or 1
30 * Adds center coordinates: command.cX, command.cY (relative or absolute, depending on command.relative)
31 * Adds start and end arc parameters (in degrees): command.phi1, command.phi2; phi1 < phi2 iff. c.sweepFlag == true
32 */
33export function annotateArcCommand(c: CommandA, x1: number, y1: number) {
34 c.lArcFlag = (0 === c.lArcFlag) ? 0 : 1;
35 c.sweepFlag = (0 === c.sweepFlag) ? 0 : 1;
36 // tslint:disable-next-line
37 let {rX, rY, x, y} = c;
38
39 rX = Math.abs(c.rX);
40 rY = Math.abs(c.rY);
41 const [x1_, y1_] = rotate([(x1 - x) / 2, (y1 - y) / 2], -c.xRot / 180 * PI);
42 const testValue = Math.pow(x1_, 2) / Math.pow(rX, 2) + Math.pow(y1_, 2) / Math.pow(rY, 2);
43
44 if (1 < testValue) {
45 rX *= Math.sqrt(testValue);
46 rY *= Math.sqrt(testValue);
47 }
48 c.rX = rX;
49 c.rY = rY;
50 const c_ScaleTemp = (Math.pow(rX, 2) * Math.pow(y1_, 2) + Math.pow(rY, 2) * Math.pow(x1_, 2));
51 const c_Scale = (c.lArcFlag !== c.sweepFlag ? 1 : -1) *
52 Math.sqrt(Math.max(0, (Math.pow(rX, 2) * Math.pow(rY, 2) - c_ScaleTemp) / c_ScaleTemp));
53 const cx_ = rX * y1_ / rY * c_Scale;
54 const cy_ = -rY * x1_ / rX * c_Scale;
55 const cRot = rotate([cx_, cy_], c.xRot / 180 * PI);
56
57 c.cX = cRot[0] + (x1 + x) / 2;
58 c.cY = cRot[1] + (y1 + y) / 2;
59 c.phi1 = Math.atan2((y1_ - cy_) / rY, (x1_ - cx_) / rX);
60 c.phi2 = Math.atan2((-y1_ - cy_) / rY, (-x1_ - cx_) / rX);
61 if (0 === c.sweepFlag && c.phi2 > c.phi1) {
62 c.phi2 -= 2 * PI;
63 }
64 if (1 === c.sweepFlag && c.phi2 < c.phi1) {
65 c.phi2 += 2 * PI;
66 }
67 c.phi1 *= 180 / PI;
68 c.phi2 *= 180 / PI;
69}
70
71/**
72 * Solves a quadratic system of equations of the form
73 * a * x + b * y = c
74 * x² + y² = 1
75 * This can be understood as the intersection of the unit circle with a line.
76 * => y = (c - a x) / b
77 * => x² + (c - a x)² / b² = 1
78 * => x² b² + c² - 2 c a x + a² x² = b²
79 * => (a² + b²) x² - 2 a c x + (c² - b²) = 0
80 */
81export function intersectionUnitCircleLine(a: number, b: number, c: number): [number, number][] {
82 assertNumbers(a, b, c);
83 // cf. pqFormula
84 const termSqr = a * a + b * b - c * c;
85
86 if (0 > termSqr) {
87 return [];
88 } else if (0 === termSqr) {
89 return [
90 [
91 (a * c) / (a * a + b * b),
92 (b * c) / (a * a + b * b)]];
93 }
94 const term = Math.sqrt(termSqr);
95
96 return [
97 [
98 (a * c + b * term) / (a * a + b * b),
99 (b * c - a * term) / (a * a + b * b)],
100 [
101 (a * c - b * term) / (a * a + b * b),
102 (b * c + a * term) / (a * a + b * b)]];
103
104}
105
106export const DEG = Math.PI / 180;
107
108export function lerp(a: number, b: number, t: number) {
109 return (1 - t) * a + t * b;
110}
111
112export function arcAt(c: number, x1: number, x2: number, phiDeg: number) {
113 return c + Math.cos(phiDeg / 180 * PI) * x1 + Math.sin(phiDeg / 180 * PI) * x2;
114}
115
116export function bezierRoot(x0: number, x1: number, x2: number, x3: number) {
117 const EPS = 1e-6;
118 const x01 = x1 - x0;
119 const x12 = x2 - x1;
120 const x23 = x3 - x2;
121 const a = 3 * x01 + 3 * x23 - 6 * x12;
122 const b = (x12 - x01) * 6;
123 const c = 3 * x01;
124 // solve a * t² + b * t + c = 0
125
126 if (Math.abs(a) < EPS) {
127 // equivalent to b * t + c =>
128 return [-c / b];
129 }
130 return pqFormula(b / a, c / a, EPS);
131
132}
133
134export function bezierAt(x0: number, x1: number, x2: number, x3: number, t: number) {
135 // console.log(x0, y0, x1, y1, x2, y2, x3, y3, t)
136 const s = 1 - t;
137 const c0 = s * s * s;
138 const c1 = 3 * s * s * t;
139 const c2 = 3 * s * t * t;
140 const c3 = t * t * t;
141
142 return x0 * c0 + x1 * c1 + x2 * c2 + x3 * c3;
143}
144
145function pqFormula(p: number, q: number, PRECISION = 1e-6) {
146 // 4 times the discriminant:in
147 const discriminantX4 = p * p / 4 - q;
148
149 if (discriminantX4 < -PRECISION) {
150 return [];
151 } else if (discriminantX4 <= PRECISION) {
152 return [-p / 2];
153 }
154 const root = Math.sqrt(discriminantX4);
155
156 return [-(p / 2) - root, -(p / 2) + root];
157
158}
159
160export function a2c(arc: CommandA, x0: number, y0: number): CommandC[] {
161 if (!arc.cX) {
162 annotateArcCommand(arc, x0, y0);
163 }
164
165 const phiMin = Math.min(arc.phi1!, arc.phi2!), phiMax = Math.max(arc.phi1!, arc.phi2!), deltaPhi = phiMax - phiMin;
166 const partCount = Math.ceil(deltaPhi / 90 );
167
168 const result: CommandC[] = new Array(partCount);
169 let prevX = x0, prevY = y0;
170 for (let i = 0; i < partCount; i++) {
171 const phiStart = lerp(arc.phi1!, arc.phi2!, i / partCount);
172 const phiEnd = lerp(arc.phi1!, arc.phi2!, (i + 1) / partCount);
173 const deltaPhi = phiEnd - phiStart;
174 const f = 4 / 3 * Math.tan(deltaPhi * DEG / 4);
175 // x1/y1, x2/y2 and x/y coordinates on the unit circle for phiStart/phiEnd
176 const [x1, y1] = [
177 Math.cos(phiStart * DEG) - f * Math.sin(phiStart * DEG),
178 Math.sin(phiStart * DEG) + f * Math.cos(phiStart * DEG)];
179 const [x, y] = [Math.cos(phiEnd * DEG), Math.sin(phiEnd * DEG)];
180 const [x2, y2] = [x + f * Math.sin(phiEnd * DEG), y - f * Math.cos(phiEnd * DEG)];
181 result[i] = {relative: arc.relative, type: SVGPathData.CURVE_TO } as any;
182 const transform = (x: number, y: number) => {
183 const [xTemp, yTemp] = rotate([x * arc.rX, y * arc.rY], arc.xRot);
184 return [arc.cX! + xTemp, arc.cY! + yTemp];
185 };
186 [result[i].x1, result[i].y1] = transform(x1, y1);
187 [result[i].x2, result[i].y2] = transform(x2, y2);
188 [result[i].x, result[i].y] = transform(x, y);
189 if (arc.relative) {
190 result[i].x1 -= prevX;
191 result[i].y1 -= prevY;
192 result[i].x2 -= prevX;
193 result[i].y2 -= prevY;
194 result[i].x -= prevX;
195 result[i].y -= prevY;
196 }
197 [prevX, prevY] = [result[i].x, result[i].y];
198 }
199 return result;
200}
Note: See TracBrowser for help on using the repository browser.