Back to Home Page

/***********************************************************************************
Specify, design and implement a class that can be used to keep track of the
position of a point in three-dimensional space. For example, consider the point
drawn here:

                                          y-axis |
                                                    |
                                                    |
                                                    |
                                                    |                                       x-axis
                                                    .----------------------------->
                                                  /
                                                /
                                              /                    . ( 2.5, 0 ,2.0)
                                            /
                                          /
                                        /
                                    z-axis

The point shown above has three coordinates: x = 2.5, y = 0, and z = 2.0.
Include member functions to set a point to a specific location, to shift a point
a given amount along one of the axes, and to retrieve the coordinates of a point.
Also provide member functions that will rotate the point by a specified angle
around a specied axis.
To compute these rotations, you will need a bit of trigonometry. Suppose you have
a point with coordinates x,y,and z. After rotating this point by an angle alpha,the
point will have new coordinates,which we'll call x',y', and z'. The equation for
the new coordinates use the math.h library functions sin and cos, as shown here:

After an alpha rotation around the x-axis:

x' = x
y' = y cos(alpha) - z sin(alpha)
z' = y sin(alpha) + z cos(alpha)

After an alpha rotation around the y-axis :

x' = x cos(alpha) + z sin(alpha)
y' = y
z' = -x sin(alpha) + z cos(alpha)

After an alpha rotation around the z-axis:

x' = x cos(alpha) - y sin(alpha)
y' = x sin(alpha) + y cos(alpha)
z' = z

Note: alpha is in radians

use this formula to convert degrees to radians

R = ( C * PI) / 180

where R : radians
C : degrees
PI : constant pi = 3.1415926....

Course : Objects Structures and Algorithms II using C++
Tutor: GUILLERMO JULCA
Mercy College

View Output

*********************************************************************************/


#include <iostream.h>
#include <math.h>
#include <conio.h>
#include "TDPoint.h"

//---------------------------------------------------------------------------

#pragma hdrstop

//---------------------------------------------------------------------------

#pragma argsused
int main(int argc, char* argv[])
{
TDPoint MyPoint(2.5,0,2.0);
cout<<"The original coordinate of my point are:"<<endl;
cout<<endl;
cout<<"x = "<<MyPoint.get_x()<<endl;
cout<<"y = "<<MyPoint.get_y()<<endl;
cout<<"z = "<<MyPoint.get_z()<<endl;

//Rotate 45 degree around the x-axis

MyPoint.rotate_x(45);
cout<<endl;
cout<< "After a 45 degree rotation around the x-axis "<<endl;
cout<< "The new coordinates of the point are: "<<endl;
cout<<endl;
cout<<"x = "<<MyPoint.get_x()<<endl;
cout<<"y = "<<MyPoint.get_y()<<endl;
cout<<"z = "<<MyPoint.get_z()<<endl;
getchar();
return 0;
}
//---------------------------------------------------------------------------

// TDpoint.cpp    File

#include <math.h>
#include "TDPoint.h"

TDPoint::TDPoint(double ini_x, double ini_y, double ini_z)
{
x = ini_x;
y = ini_y;
z = ini_z;
}
void TDPoint::shift(double delta_x, double delta_y, double delta_z)
{
x += delta_x;
y += delta_y;
z += delta_z;
}
void TDPoint::rotate_x( double c)
{
double new_x;
double new_y;
double new_z;
double theta; // angle in radians
theta = (c * M_PI)/180;
new_x = x;
new_y = y*cos(theta) - z*sin(theta);
new_z = y*sin(theta) + z*cos(theta);
x = new_x;
y = new_y;
z = new_z;
}
void TDPoint::rotate_y( double c)
{
double new_x;
double new_y;
double new_z;
double theta; // angle in radians
theta = (c * M_PI)/180;
new_x = x*cos(theta) + z*sin(theta);
new_y = y;
new_z = z*cos(theta) - x*sin(theta);
x = new_x;
y = new_y;
z = new_z;
}
void TDPoint::rotate_z( double c)
{
double new_x;
double new_y;
double new_z;
double theta; // angle in radians
theta = (c * M_PI)/180;
new_x = x*cos(theta) - y*sin(theta);
new_y = x*sin(theta) + y*cos(theta);
new_z = z;
x = new_x;
y = new_y;
z = new_z;
}

//***************************************************************************************

//TDpoint.h        Header File

#ifndef TDPoint_H
#define TDPoint_H

class TDPoint
{
public:
//CONSTRUCTOR
TDPoint(double ini_x, double ini_y, double ini_z);

//CONSTANT MEMBER FUNCTION
double get_x() const { return x; } // inline implementation
double get_y() const { return y; }
double get_z() const { return z; }

//MODIFICATION MEMBER FUNCTIONS
void shift(double delta_x, double delta_y, double delta_z);
void rotate_x( double c); // c is the angle in celcius degree
void rotate_y( double c);
void rotate_z( double c);

private:
double x; // x coordinate of this point
double y; // y coordinate of this point
double z; // z coordinate of this point

};
#endif

//*********************************************************************************************************

View Output

Back to Home Page


Copyright  © 2002                                   GJ  GUILLERMO JULCA