math - 绘制旋转矩形

标签 math graphics

我意识到这可能更像是一个数学问题。

要为我的矩形绘制线条,我需要解决它们的角。
我在 (x,y) 处有一个矩形中心,具有定义的宽度和高度。



在顶部的非旋转矩形上找到蓝点(角度 = 0)
这是
UL = (x-Width/2),(y+height/2)UR = (x+Width/2),(y+height/2)LR = (x+Width/2),(y-height/2)LL = (x-Width/2),(y-height/2)
如果角度不是 0,我如何找到点?

提前致谢。

更新:虽然我在我的图片中有 (0,0) 作为中心点,但中心点很可能不在那个位置。

最佳答案

首先将中心点变换为 0,0

X' =​​ X-x

Y' = Y-y

然后旋转一个角度A

X'' = (X-x) * cos A - (Y-y) * sin A

Y'' = (Y-y) * cos A + (X-x) * sin A

再次将中心点变换回 x,y

X''' = (X-x) * cos A - (Y-y) * sin A + x

Y''' = (Y-y) * cos A + (X-x) * sin A + y

因此,通过以下变换计算 (X,Y) 的所有 4 个点

X''' = (X-x) * cos A - (Y-y) * sin A + x

Y''' = (Y-y) * cos A + (X-x) * sin A + y

其中 x, y 是矩形的中心点,X,Y 是角点
正如我在评论中给出的那样,当角度为 0 时,您甚至没有正确定义角点。

替换后你会得到

UL  =  x + ( Width / 2 ) * cos A - ( Height / 2 ) * sin A ,  y + ( Height / 2 ) * cos A  + ( Width / 2 ) * sin A
UR  =  x - ( Width / 2 ) * cos A - ( Height / 2 ) * sin A ,  y + ( Height / 2 ) * cos A  - ( Width / 2 ) * sin A
BL =   x + ( Width / 2 ) * cos A + ( Height / 2 ) * sin A ,  y - ( Height / 2 ) * cos A  + ( Width / 2 ) * sin A
BR  =  x - ( Width / 2 ) * cos A + ( Height / 2 ) * sin A ,  y - ( Height / 2 ) * cos A  - ( Width / 2 ) * sin A

我认为这适合您的解决方案。

关于math - 绘制旋转矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/644378/

相关文章:

node.js - 如何在Three.js中从欧拉得到原始旋转四元数

php - 通过简化查询将字母拆分为组合 block

.net - Graphics.DrawRectangle(Pen,RectangleF)

android - 为什么在动画监听器中调用 .cancel() 会导致 StackOverflowError?

graphics - 构建轻量级跨平台文本编辑器的方法

c++ - 片段着色器中设置的颜色未显示 GLSL 1.30

math - 检测数据流中的周期性重复

java - 为什么我的程序不能访问 Java 中的数学方法?

c++ - 处理 C++ 角度中的符号变化

python - 以非常高的质量在 Python 中保存图像