cos/sin-problem
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > cos/sin-problem




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    cos/sin-problem  
Karl Pech


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:02 PM

Hallo,

I have some problems with the output of the following app:
###

#include <iostream>
#include <math.h>
using namespace std;

struct Position
{
float x;
float y;
};

struct Orientation
{
double rx;
double ry;
};

enum PenPosition
{
PENUP,
PENDOWN
};

struct Turtle
{
Position position;
Orientation heading;
PenPosition penPosition;
};

void moveForward( Turtle* turtle, double distance )
{
turtle->position.x += turtle->heading.rx*distance;
turtle->position.y += turtle->heading.ry*distance;

if ( turtle->penPosition == PENDOWN )
{
cout
<< "[ " << turtle->position.x << " " << turtle->position.y << " ]"
<< endl;
}
}

void turn( Turtle* turtle, double angle )
{
turtle->heading.rx =
turtle->heading.rx*cos(angle)+turtle->heading.ry*sin(angle);
turtle->heading.ry
= -turtle->heading.rx*sin(angle)+turtle->heading.ry*cos(angle);
}

void penDown( Turtle* turtle )
{
if ( turtle->penPosition == PENUP )
{
cout
<< "[ " << turtle->position.x << " " << turtle->position.y << " ]"
<< endl;
turtle->penPosition = PENDOWN;
}
}

void penUp( Turtle* turtle )
{
if ( turtle->penPosition == PENDOWN )
{
cout << "-1" << endl;
turtle->penPosition = PENUP;
}
}

void initTurtle( Turtle* turtle )
{
turtle->position.x = 0.0;
turtle->position.y = 0.0;

turtle->heading.rx = 1.0;
turtle->heading.ry = 0.0;

turtle->penPosition = PENUP;
}


void testTurtle( Turtle* turtle )
{
penDown(turtle);

for(int i = 1; i <= 4; i++) // Should draw a quadrat.
{
moveForward(turtle, 1);
turn(turtle, M_PI_2);
}

penUp(turtle);
}

int main( int argc, char** argv )
{
Turtle turtle;

initTurtle( &turtle );

testTurtle( &turtle );

return 0;
}
###

But instead of the following output:
###
[ 0 0 ]
[ 1 0 ]
[ 1 -1 ]
[ 0 -1 ]
[ 0 0 ]
-1
###

I get the following one: :-(
###
[ 0 0 ]
[ 1 0 ]
[ 1 -3.26795e-007 ]
[ 1 -2.06867e-013 ]
[ 1 -3.26795e-007 ]
-1
###

Why do I get this?

Thank You!

Karl.






[ Post a follow-up to this message ]



    Re: cos/sin-problem  
Eric Sosman


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:02 PM

Karl Pech wrote:
quote:
> > [...] > void turn( Turtle* turtle, double angle ) > { > turtle->heading.rx = > turtle->heading.rx*cos(angle)+turtle->heading.ry*sin(angle); > turtle->heading.ry > = -turtle->heading.rx*sin(angle)+turtle->heading.ry*cos(angle); > }
Here's one problem, at least (there may be others). In the `ry' calculation, the `rx' value has already been changed. You need something like double oldrx = turtle->heading.rx; double oldry = turtle->heading.ry; turtle->heading.rx = oldrx * ... + oldry * ...; turtle->heading.ry = oldrx * ... + oldry * ...; -- Eric.Sosman@sun.com




[ Post a follow-up to this message ]



    Re: cos/sin-problem  
Eric Sosman


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:02 PM

Karl Pech wrote:
quote:
> > [...] > void turn( Turtle* turtle, double angle ) > { > turtle->heading.rx = > turtle->heading.rx*cos(angle)+turtle->heading.ry*sin(angle); > turtle->heading.ry > = -turtle->heading.rx*sin(angle)+turtle->heading.ry*cos(angle); > }
Here's one problem, at least (there may be others). In the `ry' calculation, the `rx' value has already been changed. You need something like double oldrx = turtle->heading.rx; double oldry = turtle->heading.ry; turtle->heading.rx = oldrx * ... + oldry * ...; turtle->heading.ry = oldrx * ... + oldry * ...; -- Eric.Sosman@sun.com




[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 01:27 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register