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

0 Members and 2 Guests are viewing this topic.

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 #15 on: March 13, 2012, 02:12:32 pm »
That is because you typed it in wrong D: You should be using this code:

Code: [Select]
x = (sp[c].X-xc)*cosd(d) - (sp[c].Y-yc)*sind(d) + xc;
y = (sp[c].X-xc)*sind(d) + (sp[c].Y-yc)*cosd(d) + yc;

Note the plus sign instead of the minus symbol

Offline nxtboy III

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 795
  • Rating: +26/-1
  • NXT!
    • View Profile
    • Program NXT
Re: Rotation- How does it work??
« Reply #16 on: March 13, 2012, 02:40:01 pm »
Yeah, I did that and added some parentheses and BOOM!!! It worked!!!! Thanks!! :D

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 #17 on: March 13, 2012, 02:44:22 pm »
Yay! Glad I could help ^^

Offline nxtboy III

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 795
  • Rating: +26/-1
  • NXT!
    • View Profile
    • Program NXT
Re: Rotation- How does it work??
« Reply #18 on: March 13, 2012, 02:46:45 pm »
I tried rotating a picture by getting all the points. It works, but is really slow. Could I speed this up?

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 #19 on: March 13, 2012, 02:48:23 pm »
The best thing you could do would be to move the cos() and sin() outside the loop, since you really only need to calculate them once per rotation.  Just precalculate them, place them into variables of their own, and use those variables instead :) 

Offline nxtboy III

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 795
  • Rating: +26/-1
  • NXT!
    • View Profile
    • Program NXT
Re: Rotation- How does it work??
« Reply #20 on: March 13, 2012, 02:49:33 pm »
I tried that, but for some odd reason, it messes up. :(

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 #21 on: March 13, 2012, 02:55:57 pm »
Post your code again?

Offline nxtboy III

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

byte Data[];
byte Line;
byte offset;

//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[];


byte __ips_masks[] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};

byte isPixelSet(byte Xx, byte Yy)
{
  Line = (63-Yy & 0xF8) / 8;
  offset = Yy % 8;
  GetDisplayNormal(Xx, Line, 1, Data);
  return Data[0] & __ips_masks[offset];
}

task main()
{
 float x,y;
 int xc,yc;
// int dis;
 //x=0;
 //y=50;
 xc= 40;
 yc=40;
 float deg;
 int d;
 //float dis[];
 float c,s;
 int xt1,yt1,xt2,yt2;
 int l;
 //int arrs = ArrayLen(sp);
 int bul = 0;
// ArrayInit(dis,0,arrs);
// ArrayInit(spf,0,arrs);
//RectOut(50,50,3,3);
GraphicOut(50,40,"squirt.ric");
 for(int c=0; c < 100; c++)
 {
  for(int d=0; d < 64; d++)
  {
   if(isPixelSet(c,d))
   {
    l++;
   }
  }
 }
 ArrayInit(spf,0,l);
  for(int c=0; c < 100; c++)
 {
  for(int d=0; d < 64; d++)
  {
   if(isPixelSet(c,d))
   {
    spf[bul].X = c;
    spf[bul].Y = d;
    bul++;
   }
  }
 }
 PlayToneEx(600,30,1,0);
/*   for(int c=0; c < l; c++)
  {
  // dis[c] = magnitude(xc,yc,sp[c].X,sp[c].Y);
   //sp[c].X += 30;
   //sp[c].Y += 30;
   if(
   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(d);
  s = sind(d);
  for(int c=0; c < l; c++)
  {
 // deg = atan2d((sp[c].Y-yc),(sp[c].X-xc));
  // xt1 = ((spf[c].X - xc) * c);
   //yt1 = ((spf[c].Y - yc) * s);
   //xt2 = ((spf[c].X - xc) * s);
   //yt2 = ((spf[c].Y - yc) * c);
   //x = (xt1 - yt1) + xc;
   //y = (xt2 + yt2) + yc;
  // 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 = ((spf[c].X-xc)*c) - ((spf[c].Y-yc)*s) + xc;
  y = ((spf[c].Y-yc)*c) + ((spf[c].X-xc)*s) + yc;
  spf[c].X = x;
  spf[c].Y = y;
  //sp[c].X=spf[c].X;
  //sp[c].Y=spf[c].Y;
  }
  if(ButtonPressed(BTNRIGHT,0))
  d = -5;
  else
  d = 0;
 // sp[0].X=1;
  //CircleOut(x,y,10);
//  PolyOut(sp);
SetDisplayFlags( DISPLAY_REFRESH_DISABLED);
  ClearScreen();
  for(int c=0; c < l; c++)
  {
   PointOut(spf[c].X,spf[c].Y);
  }
   //PolyOut(sp);
  SetDisplayFlags( DISPLAY_ON | DISPLAY_REFRESH);

  Wait(17);
 }
}

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 #23 on: March 13, 2012, 03:00:11 pm »
For future reference you only have to post the relevant part of your code, not the whole thing :P Could it be that the issue is coming from the fact that your for loop variable is also named C?

Offline nxtboy III

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 795
  • Rating: +26/-1
  • NXT!
    • View Profile
    • Program NXT
Re: Rotation- How does it work??
« Reply #24 on: March 13, 2012, 03:02:31 pm »
Wow, I can't believe I did not think of that!! :banghead: Now it works. :)
Though it doesn't seem to go much faster. :(

You should help me program stuff...
« Last Edit: March 13, 2012, 03:02:48 pm by nxtboy III »

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 #25 on: March 13, 2012, 03:03:36 pm »
Yeah rotation is slow in general without stuff like Graphics Cards :[ unfortunately I don't think you will be able to get much faster than you have it currently.

Offline nxtboy III

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 795
  • Rating: +26/-1
  • NXT!
    • View Profile
    • Program NXT
Re: Rotation- How does it work??
« Reply #26 on: March 13, 2012, 03:05:09 pm »
yeah....
But you seem to be good at this language. You should help me with other stuff...

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 #27 on: March 13, 2012, 03:06:53 pm »
On the contrary I have little knowledge about that language, you were just lucky the issues you were having were general issues and not programming specific issues.  And if you ever have any more issues feel free to post about them and I will see what I can do ^^

Offline nxtboy III

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 795
  • Rating: +26/-1
  • NXT!
    • View Profile
    • Program NXT
Re: Rotation- How does it work??
« Reply #28 on: March 13, 2012, 03:14:47 pm »
ok, sure. ;)

Offline christop

  • LV3 Member (Next: 100)
  • ***
  • Posts: 87
  • Rating: +20/-0
    • View Profile
Re: Rotation- How does it work??
« Reply #29 on: March 13, 2012, 03:43:23 pm »
I tried rotating a picture by getting all the points. It works, but is really slow. Could I speed this up?
Are you trying to rotate a raster (bitmap) image? That's understandably going to slow because a raster image contains many points.

It might be a lot quicker to rotate it by shearing the bitmap three times: http://www.ocf.berkeley.edu/~fricke/projects/israel/paeth/rotation_by_shearing.html

Or you could try using fixed-point instead of floating-point arithmetic using your current algorithm because your system might not have floating-point hardware, so it has to be emulated in software (which is slooooow).
Christopher Williams