在 AS3 (Flex) 中查找旋转矩形的右上角点
我有一个任意宽度和高度的矩形。我知道 X、Y、宽度和高度。如何求解矩形旋转N度时的右上角坐标?我意识到如果它是轴对齐的我会简单地求解(x,y +宽度)。不幸的是,当我在矩形上应用变换矩阵以使其绕其中心旋转时,这并不成立。
I have a rectangle of any arbitrary width and height. I know X,Y, width, and height. How do I solve the upper right hand coordinates when the rectangle is rotated N degrees? I realized if it were axis aligned I would simply solve for (x,y+width). Unforunatly this doesn't hold true when I apply a transform matrix on the rectangle to rotate it around its center.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让 Flash 的显示代码为您做这些事情通常是最简单、最快的。创建一个空 Sprite 并将其放在要跟踪的角点处的矩形显示对象内。然后,在您选择的坐标空间中找到该精灵的位置:
这使您无需对矩形的设计(即注册点)做出任何假设,并且即使矩形应该倾斜或缩放以及旋转也可以工作。另外,这比一堆余弦之类的东西更容易实现和维护。
(请注意,上面的代码假设“右上角”指的是特定的角 - 如果您想检查此时出现在最右上角的角,我只需添加在所有四个角处使用精灵执行相同的操作,然后选择全局坐标中右上角的那个。)
It's usually easiest and fastest to let Flash's display code do these kinds of things for you. Create an empty Sprite and put it inside the rectangle's display object at the corner you want to track. Then, find the location of that sprite in the coordinate space of your choice:
This gets you out of making any assumptions about the rectangle's design (i.e. registration point), and works even if the rectangle should be skewed or scaled as well as being rotated. Plus, this will be much easier to implement and maintain then a mess of cosines and whatnot.
(Note that the code above assumes that "upper right" refers to a specific corner - if you want to examine whichever corner happens to upper-rightmost at the moment, I'd simply add do the same thing with a sprite at all four corners, and pick whichever is to the upper right in global coords.)
您只需计算给定半径的圆上的点即可。矩形的中心将是圆的原点,任何角将是圆圆周上的一个点。您需要使用三角学来通过旋转计算新点。我现在没有时间解释这一切,但这里有一个我过去使用过的不错的 2D Javascript 库的链接,它应该为您提供所需的一切(请记住,数学实际上是相同的)在 Javascript 和 ActionScript 中)自己解决。
http://jsdraw2d.jsfiction.com/viewsourcecode.htm
You just have to calculate the point on a circle for the given radius. The center of your rectangle will be the circle's origin and any corner will be a point on the circle's circumference. You need to use trigonometry to calculate the new point using the rotation. I don't have time right now to explain all this, but here is a link to a decent 2D Javascript library I've used in the past and which should give you everything you need (bearing in mind that the math is virtually the same in Javascript and ActionScript) to work it out for yourself.
http://jsdraw2d.jsfiction.com/viewsourcecode.htm