﻿ Rotation- How does it work??
26 May, 2013, 02:00:25
 OmnomIRC You must Register, be logged in and have at least 40 posts to use this shout-box! If it still doesn't show up afterward, it might be that OmnomIRC is disabled for your group or under maintenance.Note: You can also use an IRC client like mIRC, X-Chat or Mibbit to connect to an EFnet server and #omnimaga.

 Pages: [1] 2 3   Go Down
 Author Topic: Rotation- How does it work?? -  (Read 1040 times) 0 Members and 1 Guest are viewing this topic.
nxtboy III

Offline

Last Login: 22 May, 2013, 02:53:41
Date Registered: 30 March, 2011, 22:57:58
Posts: 784

Topic starter
Total Post Ratings: +23

 « on: 12 March, 2012, 23:15:24 » 0

Hi,

If I had a polygon with a set of points, and I wanted to rotate the polygon from the center, how would I do that?
What sort of equation would I use?
Let X represent the X axis of a point and Y represent the y axis of a point on the polygon.
Also pretend it would be rotating around point (50,50).

Thanks, and have a nice day,
nxtboy III
 Logged

3rik
LV3 Member (Next: 100)

Offline

Gender:
Last Login: 02 April, 2013, 22:45:02
Date Registered: 09 August, 2011, 04:04:54
Location: Right over there.
Posts: 92

Total Post Ratings: +8

 « Reply #1 on: 12 March, 2012, 23:17:00 » 0

Is this the type of thing you're looking for?

http://en.wikipedia.org/wiki/Rotation_(mathematics)
 « Last Edit: 13 March, 2012, 05:19:44 by 3rik » Logged

Userbars
nxtboy III

Offline

Last Login: 22 May, 2013, 02:53:41
Date Registered: 30 March, 2011, 22:57:58
Posts: 784

Topic starter
Total Post Ratings: +23

 « Reply #2 on: 12 March, 2012, 23:20:01 » 0

Do you think you could explain it?
 Logged

3rik
LV3 Member (Next: 100)

Offline

Gender:
Last Login: 02 April, 2013, 22:45:02
Date Registered: 09 August, 2011, 04:04:54
Location: Right over there.
Posts: 92

Total Post Ratings: +8

 « Reply #3 on: 12 March, 2012, 23:21:59 » 0

It's with polygons right? Do want an explanation of why it works or how to work it?

It will take a few minutes for me to prepare everything.
 « Last Edit: 12 March, 2012, 23:34:50 by 3rik » Logged

Userbars
Yeong
Eternally Young Scarlet Moon
LV12 Extreme Poster (Next: 5000)

Offline

Gender:
Last Login: 20 May, 2013, 01:44:48
Date Registered: 15 October, 2010, 04:29:49
Location: Arden, NC
Posts: 3694

Total Post Ratings: +260

 « Reply #4 on: 13 March, 2012, 03:08:10 » 0

easiest way to do is using sine/cosine to rotate the points and draw the lines according to that points.
 Logged

Project Redemption....

My project progresses:HERE
My Pastebin stuffs:HERE
My Animations: HERE
Spoiler for Images :D:

ﾉ◕ヮ◕)ﾉ:・ﾟ PENGUIN WAVE!!:„ø¤º°¨ ¨°º¤KEEP THE PENGUIN GOING ¸„ø¤º°¨ ¨°º¤øº LETS GO PENGUIN !¤¤º°¨¨°º¤øº¤ø„¸¸ø¤º°¨„ ø¤º°¨¨°º
3rik
LV3 Member (Next: 100)

Offline

Gender:
Last Login: 02 April, 2013, 22:45:02
Date Registered: 09 August, 2011, 04:04:54
Location: Right over there.
Posts: 92

Total Post Ratings: +8

 « Reply #5 on: 13 March, 2012, 05:18:03 » +1

First of all, I have no idea what math teachers would say about any of this. I was playing around with some stuff, and I stumbled upon it. I read a few things to confirm I was right.

There are ways of looking at it: rotating the coordinate system or rotating all the points. I think the second way is a bit more intuitive. That’s also the way I found it.

This is the basic setup for rotating a point.

First, we need to describe the point you want to rotate relative to the point of rotation and in terms of an angle from standard position.

