I have the coordinates of the top left point of a rectangle as well as its width, height and rotation from 0 to 180 and -0 to -180.
I am trying to get the bounding coordinates of the actual box around the rectangle.
What is a simple way of calculating the coordinates of the bounding box
- Min y, max y, min x, max x?
The A point is not always on the min y bound, it can be anywhere.
I can use matrix the transform toolkit in as3 if needed.
The method outlined by MarkusQ works perfectly but bear in mind that you don't need to transform the other three corners if you have point A already.
An alternative method, which is more efficient, is to test which quadrant your rotation angle is in and then simply compute the answer directly. This is more efficient as you only have a worst case of two if statements (checking the angle) whereas the other approach has a worst case of twelve (6 for each component when checking the other three corners to see if they are greater than the current max or less than the current min) I think.
The basic algorithm, which uses nothing more than a series of applications of Pythagoras' theorem, is shown below. I have denoted the rotation angle by theta and expressed the check there in degrees as it's pseudo-code.
This approach assumes that you have what you say you have i.e. point A and a value for theta that lies in the range [-180, 180]. I've also assumed that theta increases in the clockwise direction as that's what the rectangle that has been rotated by 30 degrees in your diagram seems to indicate you are using, I wasn't sure what the part on the right was trying to denote. If this is the wrong way around then just swap the symmetric clauses and also the sign of the st terms.
I am not sure I understand, but a compound transformation matrix will give you the new co-ordinates for all points concerned. If you think the rectangle may spill over the imagable area post transformation apply a clipping path.
In case you are unfamiliar with the exact definition of the matrices take a look here.
Here are three functions from my open source libraries. The functions are fully tested in Java but the formulae can be easily translated to any language.
The signatures are:
public static float getAngleFromPoint(final Point centerPoint, final Point touchPoint)
public static float getTwoFingerDistance(float firstTouchX, float firstTouchY, float secondTouchX, float secondTouchY)
Point getPointFromAngle(final double angle, final double radius)
This solution assumes that the pixel density is evenly spaced. Before rotating the object do the following:
Use getAngleFromPoint to calculate the angle from the center to the upper right corner (lets say this returns 20 degrees) meaning that the upp left corner is -20 degrees or 340 degrees.
Use the getTwoFingerDistance to return the diagonal distance between the center point and the upper right corner (this distance should obvoiusly be the same to all corners, This distance will be used in the next calculation).
Now lets say we rotate the object clockwise by 30 degrees. We now know that the upper right corner must be at 50 degrees and the upper left corner is at 10 degrees.
You should now be able to use the getPointFromAngle function on the upper left and upper right corner. using the radius returned from step 2. The X position multiplied by 2 from the upper right corner should give you the new width and the Y position times 2 from the upper left corner should give the the new height.
These above 4 steps should be put into conditions based upon how far you have rotated your object other wise you may return the height as the width and the width as the height.
Bare in mind the angle functions are expressed in factors of 0-1 instead of 0-360 (just multiply or divide by 360 where appropriate):
//Gets an angle from two points expressed as a factor of 0 -1 (0 being 0/360, 0.25 being 90 degrees etc)
//Measures the diagonal distance between two points
//Get XY coordinates from an angle given a radius (The angle is expressed in a factor of 0-1 0 being 0/360 degrees and 0.75 being 270 etc)
These code snippets are from my open source libraries: https://bitbucket.org/warwick/hgdialrepo and https://bitbucket.org/warwick/hacergestov2. One is a gesture library for Android and the other is a dial control for Android. There is also an OpenGLES 2.0 implementation of the dial control at: https://bitbucket.org/warwick/hggldial
Apply the rotation matrix to your corner points. Then use the minimum/maximum respectively of the obtained x,y coordinates to define your new bounding box.
I used Region for First rotating the rectangle and then use that rotated region to detect that rectangle
now to detecting that rectangle