melt
Version:
The next generation of Melt UI. Built for Svelte 5.
33 lines (32 loc) • 1.07 kB
JavaScript
export * from "./hull";
import { computeConvexHull } from "./hull";
export function getPointsFromEl(el) {
const rect = el.getBoundingClientRect();
return [
{ x: rect.left, y: rect.top }, // tl
{ x: rect.right, y: rect.top }, // tr
{ x: rect.right, y: rect.bottom }, // br
{ x: rect.left, y: rect.bottom }, // bl
];
}
export function computeConvexHullFromElements(els) {
const points = els.flatMap((el) => getPointsFromEl(el));
return computeConvexHull(points);
}
export function pointInPolygon(point, polygon) {
let inside = false;
for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
const pi = polygon[i];
const pj = polygon[j];
if (!pi || !pj)
continue;
const xi = pi.x;
const yi = pi.y;
const xj = pj.x;
const yj = pj.y;
const intersect = yi > point.y !== yj > point.y && point.x < ((xj - xi) * (point.y - yi)) / (yj - yi) + xi;
if (intersect)
inside = !inside;
}
return inside;
}