按定义的角度移动元素以将其移出其容器

Move an element by a defined angle to move it outside of its container

将元素按预定义角度移动的最佳和最优雅的方法是什么,以便它移出容器?

如下图所示,我想移动元素,使其刚好位于其容器之外。我只想定义一个特定的角度来实现它。 (忽略这里的度数范围,只是为了说明我的情况)

元素应该能够在所有角度 (0-359°) 中移动

我用Find rectangle boundary point at an angle from point that is not in the middle of the rectangle中的函数解决了它。

只是稍微修改了函数,使元素完全位于容器之外。

function getEdgePointBasedOnAngle(position: Position, elementSize: Size, angle: number): Position {
    angle = (angle) * (Math.PI / 180);
    const canvasSize: Size = { width: 1, height: 1 };
    const dx = Math.cos(angle);
    const dy = Math.sin(angle);

    // Left border
    if (dx < 1.0e-16) {
        const y = (position.x) * dy / dx + (canvasSize.height - position.y);
        if (y >= 0 && y <= canvasSize.height) {
            return {
             x: -(elementSize.width / 2) , 
              y: canvasSize.height - y - elementSize.height * -(dy * 0.5);
            };
        }
    }

    // Right border
    if (dx > 1.0e-16) {
        const y = (canvasSize.width - position.x) * dy / dx + position.y;
        if (y >= 0 && y <= canvasSize.height) {
            return {
             x: canvasSize.width + (elementSize.width / 2),
              y: y + elementSize.height * (dy * 0.5)
            };
        }
    }

    // Top border
    if (dy < 1.0e-16) {
        const x = (position.y) * dx / dy + (canvasSize.width - position.x);
        if (x >= 0 && x <= canvasSize.width) {
            return {
             x: canvasSize.width - x - elementSize.width * -(dx * 0.5),
              y: -(elementSize.height / 2)
            };
        }
    }

    // Bottom border
    if (dy > 1.0e-16) {
        const x = (canvasSize.height - position.y) * dx / dy + position.x;
        if (x >= 0 && x <= canvasSize.width) {
            return {
             x: x + elementSize.width * (dx * 0.5), 
              y: canvasSize.height + (elementSize.height / 2)
            };
        }
    }

    return position;
}