#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
}
}
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:
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.
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
#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
}
}
#include <iostream>
#include <math.h>
#include <iomanip>
usingnamespace 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.