Rotating Points in a Class

Hello :)

So my assignment had me create a class for a 3 dimensional point system and initialize it to zero.

It then has me add 2 to each point, then rotate it on the x axis by 90 degrees, then the y axis by 180 degrees, and then z axis by 45 degrees.

I am not entirely sure if my outputs are correct because he didn't give us any sample outputs, but my answers seem off. For the point (1,1,1), which gets shifted to (3,3,3,) I get: (0,-4.24264,-3). For (0,0,0), which gets shifted to (2,2,2), I get -2.22045e-16,-2.82843,-2). It seems the problem is with the X and maybe Y coordinate.

Ill just link the member functions since those are what I think I'm having a problem with

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
void point::shift_x(double amount)
{
  x = x+amount;
}
void point::shift_y(double amount)
{
  y=y+amount;
}
void point::shift_z(double amount)
{
  z=z+amount;
}

void point::rotate_x(double a)
{
  double tempy;
  double tempz;

  tempy = y;
  tempz = z;

  double pithree;
  pithree = atan(1.0) * 4.0;

  a=a*(pithree/180);

     
      y= (tempy*cos(a))-(tempz*sin(a));
      z= (tempy*sin(a))+(tempz*cos(a));
}
void point::rotate_y(double a)
{
  double tempx;
  double tempz;

  tempx = x;
  tempz = z;

  double pione;
  pione = atan(1.0)*4.0;

  a = a*(pione/180);

  x = (tempx*cos(a))+(tempz*sin(a));
  z= (tempz*cos(a))-(tempx*sin(a));
}
void point::rotate_z(double a)
{
  double tempx;
  double tempy;

  tempx = x;
  tempy = y;

  double pitwo;

  pitwo = atan(1.0)*4.0;

  a = a*(pitwo/180);

  x = (tempx*cos(a))- (y*sin(a));
  y = (tempx*sin(a))+(tempy*cos(a));
}



The double a is just the degree we rotate by, and the double amount is 2.0. If anyone could look over this and see any problems with the equations you'd really be helping me out. He gave us those rotation formulas.
Last edited on
The trig functions take arguments in radians. If your angles are 45, 90 etc - then work these out in terms of pi. The cmath library has the constant M_PI already defined. M_PI radians is 180 degrees. There is no need to recalculate pi each time. Normally one would have a DegToRadians & a RadiansToDeg functions.


Consider this - a bit shorter:

1
2
3
4
5
6
void point::shift(double dX, double dY, double dZ)
{
  m_x += dX;  //note the way to name member variables
m_y += dY;  //note the += operator
m_z += dZ;
}
I didn't know cmath already had pi defined. I know my syntax isn't perfect, but changing my syntax and stuff won't change my answer, as it is all reading correctly. It calculates the correct output for a lot of points, but the point (0,0,0) still comes out looking weird. The X coordinate should be zero after the last rotation, but it comes out looking like -2.22-45e-16. I've tried to trouble shoot it, and it is showing that it reading it correctly. It shows -1.41421 - ( -1.41421). Which should be zero.


This is what my output looks like for (0,0,0)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Please enter an x coordinate
0
Please enter a y coordinate
0
Please enter a z coordinate
0
After shift: (2,2,2)
After x rotation 2,-2,2)
After y rotation: (-2,-2,-2)
tempone is -1.41421
temptwo is -1.41421
Tempone minus temptwo is2.22045e-16
After z rotation (-2.22045e-16,-2.82843,-2)
The resulting point is (-2.22045e-16,-2.82843,-2)


For (1,1,1) my output looks like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Please enter an x coordinate
1
Please enter a y coordinate
1
Please enter a z coordinate
1
After shift: (3,3,3)
After x rotation 3,-3,3)
After y rotation: (-3,-3,-3)
tempone is -2.12132
temptwo is -2.12132
Tempone minus temptwo is0
After z rotation (0,-4.24264,-3)
The resulting point is (0,-4.24264,-3)
The X coordinate should be zero after the last rotation, but it comes out looking like -2.22-45e-16


-2.22-45e-16 almost is zero !! Check out the value of numeric_limits<double>epsilon which is the difference between 1.0 and the next representable number.

If you were to cout the -2.22-45e-16 value to 6 decimal places say - then you would have zero !!

doubles have 16 significant figures, so 1e-16 effectively is zero. You should be aware of this in the future if you come to compare values.
Ok, that makes sense. I knew that number was really close to zero. I did a static_cast to int at the end to check and it made it zero so I guess it is right. Is there anyway to have it display zero as a double and not the weird number. I think I'm going to have to ask my professor what he wants as an output. Thanks for clearing that up for me
Is there anyway to have it display zero as a double and not the weird number.


Check out setprecision in the reference pages, top left of this screen. Would actually be good for a lot of people to do lots of reading there - why is it lots of people seem blissfully unaware of the reference section on this site?

Any way GOOD LUCK !!!! :D
I did a static_cast to int at the end to check and it made it zero so I guess it is right.


Casting to int isn't a test for equality to zero. For example 0.4 cast to int will be zero. I am not sure whether this cast does any rounding - I doubt it (it just truncates ) - so 0.9 will be zero also.
Topic archived. No new replies allowed.