I have an assignment, here is the description:
Create a class named Point. It must have three private float field named as x, y, z to keep coordinates in space and public get and set functions to access these data members ( getX(), getY(), getZ(), setX(), setY(), setZ() ). Create another function that is defined outside of the scope of Point class and named Space. In Space function, you will find the middle point between two points and create a new Point object that will keep calculated coordinates. The written code below must be able to run without errors.
The teacher gave us structure of the code. The code should be like below. only the definition of get, set and space functions can change. I wrote the set and get functions, but I have no idea what to do with the space function. That's why I need help with the space function part.
#include <iostream>
using namespace std;
class Point {
private:
int x, y, z;
public:
float getX();
float getY();
float getZ();
void setX(float x1);
void setY(float y1);
void setZ(float z1);
};
float Point::getX() {
return x;
}
float Point::getY() {
return y;
}
float Point::getZ() {
return z;
}
void Point::setX(float x1) {
x = x1;
}
void Point::setY(float y1) {
y = y1;
}
void Point::setZ(float z1) {
z = z1;
}
Point Space(Point a, Point b)
{
// complete space function
}
int main()
{
float x_, y_, z_;
Point p[3];
for (int i = 0; i < 2; i)
{
cout << "Please enter x coordinate for p" << i 1 << endl;
cin >> x_;
p[i].setX(x_);
cout << "Please enter y coordinate for p" << i 1 << endl;
cin >> y_;
p[i].setY(y_);
cout << "Please enter z coordinate for p" << i 1 << endl;
cin >> z_;
p[i].setZ(z_);
}
p[2] = Space(p[0], p[1]);
cout << "Coordinations of middle point (p3) between p1 and p2 is x=" << p[2].getX() << ",y=" << p[2].getY() << ", z=" << p[2].getZ();
return 0;
}
CodePudding user response:
As the instructions says:
In Space function, you will find the middle point between two points and create a new Point object that will keep calculated coordinates.
A midpoint is the exact center point between two defined points. To find this center point, midpoint formula is applied. In 3-dimensional space, the midpoint between
(x1, y1, z1)
and(x2, y2, z1)
is(x1 x2)/2
,(y1 y2)/2
,(z1 z2)/2
.
So, try this:
Point Space(Point a, Point b)
{
Point mid;
mid.setX((a.getX() b.getX()) / 2);
mid.setY((a.getY() b.getY()) / 2);
mid.setZ((a.getZ() b.getZ()) / 2);
return mid;
}