|
Home > Archive > Unix Programming > January 2004 > cos/sin-problem
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Karl Pech 2004-01-23, 5: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.
| |
| Eric Sosman 2004-01-23, 5: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
| |
| Eric Sosman 2004-01-23, 5: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
|
|
|
|
|