I was wondering: how do i find out what is the hightest point ( the point with the highest Y coordinate ) in a rectangle that can rotate? and vice versa the one that is nearest to the origin of the stage? ( considering the Y axis ).

( actionscript 3.0 )

# Calculate the point with the max heigh.

Page 1 of 1## 6 Replies - 3537 Views - Last Post: 10 May 2014 - 11:07 PM

##
**Replies To:** Calculate the point with the max heigh.

### #2

## Re: Calculate the point with the max heigh.

Posted 11 April 2014 - 11:50 AM

Sounds like some geometry. If you know the center point and the size of the rectangle, and the rotation in degrees or radians, then there should be a formula to find out where the 4 vertices are. There's a similar question here:

http://stackoverflow...from-its-center

https://www.google.c...=UTF-8&oe=UTF-8

http://stackoverflow...from-its-center

https://www.google.c...=UTF-8&oe=UTF-8

### #3

## Re: Calculate the point with the max heigh.

Posted 13 April 2014 - 03:35 AM

Is there a way to make a tiny squadre inside the rectangle ( in the corners ), so that it will move with it?

If i'm not wrong an object can have objects in itself.

Then i can use .alpha property to make the squadre invisible.

If i'm not wrong an object can have objects in itself.

Then i can use .alpha property to make the squadre invisible.

### #4

## Re: Calculate the point with the max heigh.

Posted 13 April 2014 - 06:02 AM

I tried this, but the positions are not the one i set but they are slighty wrong,

If i check, it returns the trace.

Only topRight is at the correct position...

this.topRight.x=player.width/2 this.topRight.y=player.height/2 this.topleft.x=-(player.width/2) this.topleft.y=player.height/2 this.bottomLeft.x =-(player.width/2) this.bottomLeft.y=-(player.height/2) this.bottomright.x=player.width/2 this.bottomright.y=-(player.height/2)

If i check, it returns the trace.

if ( this.bottomright.x<player.width/2 || this.bottomright.x>player.width/2 && this.bottomright.y>-player.height/2 || this.bottomright.y<-player.height/2) { trace("cheazzosuccede") }

Only topRight is at the correct position...

### #5

## Re: Calculate the point with the max heigh.

Posted 14 April 2014 - 03:21 PM

What are they actually set to, and what should they be set to?

### #6

## Re: Calculate the point with the max heigh.

Posted 15 April 2014 - 04:44 AM

For no senseful reason, i'm an idiot. I wanted to use those squares to know where the verticles are...

I'm just trying to make the rectangle don't pass beyond the walls, sadly the rectangle can rotate, so it's more complex...

I'm just trying to make the rectangle don't pass beyond the walls, sadly the rectangle can rotate, so it's more complex...

### #7

## Re: Calculate the point with the max heigh.

Posted 10 May 2014 - 11:07 PM

This can be solved with a little trigonometry. From what you describe there may be some missing information.

We need the height and width of the rectangle (h,w)

We need the origin of the rotation (the point where the rectangle rotates around) relative to the rectangles h,w. So if its rotating around the top left the origin would be ox= 0, oy= 0. If it rotates around the center it would be ox = w/2, oy = h/2.

Then we need the coordinates of the origin in the stage. sx,sy

And last we need the actual amount that it has been rotated by. Ind degrees or radians rot = ?

So we need

We are going to use the trig functions sin and cos to create a matrix. We need to know if the trig functions use radians or degrees.

I will assume radians so lets convert 45 degrees to radians. There are 2Pi radians for 360 degrees. So find the fraction of 360 that 45 is

Now we must apply the fraction to 2PI or 2*3.141592 so let put it in all together in one line

So that's the final formula to convert from degrees to radians

Now we want the matrix that will transform the rect to the rotated coordinates. It comprise of two vectors. One pointing along the x coordinate and one pointing along the y.

A vector as a line that starts at x=0, y=0 and has a distance in both x and y. We ignore the 0,0 and only need the x,y distance. With out any rotation the vector for the width or X for the rect is width pixels across and 0 pixels down (w,0) if we were to rotate the rect 90 clockwise then the width would point down along y and there would be no x movement thus (0,w).

We need a special type of vector that we call a unit vector. That is a vector that always has a length equal to 1. So for 0Deg the x vector is (1,0) one across zero down and for y (0,1) zero across one down.

We write it like this.

or for code we can assign vars

Thas was the transform for a rotation of zero degrees. For 90deg clock wise it would be

Almost there. Lets apply the matrix to the width and the height, ignoring the origin for now.

We multiply the width to the x vector and the height to the y vector

now to move from the top left to the bottom right we add these vectors to the top right

Ok I hope you are still with me.

So now we use sin and cos as they provide a very easy way to create our unit vectors

// now for the height vector which is at 90deg or Pi/2 = +(3.14159/2) to rotRad

Now we need the origin of rotation. To get the x distance from the origin to the left of the rect we subtract the width from the origin x (ox).

Now all we do is add what we need together and then add the stage coords of the origin and we can get the 4 corners. So lets do it all in one function;

Sorry.. It has been so long since I have used flash that I can't remember the syntax so I will write it in Javascript sudo code. If you don't understand it maybe someone else can convert it for you.

I hope I did not make a typo or mistake but I also hope I gave you enough info to see what is going on and adapt this to your needs. You could also see if flash has a transform point function, as it would be very handy.

Good luck. Maybe a bit late but you never know.

We need the height and width of the rectangle (h,w)

