Click here to Skip to main content
14,667,947 members
Rate this:
Please Sign up or sign in to vote.
See more:
Dear All

I draw a rotated rectangle (angle 45) in a picture box. i know the origin point (Left Top Corner) is (x=5,y=15) of the rectangle; i want the the end point (Right Corner). any one help me.
Posted
Updated 3-May-11 22:26pm
v2
Comments
CodeHawkz 3-May-11 2:00am
   
:) U said you drew a rotated rectangle, which means you must have had the co-ordinates of the right-bottom corner when you drew it. Why don't you save it and re-use? Or if this is a custom component that we are talking about, can't store it as a property of the component?
Sandeep Mewara 3-May-11 2:38am
   
What help?

Rate this:
Please Sign up or sign in to vote.

Solution 2

A rectangle consists of 4 points. The one which is 'bottom right' may change depending on the angle (and at a 45° rotation for a square, it is ambiguous which one is most 'bottom right').

The location of the point which was originally at the bottom right is (w, h) in local coordinates. The local axes become x'=(cos a, sin a) and y'=(-sin a, cos a) so the location of that point becomes (w cos a - h sin a, w sin a + h cos a), with (w,h) = the original size of the rectangle and a = the angle through which you have rotated it (anticlockwise).

You can do the same thing for the other three points, whose local coordinates relative to the top left (you state that this is the origin) are (0,0), (w,0) and (0,h), meaning that their rotated locations are:
TL = (0, 0)
TR = (w cos a, w sin a)
BL = (-h sin a, h cos a)

You can then find out which is the most 'bottom right' by finding which has the largest dot product with the direction vector to specify 'bottom-rightness' which is (1, 1)* – which is simply the sum of coordinates, but you should understand why.

*: Actually the standard direction vector is norm(1,1) = (0.707, 0.707), but as we are comparing values to each other, the rescaling doesn't matter in this case.
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

This is a math question, I like :) .
The trick is to find the distance between center and lower right corner.
Then using Cosinus and Sinus the transformation of X and Y position can be calculated.
Sinus transforms an angle (in radians) to a floating number between 1 (90 degrees) and -1 (270 degrees).
Cosinus does the same but is 1 at 0 degrees and -1 at 180 degrees.
A possible mistake may lie in calculating 'Radius' though I believe I did it pretty well.

//Some declarations, sort of 'default settings', these should only be calculated when the rectangle is initialised or resized.
float defaultAngle = 45; //The default Angle so calculations still point to the lower right corner when it hasn't been rotated yet.
PointF center = new PointF(width/2,height/2);
float Radius = Math.Sqrt(Math.Pow(width - Center.X,2) + Math.Pow(height - Center.Y)); // Distance between the center and the lower right corner.

//The point calculations, to be done whenever the rectangle is being rotated.
fullAngle = defaultAngle + yourAngle; //yourAngle is the rotation of the rectangle.
PointF BottomRightCorner = new PointF(0,0); //Point in which we will store the 
BottomRightCorner.X = center.X + Math.Cos(Radians(fullAngle)) * Radius; // The parameter for Math.Cos requires the angle in radians.
BottomRightCorner.Y = center.Y + Math.Sin(Radians(fullAngle)) * Radius; //Same goes for Math.Sin .
//BottomRightCorner should now contain the position of the bottom right corner.

--
float Radians(float Degrees)
{
 return Degrees * (Math.PI /180); //Simple conversion form degrees to radians.
}
   
v5
Comments
abdul kamaal naser 3-May-11 4:05am
   
above not work properly
Groulien 3-May-11 4:20am
   
Where did the point end up being?Was it too far to the right, too low?
I can't use VS right now, but still rather eager to get it (this code) working.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100