c++ 在XYZ三轴空间生成圆的轨迹点

#include 
#include 
#include 

struct Point3D {
    double x;
    double y;
    double z;
};

// 生成在XY平面上的圆的轨迹点
std::vector<Point3D> generateCircleInXYPlane(double radius, int numPoints) {
    std::vector<Point3D> result;
    for (int i = 0; i < numPoints; ++i) {
        double angle = 2 * M_PI * i / numPoints;
        Point3D point;
        point.x = radius * cos(angle);
        point.y = radius * sin(angle);
        point.z = 0.0;
        result.push_back(point);
    }
    return result;
}

// 生成在XZ平面上的圆的轨迹点
std::vector<Point3D> generateCircleInXZPlane(double radius, int numPoints) {
    std::vector<Point3D> result;
    for (int i = 0; i < numPoints; ++i) {
        double angle = 2 * M_PI * i / numPoints;
        Point3D point;
        point.x = radius * cos(angle);
        point.y = 0.0;
        point.z = radius * sin(angle);
        result.push_back(point);
    }
    return result;
}

// 生成在YZ平面上的圆的轨迹点
std::vector<Point3D> generateCircleInYZPlane(double radius, int numPoints) {
    std::vector<Point3D> result;
    for (int i = 0; i < numPoints; ++i) {
        double angle = 2 * M_PI * i / numPoints;
        Point3D point;
        point.x = 0.0;
        point.y = radius * cos(angle);
        point.z = radius * sin(angle);
        result.push_back(point);
    }
    return result;
}

// 生成在XYZ三维空间的斜面上的圆的轨迹点
std::vector<Point3D> generateCircleOnSlope(double radius, const Eigen::Vector3d& slopeNormal, const Eigen::Vector3d& circleCenter, int numPoints) {
    std::vector<Point3D> result;
    Eigen::Vector3d xAxis(1.0, 0.0, 0.0);
    Eigen::Matrix3d rotationMatrix = Eigen::AngleAxisd(acos(slopeNormal.dot(xAxis)), slopeNormal.cross(xAxis).normalized()).toRotationMatrix();

    for (int i = 0; i < numPoints; ++i) {
        double angle = 2 * M_PI * i / numPoints;
        Eigen::Vector3d circlePoint(radius * cos(angle), radius * sin(angle), 0.0);
        Eigen::Vector3d rotatedPoint = rotationMatrix * circlePoint;
        Point3D point;
        point.x = circleCenter.x() + rotatedPoint.x();
        point.y = circleCenter.y() + rotatedPoint.y();
        point.z = circleCenter.z() + rotatedPoint.z();
        result.push_back(point);
    }

    return result;
}

int main() {
    double radius = 1.0;
    int numPoints = 8;

	std::vector<Point3D> circleInXYPlane = generateCircleInXYPlane(radius, numPoints);
    std::vector<Point3D> circleInXZPlane = generateCircleInXZPlane(radius, numPoints);
    std::vector<Point3D> circleInYZPlane = generateCircleInYZPlane(radius, numPoints);

    Eigen::Vector3d slopeNormal(1.0, 1.0, 1.0);  // 斜面法向量
    slopeNormal.normalize();
    Eigen::Vector3d circleCenter(0.0, 0.0, 0.0);  // 圆心坐标

    std::vector<Point3D> circleOnSlope = generateCircleOnSlope(radius, slopeNormal, circleCenter, numPoints);

    // 打印生成的点坐标
    for (const Point3D& point : circleInXYPlane) {
        std::cout << "XY Plane: (" << point.x << ", " << point.y << ", " << point.z << ")" << std::endl;
    }
    
    for (const Point3D& point : circleInXZPlane) {
        std::cout << "XZ Plane: (" << point.x << ", " << point.y << ", " << point.z << ")" << std::endl;
    }

    for (const Point3D& point : circleInYZPlane) {
        std::cout << "YZ Plane: (" << point.x << ", " << point.y << ", " << point.z << ")" << std::endl;
    }
    
    for (const Point3D& point : circleOnSlope) {
        std::cout << "Circle on Slope: (" << point.x << ", " << point.y << ", " << point.z << ")" << std::endl;
    }

    return 0;
}

你可能感兴趣的:(编程卷,c++,开发语言)