我有一個任務,這里是描述:
創建一個名為 Point 的類。它必須具有三個名為 x、y、z 的私有浮點欄位以保持空間中的坐標以及公共 get 和 set 函式來訪問這些資料成員(getX()、getY()、getZ()、setX()、setY() , 設定 Z() )。創建另一個在 Point 類范圍之外定義并命名為 Space 的函式。在 Space 函式中,您將找到兩點之間的中點并創建一個新的 Point 物件,該物件將保留計算的坐標。下面的撰寫代碼必須能夠無錯誤地運行。
老師給了我們代碼的結構。代碼應該如下所示。只有 get、set 和 space 函式的定義可以改變。我撰寫了 set 和 get 函式,但我不知道如何處理空間函式。這就是為什么我需要空間功能部分的幫助。
#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;
}
uj5u.com熱心網友回復:
正如說明所說:
在 Space 函式中,您將找到兩點之間的中點并創建一個新的 Point 物件,該物件將保留計算的坐標。
3D 中的每中點公式:
中點是兩個定義點之間的精確中心點。為了找到這個中心點,應用了中點公式。在 3 維空間中, 和 之間的
(x1, y1, z1)中點(x2, y2, z1)是(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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/441468.html
上一篇:從csv檔案創建字典
下一篇:洗掉列印命令中某些特定值的空格