Let’s say the original point has coordinates of (xi, yi) and the point of rotation has coordinates of (xcenter, ycenter).

Since unit circle trigonometry is based off a circle at the origin, let’s temporarily translate everything to the origin. Since we are working based on how the points are relative to the point of rotation and not a “fixed” point, we can move the whole system as long as we move it back. To do this, all we have to do is subtract (xcenter, ycenter) from both points. This puts the point of rotation at the origin and the other point at (xi- xcenter, yi - ycenter).

When you rotate an object around a point, the points are always the same distance from each other. This means if you draw a circle with the point of rotation at the center and the point you want to rotate on the circle, the rotated point will also be on the circle. Since we know the coordinates of the two points, we can use the distance formula to find this distance.

√((xi - xcenter)2 + (yi - ycenter)2)

Now by dropping a right triangle down from the point, we can find the angle between the point and standard position.

tan(θ) = o / h

tan(θ) = (xi - xcenter)/(yi - ycenter)

θ = arctan(xi - xcenter)/(yi - ycenter)

Now we have everything needed to express the original point using angles.

(Distance*cos(θ), Distance*sin(θ))

To rotate the point, just add the desired angle of rotation.

Since we have to move everything back to their original position, we need to add (xcenter, ycenter)

To put everything together, you would end up with these equations:

xnew = √((xi - xcenter)2 + (yi - ycenter)2)*cos(arctan(xi - xcenter)/(yi - ycenter)+ θrotation)+ xcenter

ynew = √((xi - xcenter)2 + (yi - ycenter)2)*sin(arctan(xi - xcenter)/(yi - ycenter)+ θrotation)+ ycenter

where (xnew, ynew) is the rotated point.

This looks terrible and is a pain to type so let’s simplify it. According to the linear combination of sine and cosine, a*sin(θ)+b*cos(θ)= √(a2+ b2)*cos(θ - arctan(b/a)).
The second half shows a very near resemblance to our two equations.

The shifts on the ends of the equations wouldn’t matter because if the above statement is true wouldn’t a*sin(θ)+b*cos(θ) + 5= √(a2+ b2)*cos(θ - arctan(b/a)) +5?

Starting with the x equation, a would be yi - ycenter because it is on the bottom of the fraction in the arctan.

That means that b is xi - xcenter

In this case we need to make θ =-θrotation so we can change the sign in front of the θrotation (a double negative)

Also because cos(θ)= cos(-θ) we can then distribute a negative through

xnew = √((xi - xcenter)2 + (yi - ycenter)2)*cos( (-θrotation)- arctan(xi - xcenter)/(yi - ycenter))+ xcenter

now that it matches the formula we can write it as

xnew = (yi - ycenter)*sin(-θrotation) + (xi - xcenter)*cos(-θrotation)  + xcenter

Because cos(θ)= cos(-θ) and -sin(θ)= sin(-θ) it can be written like

xnew = (xi - xcenter)*cos(θrotation) - (yi - ycenter)*sin(θrotation) + xcenter

It’s a similar process for the ys but we need to change the sine into a cosine

cos(θ)= sin(90-θ) because a right triangle’s angles add up to 180 and one is already 90 (pi and pi/2 if you prefer)

so in this case we need 90-θrotation instead (the negative distributes into the arctan but we need that negative anyway)

now that the sine is a cosine we can do the same thing as last time.

ynew = (yi - ycenter)*sin(90-θrotation) + (xi - xcenter)*cos(90-θrotation)  + ycenter

Now we can switch the sines (punny)

ynew = (yi - ycenter)*cos(θrotation) + (xi - xcenter)*sin(θrotation)  + ycenter

In the end these are the equations

xnew = (xi - xcenter)*cos(θrotation) - (yi - ycenter)*sin(θrotation) + xcenter

ynew = (yi - ycenter)*cos(θrotation) + (xi - xcenter)*sin(θrotation)  + ycenter

These are similar to the equations in the Wikipedia article (except those are for rotating around the origin).

To use these equations just plug in the coordinates of the points and make θrotation how much you want to rotate it by.

Just so you know it works to put other parametric equations in for (xi, yi) to rotate all the points in the equations thus rotating the equations

If anything needs clairification, I’m happy to explain. Once again I explored this by myself so I don’t know how it is taught in schools.

 Logged