We need the origin of the rotation (the point where the rectangle rotates around) relative to the rectangles h,w. So if its rotating around the top left the origin would be ox= 0, oy= 0. If it rotates around the center it would be ox = w/2, oy = h/2.

Then we need the coordinates of the origin in the stage. sx,sy

And last we need the actual amount that it has been rotated by. Ind degrees or radians rot = ?

So we need

w = 100; // width h = 400; // height ox = w/2; // the center of the rect oy = h/2; // will be the point of rotation. sx = 500; // this is the location of the sy = 500; // origin on the stage rot = 45; // the rotation in degrees

We are going to use the trig functions sin and cos to create a matrix. We need to know if the trig functions use radians or degrees.

I will assume radians so lets convert 45 degrees to radians. There are 2Pi radians for 360 degrees. So find the fraction of 360 that 45 is

*rotFrac = rot/360; // that is equal to 0.125*Now we must apply the fraction to 2PI or 2*3.141592 so let put it in all together in one line

*rotRad = (rot/360)*2*3.141592; // lets simplify*

rotRad = (rot/360)*6.28318; // 2*Pi

rotRad = rot*0.01745; // (2*Pi)/360rotRad = (rot/360)*6.28318; // 2*Pi

rotRad = rot*0.01745; // (2*Pi)/360

So that's the final formula to convert from degrees to radians

Now we want the matrix that will transform the rect to the rotated coordinates. It comprise of two vectors. One pointing along the x coordinate and one pointing along the y.

A vector as a line that starts at x=0, y=0 and has a distance in both x and y. We ignore the 0,0 and only need the x,y distance. With out any rotation the vector for the width or X for the rect is width pixels across and 0 pixels down (w,0) if we were to rotate the rect 90 clockwise then the width would point down along y and there would be no x movement thus (0,w).

We need a special type of vector that we call a unit vector. That is a vector that always has a length equal to 1. So for 0Deg the x vector is (1,0) one across zero down and for y (0,1) zero across one down.

We write it like this.

*[1,0]*

[0,1][0,1]

or for code we can assign vars

*vecXx = 1; // x vector (x,y)*

vecXy = 0;

vecYx = 0; // y vector (x,y)

vecYy = 1;vecXy = 0;

vecYx = 0; // y vector (x,y)

vecYy = 1;

Thas was the transform for a rotation of zero degrees. For 90deg clock wise it would be

*[0,1] // move x down along y*

[1,0] // move y across along x.[1,0] // move y across along x.

Almost there. Lets apply the matrix to the width and the height, ignoring the origin for now.

We multiply the width to the x vector and the height to the y vector

vecXx = vecXx * width; // 100*1 = 100 vecXy = vecXy * width; // 100*0 = 0 vecYx = vecYx * height; // 400*0 = 0 vecYy = vecYy * height; // 400*1 = 400

now to move from the top left to the bottom right we add these vectors to the top right

botRightX = topLeftx + vecXx + vecYx; botRightY = topLefty + vecXy + vecYy;

Ok I hope you are still with me.

So now we use sin and cos as they provide a very easy way to create our unit vectors

vecXx = cos(rotRad); // get the vector along width for x vecXy = sin(rotRad); // and y

// now for the height vector which is at 90deg or Pi/2 = +(3.14159/2) to rotRad

vecYx = cos(rotRad+(3.14159/2)); // get the vector along height for x vecYy = sin(rotRad+(3.14159/2)); // and y

Now we need the origin of rotation. To get the x distance from the origin to the left of the rect we subtract the width from the origin x (ox).

distToLeft = ox-width distToRight = width+(width-ox); distToTop = oy-height; distToBot = heigth+(height - oy);

Now all we do is add what we need together and then add the stage coords of the origin and we can get the 4 corners. So lets do it all in one function;

Sorry.. It has been so long since I have used flash that I can't remember the syntax so I will write it in Javascript sudo code. If you don't understand it maybe someone else can convert it for you.

function rotateRec(w,h,ox,oy,sx,sy,rot){ // w = width // h = height // ox = origin x(rotation point) // oy = origin y // sx = stage x // sy = stage y // rot = the rotation in degree // convert rot to radians rot = rot*0.01745; // create the matrix (the two vectors for width and height) vXx = cos(rot); // get the vector along width for x vXy = sin(rot); // and y vYx = cos(rot+(3.14159/2)); // get the vector along height for x vYy = sin(rot+(3.14159/2)); // and y // get the distance to the sides from the origin left = width-ox; // dist to left of rect right = width+left; // dist to right side top = oy-height; // dist to top of rec bot = heigth+top; // dist to bot or rec // calculate the 4 corners // origin plus the dist to the side the up or down // then add the stage location topLeftX = ox + left* vXx + top*vYx + sx; topLeftY = oy + left* vXy + top*vYy + sy; botRightX = ox + right*vXx + bot*vYx + sx; botRightY = oy + right*vXy + bot*vYy + sy; botLeftX = ox + left* vXx + bot*vYx + sx; botLeftY = oy + left* vXy + bot*vYy + sy; topRightX = ox + right*vXx + top*vYx + sx; topRightY = oy + right*vXy + top*vYy + sy; // now you have the 4 corner positions of the rectangle rotated // around a point ox,oy relative to the top left of the rect // and move to into position on the stage. }

I hope I did not make a typo or mistake but I also hope I gave you enough info to see what is going on and adapt this to your needs. You could also see if flash has a transform point function, as it would be very handy.

Good luck. Maybe a bit late but you never know.

This post has been edited by **Blindman67**: 10 May 2014 - 11:11 PM

Page 1 of 1