Skip to content

Commit

Permalink
Merge branch 'feat/worker-w-seperate-styling' of github.com:reearth/c…
Browse files Browse the repository at this point in the history
…esium-mvt-imagery-provider into feat/worker-w-seperate-styling
  • Loading branch information
pyshx committed Mar 6, 2024
2 parents 7ebf02a + c0dc056 commit fd96004
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import Point from "@mapbox/point-geometry";

const tempPoint = new Point(0, 0);

export function isPointClicked(feature: Point[][], clicked: Point, radius: number): boolean {
if (feature.length === 0 || feature[0].length === 0) return false;
const point = feature[0][0];
const distSquared = point.distSqr(clicked);
return distSquared <= radius ** 2;
}

export function isLineStringClicked(feature: Point[][], mouse: Point, lineWidth: number): boolean {
if (feature.length === 0) return false;
const points = feature[0];
const dist = lineWidth / 2;

for (let i = 0; i < points.length - 1; i++) {
const start = points[i];
const end = points[i + 1];
if (isLineSegmentClicked(start, end, mouse, dist, tempPoint)) {
return true;
}
}

return false;
}

function isLineSegmentClicked(
start: Point,
end: Point,
mouse: Point,
dist: number,
tempPoint = new Point(0, 0),
): boolean {
const lineLengthSquared = start.distSqr(end);
if (lineLengthSquared === 0) return false;

const t =
((mouse.x - start.x) * (end.x - start.x) + (mouse.y - start.y) * (end.y - start.y)) /
lineLengthSquared;
const tClamped = Math.max(0, Math.min(1, t));

const nearest = tempPoint;
nearest.x = start.x + tClamped * (end.x - start.x);
nearest.y = start.y + tClamped * (end.y - start.y);

const distanceSquared = mouse.distSqr(nearest);
return distanceSquared <= dist ** 2;
}

0 comments on commit fd96004

Please sign in to comment.