Userbars
nxtboy III

Offline

Last Login: 22 May, 2013, 02:53:41
Date Registered: 30 March, 2011, 22:57:58
Posts: 784

Topic starter
Total Post Ratings: +23

 « Reply #6 on: 13 March, 2012, 13:44:59 » 0

I tried both equations, and it ended up looking warped.
Why??

EDIT: Well, actually it is not technically a polygon, but rather a set of points (with connected lines) I am trying to rotate.
 « Last Edit: 13 March, 2012, 13:48:31 by nxtboy III » Logged

christop
LV3 Member (Next: 100)

Offline

Gender:
Last Login: 13 April, 2013, 04:04:55
Date Registered: 26 February, 2011, 19:58:44
Location: Arizona, USA
Posts: 87

Total Post Ratings: +20

 « Reply #7 on: 13 March, 2012, 17:25:48 » 0

3rik went into a lot of detail, but let's keep it simple here.

First of all, let's forget that you have a set of points and assume you have only one point to rotate about the origin. For this all you need is these two formulae:
 12 x' = x*cos(theta) - y*sin(theta)y' = x*sin(theta) + y*cos(theta)
where x and y are the coordinates before rotation, x' and y' (x prime and y prime) are the coordinates after rotation, and theta is the angle to rotate the points.

If you need to rotate your point about another point instead of the origin, you just have to subtract the center point from the rotating point and then add the center point back after the rotation:
 12 x' = (x-x0)*cos(theta) - (y-y0)*sin(theta) + x0y' = (x-x0)*sin(theta) + (y-y0)*cos(theta) + y0
where x0 and y0 are the coordinates of the center point. (Obviously if (x0, y0) is (0, 0), the formulae are equivalent to the first set of formulae above.)

Now, you want to rotate a set of points. That's easy. Just evaluate the formulae above for all points in your scene. Since all points will be rotated by the same angle, you can calculate sin(theta) and cos(theta) once for all points. Here's some pseudo-code to illustrate what I mean:
 12345 c = cos(theta)s = sin(theta)for each point (x, y):    x' = x*c - y*s    y' = x*s + y*c
You'll have to deal with connecting them with lines after they're rotated. I won't go into details on how to do that, but you could take one list of coordinates and return a second list of coordinates after they're rotated (say, L1 is the list of original points, and L2 is the list of rotated points), and then have another list that contains pairs of subscripts ("from" and "to" subscripts) into one of those lists.
 « Last Edit: 13 March, 2012, 17:27:53 by christop » Logged

Christopher Williams
nxtboy III

Offline

Last Login: 22 May, 2013, 02:53:41
Date Registered: 30 March, 2011, 22:57:58
Posts: 784

Topic starter
Total Post Ratings: +23

 « Reply #8 on: 13 March, 2012, 19:39:54 » 0

I'm confused....
I tried it and still, it did not work. I am trying to rotate a polygon by rotating it's points. I still want the same shape!
 Logged

Builderboy
Physics Guru

Offline

Gender:
Date Registered: 20 April, 2009, 00:28:53
Location: Ravenholm
Posts: 5644

Total Post Ratings: +589

 « Reply #9 on: 13 March, 2012, 19:50:36 » 0

Show us your code?  Maybe there is something being lost in translation?
 Logged

nxtboy III

Offline

Last Login: 22 May, 2013, 02:53:41
Date Registered: 30 March, 2011, 22:57:58
Posts: 784

Topic starter
Total Post Ratings: +23

 « Reply #10 on: 13 March, 2012, 19:52:56 » 0

Hmmm.... Um well this is not for a calc.... but i'll show you anyway. **I use stuff like cosd with a d at the end so I can input degrees. Otherwise just typing cos is using radians.

