帮我写一个c++的三维坐标系
时间: 2023-07-30 12:10:19 浏览: 108
好的,以下是一个简单的 C++ 三维坐标系实现:
```cpp
#include <iostream>
using namespace std;
struct Point3D {
double x, y, z;
};
class CoordinateSystem {
private:
Point3D origin; // 坐标系原点
public:
CoordinateSystem() {
origin.x = origin.y = origin.z = 0.0;
}
void setOrigin(double x, double y, double z) {
origin.x = x;
origin.y = y;
origin.z = z;
}
void drawPoint(Point3D p) {
cout << "(" << p.x << ", " << p.y << ", " << p.z << ")" << endl;
}
void drawAxis() {
cout << "x-axis: ";
drawPoint(origin);
drawPoint({ origin.x + 1.0, origin.y, origin.z });
cout << "y-axis: ";
drawPoint(origin);
drawPoint({ origin.x, origin.y + 1.0, origin.z });
cout << "z-axis: ";
drawPoint(origin);
drawPoint({ origin.x, origin.y, origin.z + 1.0 });
}
};
int main() {
CoordinateSystem cs;
cs.setOrigin(1.0, 2.0, 3.0);
cs.drawAxis();
return 0;
}
```
这个程序定义了一个三维坐标系类 `CoordinateSystem`,包含一个坐标系原点 `origin` 和三个方法:`setOrigin` 设置坐标系原点,`drawPoint` 绘制一个三维点,`drawAxis` 绘制三个坐标轴。在 `main` 函数中,我们创建了一个坐标系对象 `cs`,设置坐标系原点为 `(1.0, 2.0, 3.0)`,并绘制坐标轴。
阅读全文