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!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <math.h>


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 << ", " <<t.y << std::endl;
 	t.move(1); // we put 1 here "optional" this is ds
 	t.turn(2*M_PI/4); // why 4, because 360 divided with 4 is 90
 
 	
 	// Put in some if or else commands here to demonstrate
 	// if the turtle do this do this
 	// If not do this
 
 
 	}	
 }
 

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
 #include <iostream>
#include <math.h>


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 << ", " <<t.y << std::endl;
 	t.move(1); // we put 1 here "optional" this is ds
 	t.turn(2*M_PI/4); // why 4, because 360 divided with 4 is 90
 
 	
 	// Put in some if or else commands here to demonstrate
 	// if the turtle do this do this
 	// If not do this
 
 
 	}	
 }
 
The thing is when you rotate something you don't get nice round numbers, so you need to round up.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <math.h>
#include <iomanip>

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 << ", " <<t.y << std::endl;
 	t.move(1); // we put 1 here "optional" this is ds
 	t.turn(M_PI/4); 
 
 	
 	// Put in some if or else commands here to demonstrate
 	// if the turtle do this do this
 	// If not do this
 
	
 	}	

	char c;
	cin>>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.