ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.