将任意梯度旋转 90 度的算法

发布于 2024-11-25 22:14:48 字数 1601 浏览 2 评论 0原文

我正在尝试一些基本的光线追踪,并且有一个 2D 渐变(称为 dybydx)。我从正方形中心追踪,0.5,0.5,并希望设置垂直于梯度的额外轨迹以增加观察视野(约<0.5)。我对 fp 算术相当陌生,这在我调试时引起了一些头痛。

我希望下面的代码可以解释其余的内容:

        if (incX) {
        if (incY) {
            if (cclockwise) {
                x -= System::Math::Sin(theta) / 2;
                y += System::Math::Cos(theta) / 2;
            } else {
                x += System::Math::Sin(theta) / 2;
                y -= System::Math::Cos(theta) / 2;
            }
        } else {
            if (cclockwise) {
                x += System::Math::Cos(theta) / 2;
                y += System::Math::Sin(theta) / 2;
            } else {
                x -= System::Math::Cos(theta) / 2;
                y -= System::Math::Sin(theta) / 2;
            }
        }
    } else {
        if (incY) {
            if (cclockwise) {
                x -= System::Math::Cos(theta) / 2;
                y -= System::Math::Sin(theta) / 2;
            } else {
                x += System::Math::Cos(theta) / 2;
                y += System::Math::Sin(theta) / 2;
            }
        } else {
            if (cclockwise) {
                x += System::Math::Sin(theta) / 2;
                y -= System::Math::Cos(theta) / 2;
            } else {
                x -= System::Math::Sin(theta) / 2;
                y += System::Math::Cos(theta) / 2;
            }
        }
    }

我已经在纸上绕了一圈又一圈的象限,但我忘记了 Windows 反转了传统的 y 轴(因此我认为是顺时针的不是,但这是一个任意错误并且不重要) 。我真正想要的是一种万无一失的方法,可以将我的梯度旋转 90 度。谢谢。

编辑 - theta 是梯度形成的从水平到 +ve y 轴的角度(在纸上)。

编辑 - incX 和 incY 表示原始梯度(真的,真的)分别在 X 和 Y 上增加。

I'm attempting some rudimentary ray tracing and I have a 2D gradient (called dybydx). I trace from the centre of a square, 0.5, 0.5, and would like to set additional traces normal to the gradient to increase the field of observation (by ~< 0.5). I'm fairly new to fp arithmetic and this is causing some head scratching when I debug.

I hope the following code explains the rest:

        if (incX) {
        if (incY) {
            if (cclockwise) {
                x -= System::Math::Sin(theta) / 2;
                y += System::Math::Cos(theta) / 2;
            } else {
                x += System::Math::Sin(theta) / 2;
                y -= System::Math::Cos(theta) / 2;
            }
        } else {
            if (cclockwise) {
                x += System::Math::Cos(theta) / 2;
                y += System::Math::Sin(theta) / 2;
            } else {
                x -= System::Math::Cos(theta) / 2;
                y -= System::Math::Sin(theta) / 2;
            }
        }
    } else {
        if (incY) {
            if (cclockwise) {
                x -= System::Math::Cos(theta) / 2;
                y -= System::Math::Sin(theta) / 2;
            } else {
                x += System::Math::Cos(theta) / 2;
                y += System::Math::Sin(theta) / 2;
            }
        } else {
            if (cclockwise) {
                x += System::Math::Sin(theta) / 2;
                y -= System::Math::Cos(theta) / 2;
            } else {
                x -= System::Math::Sin(theta) / 2;
                y += System::Math::Cos(theta) / 2;
            }
        }
    }

I've been round and round the quadrants on paper but I forgot Windows reverses the conventional y-axis (hence what I thought was clockwise isn't, but that is an arbitrary error and unimportant). What I really would like, is a fool-proof way to rotate my gradient 90 degrees either way. Thanks.

edit-- theta is the angle from horizontal to the +ve y axies that the gradient makes (on paper).

edit-- incX and incY mean the original gradient (really, really) is increasing in X and Y respectively.

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

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

发布评论

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

评论(2

凉城已无爱 2024-12-02 22:14:48

如果你用 @dario_ramos 方程替换 90 度,你会得到:

x' = -y
y' = x

顺便说一句,如果你在格子纸上画过这个,你就会明白为什么它如此微不足道。

if you substitue 90 degrees in @dario_ramos equation you get:

x' = -y
y' = x

Btw if you ever draw this on a checked paper you'll see why it's so trivial.

场罚期间 2024-12-02 22:14:48

一般公式是:

x' = x cos(theta) - y sin(theta)
y' = x sin(theta) + y cos(theta)

您不需要使用方向标志:顺时针或逆时针的事实由 theta 符号表示,您可以定义它。例如,定义正 θ 表示逆时针方向。该公式考虑了两种情况

编辑:如果您只想旋转 90 度,请使用 yi_H 的解决方案;它更高效,因为相比之下 cos() 和 sin() 的 cpu 成本较高

The general formula is:

x' = x cos(theta) - y sin(theta)
y' = x sin(theta) + y cos(theta)

You don't need to use a flag for the orientation: the fact that it is clockwise or counterclockwise is represented by the sign of theta, you define that. For example, define that a positive theta means counterclokwise. The formula considers both cases

Edit: Go with yi_H's solution if you only wish to rotate 90 degrees; it's more efficient, since cos() and sin() are cpu-expensive in comparison

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