Last active
December 22, 2024 13:31
-
-
Save zopieux/6e3a57003cb7cb9b873bd3476d2ca6ab to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function calculateFlexPoints() { | |
const p1 = points.find(p => p.id === segment.p1_id); | |
const p2 = points.find(p => p.id === segment.p2_id); | |
if (!p1 || !p2) return []; | |
const dx = p2.x - p1.x; | |
const dy = p2.y - p1.y; | |
const segment_length = Math.sqrt(dx * dx + dy * dy); | |
const flexPointsCoords = []; | |
let total_gap = flexPoints.gap * (flexPoints.count - 1); | |
if (flexPoints.alignment === 'SpaceAround') { | |
total_gap = flexPoints.gap * (flexPoints.count + 1); | |
} else if (flexPoints.alignment === 'SpaceEvenly') { | |
total_gap = flexPoints.gap * (flexPoints.count); | |
} | |
let direction = 1; | |
if (flexPoints.alignment === 'Start') { | |
} else if (flexPoints.alignment === 'End') { | |
direction = -1; | |
} else if (flexPoints.alignment === 'Center') { | |
} | |
for (let i = 0; i < flexPoints.count; i++) { | |
let t = 0; | |
if (flexPoints.alignment === 'SpaceBetween') { | |
t = i / (flexPoints.count - 1); | |
} else if (flexPoints.alignment === 'SpaceAround') { | |
t = (i + 1) / (flexPoints.count + 1); | |
} else if (flexPoints.alignment === 'SpaceEvenly') { | |
t = (i + 0.5) / (flexPoints.count); | |
} else if (flexPoints.alignment === 'Start') { | |
t = 0; | |
} else if (flexPoints.alignment === 'End') { | |
t = 1; | |
} else if (flexPoints.alignment === 'Center') { | |
t = 0.5; | |
} | |
let gap_offset = 0; | |
if (flexPoints.alignment === 'Start') { | |
gap_offset += i * flexPoints.gap; | |
} else if (flexPoints.alignment === 'End') { | |
gap_offset = -total_gap; | |
gap_offset += i * flexPoints.gap; | |
} else if (flexPoints.alignment === 'Center') { | |
gap_offset = -total_gap / 2; | |
gap_offset += i * flexPoints.gap; | |
} | |
const point_x = p1.x + dx * t + gap_offset * (dx / segment_length); | |
const point_y = p1.y + dy * t + gap_offset * (dy / segment_length); | |
flexPointsCoords.push({ x: point_x, y: point_y }); | |
} | |
return flexPointsCoords; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function lineRectIntersection(startx, starty, otherx, othery, leftx, topy, width, height, ray = false) { | |
const rightx = leftx + width; | |
const bottomy = topy + height; | |
const dx = otherx - startx; | |
const dy = othery - starty; | |
let tNear = -Infinity; | |
let tFar = Infinity; | |
if (dx === 0) { | |
if (startx < leftx || startx > rightx) { | |
return []; | |
} | |
} else { | |
const tx1 = (leftx - startx) / dx; | |
const tx2 = (rightx - startx) / dx; | |
tNear = Math.max(tNear, Math.min(tx1, tx2)); | |
tFar = Math.min(tFar, Math.max(tx1, tx2)); | |
} | |
if (dy === 0) { | |
if (starty < topy || starty > bottomy) { | |
return []; | |
} | |
} else { | |
const ty1 = (topy - starty) / dy; | |
const ty2 = (bottomy - starty) / dy; | |
tNear = Math.max(tNear, Math.min(ty1, ty2)); | |
tFar = Math.min(tFar, Math.max(ty1, ty2)); | |
} | |
if (tNear > tFar) { | |
return []; | |
} | |
if (ray) { | |
if (tFar < 0) return null; | |
return [ | |
{ x: startx + tNear * dx, y: starty + tNear * dy }, | |
]; | |
} else { | |
return [ | |
{ x: startx + tNear * dx, y: starty + tNear * dy }, | |
{ x: startx + tFar * dx, y: starty + tFar * dy }, | |
]; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function perpendicularLinePath(x, y, length, startx, starty, endx, endy) { | |
const dx = endx - startx; | |
const dy = endy - starty; | |
const segmentLength = Math.sqrt(dx * dx + dy * dy); | |
if (segmentLength === 0) { | |
return ""; | |
} | |
const ux = -dy / segmentLength; | |
const uy = dx / segmentLength; | |
const halfLength = length / 2; | |
const x1 = x + ux * halfLength; | |
const y1 = y + uy * halfLength; | |
const x2 = x - ux * halfLength; | |
const y2 = y - uy * halfLength; | |
return `M${x1},${y1}L${x2},${y2}`; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment