how to post a 1 digit for this program

Hi, i really don't know what to do guys can you help me with this code for my matrix program? this program is not yet finished and i am stuck with this problem

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
#include <math.h>

using namespace std;

int main()
{
int rotx, roty, rotz, n, ac1, ac2, ac3, ac4, atransx, atransy, atransz;
double axx1, ayx1, azx1, axy1, ayy1, azy1, axz1, ayz1, azz1;
int transx=0;
int transy=0;
int transz=0;
int c1=0;
int c2=0;
int c3=0;
int c4=1;
int counter;
int count=0;
char command;
Start:
float xx1 = 0;
float yx1 = 0;
float zx1 = 0;
float xy1 = 0;
float yy1 = 0;
float zy1 = 0;
float xz1 = 0;
float yz1 = 0;
float zz1 = 0;

cout<<"Choose a command (Maximum of 3 Operations):"<<endl;
cout<<"[1] Rotate X"<<endl<<"[2] Rotate Y"<<endl<<"[3] Rotate Z"<<endl<<"[4] Add Transformation"<<endl<<"[6] Calculate"<<endl<<"[7] Quit"<<endl<<"Choice: ";
cin>>n;

switch (n){
case 1:
cout <<"Rotate X at angle: "<<endl;
cin >> rotx;
xx1 = sin (rotx*3.14/180);
yx1 = cos (rotx*3.14/180);
zx1 = cos (rotx*3.14/180);
xy1 = cos (rotx*3.14/180);
yy1 = cos (rotx*3.14/180);
zy1 = sin (rotx*3.14/180);
xz1 = cos (rotx*3.14/180);
yz1 = -sin (rotx*3.14/180);
zz1 = cos (rotx*3.14/180);
count=count+1;
cout<<"Angle included"<<endl<<endl;
goto Input;
break;


case 2:
cout<<"Rotate Y at angle: "<<endl;
cin >> roty;
xx1 = cos (roty*3.14/180);
yx1 = cos (roty*3.14/180);
zx1 = -sin (roty*3.14/180);
xy1 = cos (roty*3.14/180);
yy1 = sin (roty*3.14/180);
zy1 = cos (roty*3.14/180);
xz1 = sin (roty*3.14/180);
yz1 = cos (roty*3.14/180);
zz1 = cos (roty*3.14/180);
count++;
cout<<"Angle included"<<endl<<endl;
goto Input;
break;

case 3:
cout <<"Rotate Z at angle: "<<endl;
cin >> rotz;
xx1 = cos (rotz*3.14/180);
yx1 = sin (rotz*3.14/180);
zx1 = cos (rotz*3.14/180);
xy1 = -sin (rotz*3.14/180);
yy1 = cos (rotz*3.14/180);
zy1 = cos (rotz*3.14/180);
xz1 = cos (rotz*3.14/180);
yz1 = cos (rotz*3.14/180);
zz1 = sin (rotz*3.14/180);
count++;
cout<<"Angle included"<<endl<<endl;
goto Input;
break;
} //end of cases

Input:
if (count==1)
{
axx1=xx1;
ayx1=yx1;
azx1=zx1;
ac1=c1;
axy1=xy1;
ayy1=yy1;
azy1=zy1;
ac2=c2;
axz1=xz1;
ayz1=yz1;
azz1=zz1;
ac3=c3;
atransx=transx;
atransy=transy;
atransz=transz;
ac4=c4;

cout<<"Your Translation is:"<<endl<<endl;
cout<<" "<<axx1<<" "<<axy1<<" "<<axz1<<" "<<atransx<<" "<<endl;
cout<<" "<<ayx1<<" "<<ayy1<<" "<<ayz1<<" "<<atransy<<" "<<endl;
cout<<" "<<azx1<<" "<<azy1<<" "<<azz1<<" "<<atransz<<" "<<endl;
cout<<" "<<ac1<<" "<<ac2<<" "<<ac3<<" "<<ac4<<" "<<endl<<endl<<endl;
goto Start;
}
else

return 0;
}


output:
1 0.000796327 0.000796327 0
0.000796327 0.000796327 -1 0
0.000796327 1 0.000796327 0
0 0 0 1

output should be:
1 0 0 0
0 0 -1 0
0 1 0 0
0 0 0 1

any idea how can i have this??? thanks a lot
Last edited on
Rounding error.
Floating point operations don't give exact results, usually just a very close result due to rounding errors.
Even if they would be exact, then the fact that you use PI to only 2 decimals will give incorrect results.
replace all your float's with double's. Floats math isn't exact
you are not going to get 1 digits with floats or double.

your floats are fine.
your Pi is off.
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
#include <iostream>
#include <cmath>

using namespace std;
 
const float Pi = 4.0 * atan(1.0); // 3.14159

class Obj
{
  public:
	float x , y;
	short Ang;
	
	Obj( short aAng )
	{ Ang = aAng; }
	
	void Round( string txt )
	{
		if ( x > -0.0000001 && x < 0.0000001 ) { x = 0; }
		if ( y > -0.0000001 && y < 0.0000001 ) { y = 0; }
		cout << txt << x << " : " << y << endl;
	}
};

void Rotate(Obj& o)
{
	o.x = sin( o.Ang * Pi / 180 );
	o.y = cos( o.Ang * Pi / 180 );
}

