LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-30-2005, 04:47 AM   #1
Orkie
Member
 
Registered: Mar 2005
Distribution: Breezy Badger
Posts: 248

Rep: Reputation: 30
Incorrect Formulae


Please can somebody check the formulae that I'm using for my Logo interpreter. I just can't seem to get them right! This is my FORWARD code:

Code:
if(command[0] == "FORWARD")
     {
                   // Setup variables
                   point temp;
                   int distance = atoi(command[1].c_str());
                   
                   // If angle is within the x quadrant's boundries, use correct formula
                   if(angle >= 0 && angle < 90)
                   {
                            int tempAngle = angle;
                            
                            int opp = (int) ROUND(sin(tempAngle) * distance);
                            int adj = (int) ROUND(cos(tempAngle) * distance);
                            
                            temp.x = currentLocation.x + opp;
                            temp.y = currentLocation.y - adj;
                   }
                   else if(angle >= 90 && angle < 180)
                   {
                            int tempAngle = angle - 90;
                            
                            int opp = (int) ROUND(sin(tempAngle) * distance);
                            int adj = (int) ROUND(cos(tempAngle) * distance);
                            
                            temp.x = currentLocation.x + adj;
                            temp.y = currentLocation.y + opp;
                   }
                   else if(angle >= 180 && angle < 270)
                   {
                            int tempAngle = angle - 180;
                            
                            int opp = (int) ROUND(sin(tempAngle) * distance);
                            int adj = (int) ROUND(cos(tempAngle) * distance);
                            
                            temp.x = currentLocation.x - opp;
                            temp.y = currentLocation.y + adj;
                   }
                   else if(angle >= 270 && angle < 360)
                   {
                            int tempAngle = angle - 270;
                            
                            int opp = (int) ROUND(sin(tempAngle) * distance);
                            int adj = (int) ROUND(cos(tempAngle) * distance);
                            
                            temp.x = currentLocation.x - adj;
                            temp.y = currentLocation.y - opp;
                   }
                   else
                   {
                       error("Angle falls outside of allowed range");
                       return false;
                   }
                   
                   // Draw the line
                   aalineRGBA(screen, currentLocation.x, currentLocation.y, temp.x, temp.y, penColour.r, penColour.g, penColour.b, 255);
                   
                   // Make the new coordinates active
                   currentLocation = temp;
     }
It works out the new co-ordinates for after moving and stores them in temp.{x,y}. It then draws a line from the old location to the newly calculated one then sets the new location to be the current one for the next time that the function runs.

When I run the following program, I get a strange shape:
Code:
FORWARD 50 ;
RIGHT 10 ;
FORWARD 50 ;
And here is what is drawn:
[img=http://img474.imageshack.us/img474/8837/shape6dj.jpg]

I have used many different FORWARD functions, such this (tempx and tempy are equivalent to opp and adj):
Code:
int tempx = (int) ROUND(sin(angle) * distance);
int tempy = (int) ROUND(cos(angle) * distance);
Could this be a bug in SDL_gfx? Or is it my code? Thanks
 
Old 10-30-2005, 09:43 AM   #2
naf
Member
 
Registered: Oct 2005
Location: Chicago, USA
Distribution: Slackware & Fedora
Posts: 66

Rep: Reputation: 15
The picture does not come up for me.

Which direction is angle 0 at again? I'll assume straight up (12-O'Clock) because the adjustment is trivial (just add an offset and modulo).

Perhaps the problem is that you expect the trig functions to use degrees and not radians.

double trueangle = ( PI * angle / 180.0 );

Then, for all angles (no quadrants):
temp.x = oldposition.x + sin( trueangle ) * distance;
temp.y = oldposition.y + cos( trueangle ) * distance;

This little Maple script should help emphasize it.
Code:
for angle from 0 to 360 by 15 do     
 (angle,round(sin(Pi*angle/180)*distance),round(cos(Pi*angle/180)*distance)) 
end do;
                              0, 0, 100
                              15, 26, 97
                              30, 50, 87
                              45, 71, 71
                              60, 87, 50
                              75, 97, 26
                              90, 100, 0
                             105, 97, -26
                             120, 87, -50
                             135, 71, -71
                             150, 50, -87
                             165, 26, -97
                             180, 0, -100
                            195, -26, -97
                            210, -50, -87
                            225, -71, -71
                            240, -87, -50
                            255, -97, -26
                             270, -100, 0
                             285, -97, 26
                             300, -87, 50
                             315, -71, 71
                             330, -50, 87
                             345, -26, 97
                             360, 0, 100
 
Old 10-30-2005, 10:13 AM   #3
Orkie
Member
 
Registered: Mar 2005
Distribution: Breezy Badger
Posts: 248

Original Poster
Rep: Reputation: 30
Thanks for that, I've got it working now. Clearly we haven't done enough trigonometry in school
Here is what I have now:
Code:
if(command[0] == "FORWARD")
     {
                   // Setup variables
                   point temp;
                   int distance = atoi(command[1].c_str());
                   
                   // Calculate new position and store in 'temp'
                   int realAngle = angle + 90;
                   long double degRad = M_PI / 180.0;
                   
                   int tempx = (int) ROUND(cos((realAngle * degRad)) * distance);
                   int tempy = (int) ROUND(sin((realAngle * degRad)) * distance);
                   
                   temp.x = currentLocation.x - tempx;
                   temp.y = currentLocation.y - tempy;
                   
                   // Draw the line
                   aalineRGBA(screen, currentLocation.x, currentLocation.y, temp.x, temp.y, penColour.r, penColour.g, penColour.b, 255);
                   
                   // Make the new coordinates active
                   currentLocation = temp;
     }
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Incorrect Mouse, Incorrect Keymap, and Trapped in X Kenji Miyamoto Debian 8 08-24-2005 02:42 PM
Incorrect time Delphinusnz Mandriva 2 10-12-2004 02:22 AM
Software to make equations/formulae akudewan Linux - Software 6 06-08-2004 11:37 AM
/etc/fstab incorrect narinder31 Linux - Newbie 5 08-15-2003 02:52 AM
Incorrect DMA mismacku Linux - General 2 05-11-2003 01:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration