sk3
This commit is contained in:
parent
25ae9afdb5
commit
eb25b161c0
@ -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
|
||||
};
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user