### Need help with Turtle Graphics

Hey people I have a simple question here!

My teacher told me during class that this turtle class should print out

0, 0
1, 0
1, 1
0, 1

but i get

0, 0
1, 0
-4.37114e-08, -0
1.91069e-15, 0

can you show me what im doing wrong!

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758`` ``````#include #include class Turtle { public: float x, y; Turtle(){ x = 0; // this is the starting point for the x y = 0; // this is the starting point for the y mUx = 1; mUy = 0; } void move(float ds){ x = mUx * ds; y = mUy * ds; } void turn(float ang){ float ux = mUx; float uy = mUy; mUx = ux * cos(ang) - uy * sin(ang); mUy = uy * cos(ang) - uy * sin(ang); } private: // we make the unit vector private because we provide // a sperate interface for changing them float mUx, mUy; }; int main(){ Turtle t; for(int i=0;i<4;++i){ std::cout << t.x << ", " <

Last edited on
Those funny values are extremely close to zero. This happens because floating point numbers are limited in what decimal values they can represent. Read this:

http://floating-point-gui.de/
What Moschops said is right, and therefore there is something wrong with the formulae as well? Because it is not going to print
0, 0
1, 0
1, 1
0, 1
as you expect.
Looking at last two lines, you can make the first value 1, 0 from -4.37114e-08 and 1.91069e-15 by using floor and abs functions but cant make second value 1 from -0 and 0.
Isnt this the same thing:

t.turn(M_PI/2);
I tried replacing all floats with ints in the code

now I get

0, 0

1, 0

0, 0

0, 0

????

the first two steps are correct, why wont the code then not calculate the third and fourth pair of coordinates??? im using a for loop set to loop four times, I rellay dont understand this
Are you sure this line is correct?

 `` `` ``mUy = uy * cos(ang) - uy * sin(ang);``

I changed a spelling mistake in line 28, 29, but i still dont get the the values my teacher told me to get from this code

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758`` `````` #include #include class Turtle { public: float x, y; Turtle(){ x = 0; // this is the starting point for the x y = 0; // this is the starting point for the y mUx = 1; mUy = 0; } void move(float ds){ x = mUx * ds; y = mUy * ds; } void turn(float ang){ float ux = mUx; float uy = mUy; mUx = ux * cos(ang) - uy * sin(ang); mUy = uy * cos(ang) + ux * sin(ang); } private: // we make the unit vector private because we provide // a sperate interface for changing them float mUx, mUy; }; int main(){ Turtle t; for(int i=0;i<4;++i){ std::cout << t.x << ", " <
The thing is when you rotate something you don't get nice round numbers, so you need to round up.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162`` ``````#include #include #include using namespace std; #define M_PI 3.14159265358979323846264338327 class Turtle { public: float x, y; Turtle(){ x = 0; // this is the starting point for the x y = 0; // this is the starting point for the y mUx = 1; mUy = 0; } void move(float ds){ x = ceil(mUx * ds); y = ceil(mUy * ds); } void turn(float ang){ float ux = mUx; float uy = mUy; mUx = ux * cos(ang) - uy * sin(ang); mUy = uy * cos(ang) + ux * sin(ang); } private: // we make the unit vector private because we provide // a sperate interface for changing them float mUx, mUy; }; int main(){ Turtle t; for(int i=0;i<4;++i){ std::cout << t.x << ", " <>c; };``````

notice that i also changed this:

 `` `` ``t.turn(2*M_PI/4); // why 4, because 360 divided with 4 is 90 ``

If you plot what is occurring on a piece of paper, you'll notice that the point rotates about the origin from position 1,0 to position 0,1. It moves in 45 degree increments, and 45 degrees converted to radians is pi/4.

the ceil(...) is a function that rounds up.
Last edited on
Topic archived. No new replies allowed.