在 AS3 (Flex) 中查找旋转矩形的右上角点

发布于 2024-08-30 17:19:34 字数 117 浏览 2 评论 0原文

我有一个任意宽度和高度的矩形。我知道 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 技术交流群。

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

发布评论

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

评论(2

妄断弥空 2024-09-06 17:19:34

让 Flash 的显示代码为您做这些事情通常是最简单、最快的。创建一个空 Sprite 并将其放在要跟踪的角点处的矩形显示对象内。然后,在您选择的坐标空间中找到该精灵的位置:

var p:Point = new Point(0,0);
myRectangle.myCornerSprite.localToGlobal( p );
someDisplayObject.globalToLocal( p ); // for a coord space besides the stage

这使您无需对矩形的设计(即注册点)做出任何假设,并且即使矩形应该倾斜或缩放以及旋转也可以工作。另外,这比一堆余弦之类的东西更容易实现和维护。

(请注意,上面的代码假设“右上角”指的是特定的角 - 如果您想检查此时出现在最右上角的角,我只需添加在所有四个角处使用精灵执行相同的操作,然后选择全局坐标中右上角的那个。)

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:

var p:Point = new Point(0,0);
myRectangle.myCornerSprite.localToGlobal( p );
someDisplayObject.globalToLocal( p ); // for a coord space besides the stage

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.)

长梦不多时 2024-09-06 17:19:34

您只需计算给定半径的圆上的点即可。矩形的中心将是圆的原点,任何角将是圆圆周上的一个点。您需要使用三角学来通过旋转计算新点。我现在没有时间解释这一切,但这里有一个我过去使用过的不错的 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

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