From eb25b161c07ac805436ea53b9c4436c38f072c01 Mon Sep 17 00:00:00 2001 From: ysCha Date: Fri, 14 Nov 2025 19:02:18 +0900 Subject: [PATCH] sk3 --- src/util/skeleton-utils.js | 83 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/src/util/skeleton-utils.js b/src/util/skeleton-utils.js index 9f1962c2..e525171b 100644 --- a/src/util/skeleton-utils.js +++ b/src/util/skeleton-utils.js @@ -2771,4 +2771,87 @@ function pointToLineDistance(point, lineP1, lineP2) { const dx = point.x - xx; const dy = point.y - yy; return Math.sqrt(dx * dx + dy * dy); +} + +/** + * Moves both p1 and p2 in the specified direction by a given distance + * @param {Object} p1 - The first point {x, y} + * @param {Object} p2 - The second point {x, y} + * @param {string} direction - Direction to move ('up', 'down', 'left', 'right') + * @param {number} distance - Distance to move + * @returns {Object} Object containing the new positions of p1 and p2 + */ +function moveLineInDirection(p1, p2, direction, distance) { + // Create copies to avoid mutating the original points + const newP1 = { ...p1 }; + const newP2 = { ...p2 }; + + const move = (point) => { + switch (direction.toLowerCase()) { + case 'up': + point.y -= distance; + break; + case 'down': + point.y += distance; + break; + case 'left': + point.x -= distance; + break; + case 'right': + point.x += distance; + break; + default: + throw new Error('Invalid direction. Use "up", "down", "left", or "right"'); + } + return point; + }; + + return { + p1: move(newP1), + p2: move(newP2) + }; +} + +/** + * Determines the direction and distance between original points (p1, p2) and moved points (newP1, newP2) + * @param {Object} p1 - Original first point {x, y} + * @param {Object} p2 - Original second point {x, y} + * @param {Object} newP1 - Moved first point {x, y} + * @param {Object} newP2 - Moved second point {x, y} + * @returns {Object} Object containing direction and distance of movement + */ +function getMovementInfo(p1, p2, newP1, newP2) { + // Calculate the movement vector for both points + const dx1 = newP1.x - p1.x; + const dy1 = newP1.y - p1.y; + const dx2 = newP2.x - p2.x; + const dy2 = newP2.y - p2.y; + + // Verify that both points moved by the same amount + if (dx1 !== dx2 || dy1 !== dy2) { + throw new Error('Points did not move in parallel'); + } + + // Determine the primary direction of movement + let direction; + const absDx = Math.abs(dx1); + const absDy = Math.abs(dy1); + + if (absDx > absDy) { + // Horizontal movement is dominant + direction = dx1 > 0 ? 'right' : 'left'; + } else { + // Vertical movement is dominant + direction = dy1 > 0 ? 'down' : 'up'; + } + + // Calculate the actual distance moved + const distance = Math.sqrt(dx1 * dx1 + dy1 * dy1); + + return { + direction, + distance, + dx: dx1, + dy: dy1 + }; } \ No newline at end of file