here ya go:

 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 float magnitude(int x1,int y1,int x2,int y2){ return(sqrt(pow(x2-x1,2) + pow(y2-y1,2)));}struct floatpoint{ float X; float Y;};//LocationType poly[] = {{10,10},{LocationType sp[]={{10,10},{10,30},{5,27},{10,30},{15,27},{10,30},{10,25},{5,22},{10,25},{15,22},{10,25},{10,20},{5,17},{10,20},{15,17},{10,20}};floatpoint spf[];task main(){ float x,y; float xc,yc;// int dis; //x=0; //y=50; xc= 30; yc=30; float deg; int d; float dis[]; float c,s; int arrs = ArrayLen(sp); ArrayInit(dis,0,arrs); ArrayInit(spf,0,arrs);   for(int c=0; c < arrs; c++)  {   dis[c] = magnitude(xc,yc,sp[c].X,sp[c].Y);   sp[c].X += 30;   sp[c].Y += 30;   spf[c].X = sp[c].X;   spf[c].Y = sp[c].Y;   } while(1) { // x=x*(cosd(30))-y*(sind(30));  //y=x*(sind(30))+y*(cosd(30)); // x = cosd(deg--)*magnitude(0,0,x,y); // y = sind(deg)*magnitude(0,0,x,y); // x -= xc;  //y -= yc;  c = cosd(0);  s = sind(0);  for(int c=0; c < arrs; c++)  { // deg = atan2d((sp[c].Y-yc),(sp[c].X-xc));  // x = spf[c].X*c-spf[c].Y*s;   //y = spf[c].X*s+spf[c].Y*c;   x = sqrt(pow((spf[c].X - xc),2) + pow((spf[c].Y - yc),2))*cosd(atand(spf[c].X - xc)/(spf[c].Y - yc)+ 5)+ xc;   y = sqrt(pow((spf[c].X - xc),2) + pow((spf[c].Y - yc),2))*sind(atand(spf[c].X - xc)/(spf[c].Y - yc)+ 5)+ yc;  //y = dis[c]*sind(deg + 3)+yc; // x = (sp[c].X-xc)*cosd(d) - (sp[c].Y-yc)*sind(d) + xc;  //y = (sp[c].Y-yc)*cosd(d) - (sp[c].X-xc)*sind(d) + yc;  spf[c].X = x;  spf[c].Y = y;  sp[c].X=spf[c].X;  sp[c].Y=spf[c].Y;  }  d--; // sp[0].X=1;  //CircleOut(x,y,10);  PolyOut(sp);/*  for(int c=0; c < arrs; c++)  {   PointOut(sp[c].X,sp[x].Y);  }*/  Wait(30);  ClearScreen(); }}
 Logged

Builderboy
Physics Guru

Offline

Gender:
Date Registered: 20 April, 2009, 00:28:53
Location: Ravenholm
Posts: 5644

Total Post Ratings: +589

 « Reply #11 on: 13 March, 2012, 20:00:22 » 0

What is this code for?

 12 x = sqrt(pow((spf[c].X - xc),2) + pow((spf[c].Y - yc),2))*cosd(atand(spf[c].X - xc)/(spf[c].Y - yc)+ 5)+ xc;y = sqrt(pow((spf[c].X - xc),2) + pow((spf[c].Y - yc),2))*sind(atand(spf[c].X - xc)/(spf[c].Y - yc)+ 5)+ yc;

You seem to have commented out the relevant rotation code as well?
 Logged

nxtboy III

Offline

Last Login: 22 May, 2013, 02:53:41
Date Registered: 30 March, 2011, 22:57:58
Posts: 784

Topic starter
Total Post Ratings: +23

 « Reply #12 on: 13 March, 2012, 20:04:17 » 0

That is the rotation code. I commented out the other rotation code so I could test other algorithms people told me.
 Logged

Builderboy
Physics Guru

Offline

Gender:
Date Registered: 20 April, 2009, 00:28:53
Location: Ravenholm
Posts: 5644

Total Post Ratings: +589

 « Reply #13 on: 13 March, 2012, 20:06:29 » 0

Lol well that would probably be your problem.  Use the code christop or 3rik posted and everything should work just fine.  Square roots and arctangents and powers are not used in any rotation code I know of.
 Logged

nxtboy III

Offline

Last Login: 22 May, 2013, 02:53:41
Date Registered: 30 March, 2011, 22:57:58
Posts: 784

Topic starter
Total Post Ratings: +23

 « Reply #14 on: 13 March, 2012, 20:08:44 » 0

But I did try all of them and they don't work!
The squareroot is for the distance formula.
 « Last Edit: 13 March, 2012, 20:09:28 by nxtboy III » Logged

 Pages: [1] 2 3   Go Up