Author Topic: Rotation- How does it work??  (Read 11281 times)

0 Members and 1 Guest are viewing this topic.

Offline nxtboy III

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 795
  • Rating: +26/-1
  • NXT!
    • View Profile
    • Program NXT
Rotation- How does it work??
« on: March 12, 2012, 05:15:24 pm »
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

Offline 3rik

  • LV3 Member (Next: 100)
  • ***
  • Posts: 92
  • Rating: +8/-0
  • My TI-84+ SE
    • View Profile
Re: Rotation- How does it work??
« Reply #1 on: March 12, 2012, 05:17:00 pm »
Is this the type of thing you're looking for?

http://en.wikipedia.org/wiki/Rotation_(mathematics)
« Last Edit: March 12, 2012, 11:19:44 pm by 3rik »
Userbars

Offline nxtboy III

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 795
  • Rating: +26/-1
  • NXT!
    • View Profile
    • Program NXT
Re: Rotation- How does it work??
« Reply #2 on: March 12, 2012, 05:20:01 pm »
Do you think you could explain it?

Offline 3rik

  • LV3 Member (Next: 100)
  • ***
  • Posts: 92
  • Rating: +8/-0
  • My TI-84+ SE
    • View Profile
Re: Rotation- How does it work??
« Reply #3 on: March 12, 2012, 05:21:59 pm »
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: March 12, 2012, 05:34:50 pm by 3rik »
Userbars

Offline Yeong

  • Not a bridge
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3739
  • Rating: +278/-12
  • Survivor of Apocalypse
    • View Profile
Re: Rotation- How does it work??
« Reply #4 on: March 12, 2012, 09:08:10 pm »
easiest way to do is using sine/cosine to rotate the points and draw the lines according to that points.
Sig wipe!

Offline 3rik

  • LV3 Member (Next: 100)
  • ***
  • Posts: 92
  • Rating: +8/-0
  • My TI-84+ SE
    • View Profile
Re: Rotation- How does it work??
« Reply #5 on: March 12, 2012, 11:18:03 pm »
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.


Offline nxtboy III

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 795
  • Rating: +26/-1
  • NXT!
    • View Profile
    • Program NXT
Re: Rotation- How does it work??
« Reply #6 on: March 13, 2012, 07:44:59 am »
:(
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: March 13, 2012, 07:48:31 am by nxtboy III »

Offline christop

  • LV3 Member (Next: 100)
  • ***
  • Posts: 87
  • Rating: +20/-0
    • View Profile
Re: Rotation- How does it work??
« Reply #7 on: March 13, 2012, 11:25:48 am »
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:
Code: [Select]
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:
Code: [Select]
x' = (x-x0)*cos(theta) - (y-y0)*sin(theta) + x0
y' = (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:
Code: [Select]
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: March 13, 2012, 11:27:53 am by christop »
Christopher Williams

Offline nxtboy III

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 795
  • Rating: +26/-1
  • NXT!
    • View Profile
    • Program NXT
Re: Rotation- How does it work??
« Reply #8 on: March 13, 2012, 01:39:54 pm »
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!

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Rotation- How does it work??
« Reply #9 on: March 13, 2012, 01:50:36 pm »
Show us your code?  Maybe there is something being lost in translation?

Offline nxtboy III

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 795
  • Rating: +26/-1
  • NXT!
    • View Profile
    • Program NXT
Re: Rotation- How does it work??
« Reply #10 on: March 13, 2012, 01:52:56 pm »
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:

Code: [Select]
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();
 }
}

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Rotation- How does it work??
« Reply #11 on: March 13, 2012, 02:00:22 pm »
What is this code for?

Code: [Select]
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?  ???

Offline nxtboy III

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 795
  • Rating: +26/-1
  • NXT!
    • View Profile
    • Program NXT
Re: Rotation- How does it work??
« Reply #12 on: March 13, 2012, 02:04:17 pm »
That is the rotation code. I commented out the other rotation code so I could test other algorithms people told me.

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Rotation- How does it work??
« Reply #13 on: March 13, 2012, 02:06:29 pm »
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.

Offline nxtboy III

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 795
  • Rating: +26/-1
  • NXT!
    • View Profile
    • Program NXT
Re: Rotation- How does it work??
« Reply #14 on: March 13, 2012, 02:08:44 pm »
But I did try all of them and they don't work! :(
The squareroot is for the distance formula.
« Last Edit: March 13, 2012, 02:09:28 pm by nxtboy III »