Skip to content

Commit 27f40d6

Browse files
committed
Refactor hit-test.js for readability and precision
Replaced hardcoded EPSILON value with Number.EPSILON for better precision. Reformatted several expressions and assignments to improve code readability and maintain consistent style. No functional changes were made.
1 parent b67d79c commit 27f40d6

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

src/utils/hit-test.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { mod, TWO_PI } from './math.js';
55

66
const TRANSPARENT_REGEX = /^(?:none|transparent)$/i;
77
const DEFAULT_PRECISION = 8;
8-
const EPSILON = 1e-9;
8+
const EPSILON = Number.EPSILON;
99

1010
function createPoint(x, y) {
1111
return { x, y };
@@ -62,7 +62,7 @@ function sampleArcPoints(prev, anchor, precision) {
6262
let rxs = rx * rx;
6363
let rys = ry * ry;
6464

65-
const cr = x1p * x1p / rxs + y1p * y1p / rys;
65+
const cr = (x1p * x1p) / rxs + (y1p * y1p) / rys;
6666

6767
if (cr > 1) {
6868
const s = Math.sqrt(cr);
@@ -115,10 +115,7 @@ function sampleArcPoints(prev, anchor, precision) {
115115
return deltaAngle;
116116
})();
117117

118-
const steps = Math.max(
119-
Constants.Resolution,
120-
Math.max(precision * 2, 1)
121-
);
118+
const steps = Math.max(Constants.Resolution, Math.max(precision * 2, 1));
122119

123120
const points = [];
124121

@@ -219,8 +216,7 @@ function buildPathHitParts(path, precision = DEFAULT_PRECISION) {
219216

220217
for (let i = 0; i < vertices.length; i++) {
221218
const vertex = vertices[i];
222-
const command =
223-
vertex.command || (i === 0 ? Commands.move : Commands.line);
219+
const command = vertex.command || (i === 0 ? Commands.move : Commands.line);
224220

225221
if (command === Commands.move) {
226222
closePolygon(false);
@@ -288,9 +284,8 @@ function pointInPolygons(polygons, x, y) {
288284
const v1 = polygon[j];
289285

290286
const intersects =
291-
(v1.y > y) !== (v0.y > y) &&
292-
x <
293-
((v0.x - v1.x) * (y - v1.y)) / ((v0.y - v1.y) || 1e-12) + v1.x;
287+
v1.y > y !== v0.y > y &&
288+
x < ((v0.x - v1.x) * (y - v1.y)) / (v0.y - v1.y || 1e-12) + v1.x;
294289

295290
if (intersects) {
296291
inside = !inside;
@@ -313,8 +308,7 @@ function distanceToSegmentSquared(x, y, a, b) {
313308
return ddx * ddx + ddy * ddy;
314309
}
315310

316-
const t =
317-
((x - a.x) * dx + (y - a.y) * dy) / (dx * dx + dy * dy);
311+
const t = ((x - a.x) * dx + (y - a.y) * dy) / (dx * dx + dy * dy);
318312
const clamped = Math.max(0, Math.min(1, t));
319313

320314
const cx = a.x + clamped * dx;

0 commit comments

Comments
 (0)