Specify, design, and implement a class that can be used to keep track of the pos
ID: 3780966 • Letter: S
Question
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 at the top of the next column. The point shown there has three coordinates: x = 2.5, y = 0, and z 2.0. Include member functions to set a point to a specified 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 specified axis. To compute these rotations, you will need abit of trigonometry. Suppose you have a point with coordinates, x, y, and z. After rotating this point (counter-clockwise) by an angle theta, the point will have new coordinates, which we'll call x', y', and z'. The equations for the new coordinates use the c math library functions sin and cos, as shown here: After a a rotation around the x-axis: x' = x y' y cos(theta)-z sin(theta) z' = y sin(theta) +z cos(theta)Explanation / Answer
// Please see comments given with code .
// angle to be passed in PI terms +ve for anticlock and -ve for clock
// axis value to be passed for axis of rotation : x = 1 , y = 2 , z = 3
#include <iostream>
#include <cmath>
using namespace std;
#include <iostream>
using namespace std;
class TDPR
{
private :
// coordinates
double x,y,z;
// default constructor
public : TDPR()
{
}
// constructor to initialize coordinates passing x,y,z in sequence
public : TDPR
(double xc,double yc,double zc)
{
x = xc ;
y = yc ;
z = zc
;
}
// to set coordinates passing x,y,z in sequence
public : void setPoint(double xc,double yc,double zc)
{
x = xc ;
y = yc ;
z = zc ;
}
// to rotate angle in PI value ,+ve for counterclockwise and -ve for clockwise and axis as 1 for x , 2 for y and 3 for z
public : void rotate(double angle , int axis )
{
if(axis==1)
{
y = y*cos(angle) -
z*sin(angle);
z = y*sin(angle) + z*cos(angle);
}
else
if(axis==2)
{
x = x*cos(angle) + z*sin(angle);
z = z*cos(angle) - x*sin(angle);
}
else if
(axis==3)
{
x = x*cos(angle) - y*sin(angle);
y =
x*sin(angle) + y*cos(angle);
}
}
public : void showPoint
()
{
cout << " x = " << x <<" y = " << y <<" z = " <<
z << endl;
}
} ;
// Main Function
int main()
{
TDPR T1 ; // T1 object created without coordinates
TDPR T2(1.5,2.0,3.0); // T2 object created with coordinates
T1.setPoint(3.0,0.0,1.0); // setting coordinates for T1
T1.showPoint(); // show coordinates for T1
T2.showPoint(); // show coordinates for T2
T1.rotate(1,2); // rotate T1
T2.rotate(-1,1); // rotate T2
T1.showPoint(); // show coordinates for T1
T2.showPoint(); // show coordinates for T2
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.