c++ - 以纵横比旋转二维 vector

标签 c++ vector rotation aspect-ratio

我们中的许多人都熟悉在给定角度 theta 的情况下围绕原点旋转 2D vector 的方法:

newX = x * cos(theta) - y * sin(theta);
newY = x * sin(theta) + y * cos(theta);

我现在尝试在图像 UV 空间中旋转坐标,它看起来像这样:

UV Space

(图片借自 this SO 问题。)

这里 u 轴的单位比 v 轴的单位宽,所以上面的方法导致坐标围绕椭圆而不是圆形旋转.我需要 vector 的旋转,就像坐标是正方形一样,这意味着需要考虑纵横比。我认为这就像将坐标拉伸(stretch)到正方形空间一样简单,旋转,然后向后拉伸(stretch),尽管看起来 vector 仍然在椭圆旋转:

newX = (x * cos(theta) * Aspect - y * sin(theta)) / Aspect;
newY = x * sin(theta) * Aspect + y * cos(theta);

感谢任何帮助,提前致谢!

最佳答案

旋转和纵横比的一般版本是:

(center_c, center_y) 为旋转中心

(aspect_x, aspect_y) 是 aspect_ratio

tmp_x = (x-center_x)/aspect_x
tmp_y = (y-center_y)/aspect_y
tmp_x = tmp_x * cos(theta) - tmp_y * sin(theta)
tmp_x = tmp_x * sin(theta) + tmp_y * cos(theta)
new_x = aspect_x*tmp_x-center_x
new_y = aspect_y*tmp_x-center_y

希望对您有所帮助。

关于c++ - 以纵横比旋转二维 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52141572/

相关文章:

c++ - std::runtime_error::runtime_error(const std::string&) 如何满足 std::exception 对 throw() 的要求?

c++ - 其参数作为模板非类型传递的可变参数函数模板

c++ - 将 std::vector<T> move 到 T*

c++ - 另一个 : Passing Vector of Structs to Function - C++, MinGW

c++ - std::vector 的性能差是因为没有调用 realloc 的对数次数吗?

iphone - iPad 上的 UIWebView 旋转

c++ - 为什么对一个 vector 进行多线程操作的速度很慢?

c++ - eekg() 函数失败

Java - 通过设置绘制立方体面的点的位置来设置旋转

java - 无法旋转简单的 OpenGL 三角形