0

I have a rotated rectangle that I don't know the original width and height of. As the current width and height is just the bounding box encapsulating the rectangle, how would I find the actual width and height? thanks.

Calender Man
  • 23
  • 1
  • 8
  • cant you unrotate it, measure width and height, then rotate back into position? –  Feb 08 '13 at 18:56
  • I was thinking of doing that, but I don't have any of the corners of the actual rectangle so I cant rotate them back. The only things I have are the mid point and the bounds of the bounding box. – Calender Man Feb 08 '13 at 19:55
  • If you dont know the angle of rotation, it will be complicated to calculate true bounds of rectangle. you may need to get help from http://math.stackexchange.com/ –  Feb 08 '13 at 20:40
  • I know the angle of rotation, I just don't have the corners to rotate back by that angle. Thanks for the link, I'll try asking there. – Calender Man Feb 08 '13 at 20:45
  • dont understand what you mean by "dont have the corners" –  Feb 08 '13 at 20:47
  • To rotate the rectangle back to its original state, you need a point on the rectangle. I have the mid point so I can rotate that back, but since I don't know the co-ordinates of the Top Left and Bottom Right corners of the rotated rectangle, I cant rotate them back to get the width and height from them. – Calender Man Feb 08 '13 at 20:53
  • what form does your rectangle take? is it a display object or a variable? Can you post some code? –  Feb 08 '13 at 20:59
  • Its a Rectangle created when there's a collision between two display objects, one of these display objects is rotated, so the collision rectangle ends up also being rotated. And that's why I never know the original height and width of the rectangle as it was never rotated in the fist place. – Calender Man Feb 08 '13 at 21:04

1 Answers1

0

You can use DisplayObject's getBounds()/getRect() and pass the display object itself as a parameter. e.g. if yourClip is rotated, you try this:

trace(yourClip.getBounds(yourClip));.

It should be pretty close to the original dimensions. This works assuming you're using display objects (Sprites, MovieClips, etc.). Not sure if it helps, but Mike Chambers' collision detection articles are pretty good.

In a comment you mentioned "Rectangle". Would that be flash.geom.Rectangle ? If you prefer using geometry classes only, I suppose you could be using flash.geom.Matrix instances to keep track of coordinate systems. Transforming the corner points of the rectangle using Matrix's transformPoint() should be trivial. Also, you can move back and forth between coordinate systems using matrices again (and the inverted versions as well). Still the DisplayObject class offers quite a few handy functions so you wouldn't need to do the matrix math yourself.

George Profenza
  • 50,687
  • 19
  • 144
  • 218
  • Thanks that's a pretty cool trick, but yeah the collision rectangle is a flash.geom.Rectangle that's made when a collision has occurred. Basically, my problem is I want to know which side of the rectangle was collided against. So I'm using the collision rectangle's bounds, for example, if it was a collision against the top I would check if the rectangle.top was between the y co-ords of the display objects top left and top right y co-ords. Since there's overlaps using that method, I was trying to find a point on the collision rectangle rather than the bounds of it. – Calender Man Feb 09 '13 at 00:11