int main()
{
	Obj Myo( 0 );
	Rotate( Myo );
	Myo.Round( "Angle 0   : ");
	
	Myo.Ang = 45;
	Rotate( Myo );
	Myo.Round( "Angle 45  : ");
	
	Myo.Ang = 90;
	Rotate( Myo );
	Myo.Round( "Angle 90  : ");
	
	Myo.Ang = 180;
	Rotate( Myo );
	Myo.Round( "Angle 180 : ");

	Myo.Ang = 270;
	Rotate( Myo );
	Myo.Round( "Angle 270 : ");
	
	cout << 4.0 * atan(1.0) << endl;
	
	return 0;
}



hi r0mai, cmccmc and drakemagi for the reply.. now im trying to changed totally everything from float to double. Very great one drakemagi for the code effort.

although i still don't understand the part of your program.

1
2
3
4
5
6
7
8
class Obj{
Obj( short aAng )
	{ Ang = aAng; }
	
	void Round( string txt )
	{
}
}


but i know the code u have given to me is for rounding off, i think i need to hang more with c++ to know more about those keywords
1
2
3
4
5
6
7
8
class Obj     // self define object
{
    Obj( short aAng )    // Constructor , an auto build
    { Ang = aAng; }
	
    void Round( string txt )  // a function/method  . That belong to the class.
    { ... }
}


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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <iostream>
#include <cmath>
#include <string>

using namespace std;

const float Pi = 4.0 * atan(1.0); // 3.14159

// a class is a self define object
class xyz
{
  public: // made public for outside access
	float x , y, z;
	
	xyz() { x = 0; y = 0; z = 0; } // constructor
	
	// void clear() is just a function . void means no return item
	void clear() { x = 0; y = 0; z = 0; } // a function of xyz
};

class myobject
{
  public:
	xyz x, y, z, rot, atrans;
	
	void clear() // a function of myobject
	{ x.clear(); y.clear(); z.clear(); rot.clear(); atrans.clear();}
	
	// all round does is take low numbers and set it to 0
	// numbers like -4.37114e-08
	float Round ( float f ) // a function of myobject class
	{
                // comment if block out to see really small float
		if ( f > -0.0000001 && f < 0.0000001 ) { f = 0; }
		return f;
	}
	
	void Rotate( short n ) // a function of myobject class
	{
		switch(n)
		{
			case 1:
			{
				cout <<"Rotate X at angle: ";
				cin >> rot.x;
				x.x = Round( sin (rot.x*Pi/180.0) );
				y.x = Round( cos (rot.x*Pi/180.0) );
				z.x = Round( cos (rot.x*Pi/180.0) );
				x.y = Round( cos (rot.x*Pi/180.0) );
				y.y = Round( cos (rot.x*Pi/180.0) );
				z.y = Round( sin (rot.x*Pi/180.0) );
				x.z = Round( cos (rot.x*Pi/180.0) );
				y.z = Round( -sin (rot.x*Pi/180.0) );
				z.z = Round( cos (rot.x*Pi/180.0) );
				cout<<"Angle included"<<endl<<endl;
				break;
			}
			case 2:
			{
				cout <<"Rotate Y at angle: ";
				cin >> rot.y;
				x.x = Round( cos (rot.y*Pi/180.0) );
				y.x = Round( cos (rot.y*Pi/180.0) );
				z.x = Round( -sin (rot.y*Pi/180.0) );
				x.y = Round( cos (rot.y*Pi/180.0) );
				y.y = Round( sin (rot.y*Pi/180.0) );
				z.y = Round( cos (rot.y*Pi/180.0) );
				x.z = Round( sin (rot.y*Pi/180.0) );
				y.z = Round( cos (rot.y*Pi/180.0) );
				z.z = Round( cos (rot.y*Pi/180.0) );
				cout<<"Angle included"<<endl<<endl;
				break;
			}
			case 3:
			{
				cout <<"Rotate Z at angle: ";
				cin >> rot.z;
				x.x = Round( cos (rot.z*Pi/180.0) );
				y.x = Round( sin (rot.z*Pi/180.0) );
				z.x = Round( cos (rot.z*Pi/180.0) );
				x.y = Round( -sin (rot.z*Pi/180.0) );
				y.y = Round( cos (rot.z*Pi/180.0) );
				z.y = Round( cos (rot.z*Pi/180.0) );
				x.z = Round( cos (rot.z*Pi/180.0) );
				y.z = Round( cos (rot.z*Pi/180.0) );
				z.z = Round( sin (rot.z*Pi/180.0) );
				cout<<"Angle included"<<endl<<endl;
				break;
			}
			default:
			{
				break;
			}
		}
	}
	
	void Print()
	{
		string space = "       ";
		cout << endl <<" Your Translation is:" << endl;
		cout << space << x.x << " " << x.y << " " << x.z << " " << atrans.x << endl;
		cout << space << y.x << " " << y.y << " " << y.z << " " << atrans.y << endl;
		cout << space << z.x << " " << z.y << " " << z.z << " " << atrans.z << endl;
		cout << endl;
	}
};

int main()
{
	myobject obj;
	int n;
	bool run = true;
	
	while (run)
	{
		cout << "  (1) Rotate X" << endl; 
		cout << "  (2) Rotate Y" << endl;
		cout << "  (3) Rotate Z" << endl;
		cout << "  (10) Reset" << endl;
		cout << "  (11) Result" << endl;
		cout << "  (12) Quit" << endl;
		cout << endl << "Choice :: ";
		cin >> n;
		
		if ( n < 9 ) { obj.Rotate( n ); }
		else if (n == 10) { obj.clear(); }
		else if (n == 11) { obj.Print(); }
		else if (n == 12) { run = false; }	
	}
	
	return 0;
}
Last edited on
Topic archived. No new replies allowed.