计算用户输入的坐标值的中点。 (x、y、z 轴)需要 class 个主题

calculation of the midpoint of the coordinate values entered by the user. (x, y, z axes) Need class topic

我有一个任务,这里是描述:

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.

老师给了我们代码的结构。 代码应该如下所示。只有 get、set 和 space 函数的定义可以更改。 我编写了 set 和 get 函数,但我不知道如何处理 space 函数。这就是为什么我需要 space 函数部分的帮助。

#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;
}

如说明所述:

In Space function, you will find the middle point between two points and create a new Point object that will keep calculated coordinates.

根据 Mid Point Formula in 3D:

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.

所以,试试这个:

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;
}