GAMES101-Homework4

2023-12-18 14:11:56

在这里插入图片描述
原理:就是上张图片

//该段代码表示四个点的情况,相当于举例子去计算上诉的公式
void naive_bezier(const std::vector<cv::Point2f> &points, cv::Mat &window) 
{
    auto &p_0 = points[0];
    auto &p_1 = points[1];
    auto &p_2 = points[2];
    auto &p_3 = points[3];

    for (double t = 0.0; t <= 1.0; t += 0.001) 
    {
        auto point = std::pow(1 - t, 3) * p_0 + 3 * t * std::pow(1 - t, 2) * p_1 +
                 3 * std::pow(t, 2) * (1 - t) * p_2 + std::pow(t, 3) * p_3;

        window.at<cv::Vec3b>(point.y, point.x)[2] = 255;
    }
}
int factorial(int n) //递归实现阶乘
{
    if (n == 0) return 1;
    return n * factorial(n - 1);
}
cv::Point2f recursive_bezier(const std::vector<cv::Point2f> &control_points, float t) 
{
    // TODO: Implement de Casteljau's algorithm
    int n = control_points.size() - 1; //n-1个控制点
    auto point = control_points[0]*std::pow(1-t,n); //初始化第一个点
    for (int i = 1; i <= n; ++i)
    {
        point += control_points[i] * factorial(n) / (factorial(i) * factorial(n - i)) * std::pow(t, i) * std::pow(1 - t, n - i);        //   套上公式去计算
    }
    return point;
}
void bezier(const std::vector<cv::Point2f> &control_points, cv::Mat &window) 
{
    // TODO: Iterate through all t = 0 to t = 1 with small steps, and call de Casteljau's 
    // recursive Bezier algorithm.
    for (double t = 0.0; t <= 1.0; t += 0.001)
    {
        auto point = recursive_bezier(control_points, t);
        window.at<cv::Vec3b>(point.y, point.x)[1] = 255; //颜色改为绿色
    }
}

文章来源:https://blog.csdn.net/mumuhaoshuai/article/details/135059543
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。