旋转圆向矢量(JS)

发布于 2025-01-25 02:36:58 字数 1104 浏览 4 评论 0 原文

我可能会急剧思考这个问题。但是我有一个用pi切片动态生成的圆圈。我希望能够单击圆并旋转它。我单击的圆上的任何点,我想跟随鼠标的那个点。

我已经玩了一段时间了。我可以让它在圆圈的一侧工作。但是圆圈的另一侧与我期望的相反。这基本上是设置

        const currentDegrees = piCircle.current;
        const rect = canvas.getBoundingClientRect();
        const x = state.xy[0] - rect.left;
        const y = state.xy[1] - rect.top;
        
        const dx = state.delta[0];
        const dy = state.delta[1];

X和Y是圆圈上的鼠标位置,DX和DY是鼠标正在移动的归一化运动(通常为1/0/-1)。电流定位是圆的当前旋转。

我目前有些工作的方法是这种
const degrees = todegrees(Math.atan2(y + dy,x + dx));

该方法似乎并不尊重圆的旋转。在圆的右侧它可以正常工作,在左侧,运动是倒置的。

为了简化这个问题,我意识到我可以更像这样。我有两个向量,鼠标位置(x& y)和delta(x& y)。通过将这两个添加在一起,我将有一个第三个向量,这是我希望在施加一定程度的旋转时最终位置的目的地。

    const x = state.xy[0] - rect.left;
    const y = state.xy[1] - rect.top;
    const dx = x + state.delta[0];
    const dy = y + state.delta[1];

我也很糟糕地帮助可视化我的问题 ”在此处输入图像说明”

I'm probably drastically overthinking this. But I have a circle I am dynamically generating with slices of PI. I would like to have the ability to click the circle and rotate it. Whatever point on the circle I click, That point on the circle is the one I would like to follow the mouse.

I've been playing around with it for awhile. And I can get it to work on one side of the circle. But the other side of the circle does the opposite of what I expect. Heres basically the setup

        const currentDegrees = piCircle.current;
        const rect = canvas.getBoundingClientRect();
        const x = state.xy[0] - rect.left;
        const y = state.xy[1] - rect.top;
        
        const dx = state.delta[0];
        const dy = state.delta[1];

x and y are the mouse positions on the circle, dx and dy is the normalized motion the mouse is moving in(typically 1/0/-1). currentDegrees is the current rotation of the circle.

My current, somewhat working approach was this
const degrees = toDegrees(Math.atan2(y + dy, x + dx));

That approach doesnt seem to respect the rotation of the circle. on the right side of the circle it works fine, on the left the motion is inverted.

To simplify the issue, I realize I can phrase it more like this. I have two vectors, mouse position(x&y) and delta(x&y). By adding those two together i would have a third vector which is the destination I would like my initial position to end up at when I apply some degree of rotation.

    const x = state.xy[0] - rect.left;
    const y = state.xy[1] - rect.top;
    const dx = x + state.delta[0];
    const dy = y + state.delta[1];

I drew an awful thing to also help visualize my problementer image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉薄对峙 2025-02-01 02:36:59

这是我如何做到这一点的最佳猜测。它不是完美的。圆旋转比指针稍慢。我不确定如何改进它。我愿意接受建议。

        const x = state.xy[0] - rect.left;
        const y = state.xy[1] - rect.top;
        
        const dx = x + state.delta[0];
        const dy = y + state.delta[1];

        const startAngle = Math.atan2(y - centerY , x - centerX);
        const endAngle = Math.atan2(dy - centerY, dx - centerX)

        const cx = centerX + radius * Math.cos(startAngle);
        const cy = centerY + radius * Math.sin(startAngle);

        const dcx = centerX + radius * Math.cos(endAngle);
        const dcy = centerY + radius * Math.sin(endAngle);

        const rsa = Math.atan2(centerY - dcy, centerX - dcx);
        const rea = Math.atan2(centerY - cy, centerX - cx);

        const angle = toDegrees(rsa - rea)

This is my best guess for how to do this. Its not perfect. The circle rotates slightly slower than the pointer. I'm not sure how to improve it. I'm open to suggestions.

        const x = state.xy[0] - rect.left;
        const y = state.xy[1] - rect.top;
        
        const dx = x + state.delta[0];
        const dy = y + state.delta[1];

        const startAngle = Math.atan2(y - centerY , x - centerX);
        const endAngle = Math.atan2(dy - centerY, dx - centerX)

        const cx = centerX + radius * Math.cos(startAngle);
        const cy = centerY + radius * Math.sin(startAngle);

        const dcx = centerX + radius * Math.cos(endAngle);
        const dcy = centerY + radius * Math.sin(endAngle);

        const rsa = Math.atan2(centerY - dcy, centerX - dcx);
        const rea = Math.atan2(centerY - cy, centerX - cx);

        const angle = toDegrees(rsa - rea)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文