阅读量:0
当然可以。首先,我们需要定义一个表示点的类 Point
,然后实现变换(例如平移、旋转)和投影(例如正交投影、透视投影)的方法。以下是一个简单的示例:
#include <iostream> #include <cmath> class Point { public: double x, y, z; Point(double x = 0, double y = 0, double z = 0) : x(x), y(y), z(z) {} // 平移变换 Point translate(double dx, double dy, double dz) const { return Point(x + dx, y + dy, z + dz); } // 旋转变换(绕Z轴) Point rotateZ(double angle) const { double rad = angle * M_PI / 180.0; double newX = x * cos(rad) - y * sin(rad); double newY = x * sin(rad) + y * cos(rad); return Point(newX, newY, z); } // 投影方法(正交投影) Point orthographicProjection(double width, double height, double depth) const { double u = (x + 1.0) / 2.0 * width; double v = (z + 1.0) / 2.0 * height; return Point(u, v, 0); } // 投影方法(透视投影) Point perspectiveProjection(double fov, double aspect, double near, double far) const { double range = far - near; double n = near + (z - near) * tan(fov * M_PI / 360.0); double u = (x + 1.0) / 2.0 * width * (n + near) / range; double v = (z + 1.0) / 2.0 * height * (n + near) / range; return Point(u, v, n); } }; int main() { Point p(1, 1, 1); Point translated = p.translate(2, 3, 4); Point rotated = p.rotateZ(45); Point orthographic = p.orthographicProjection(800, 600, 100); Point perspective = p.perspectiveProjection(45, 1, 0.1, 100); std::cout << "Original point: (" << p.x << ", " << p.y << ", " << p.z << ")\n"; std::cout << "Translated point: (" << translated.x << ", " << translated.y << ", " << translated.z << ")\n"; std::cout << "Rotated point: (" << rotated.x << ", " << rotated.y <<