作业分析
这次作业比较简单,不会写的话可以参考代码里面的naive_bezier。做的过程中有一个问题就是如何显示绿色线条。需要把naive_bezier里面的:window.at<cv::Vec3b>(point.y, point.x)[2] = 255;
改为window.at<cv::Vec3b>(point.y, point.x)[1] = 255;
(也不知道大佬是怎么知道的这点。。我确实是只有参考了别人的才知道怎么写)。
其他倒没什么,直接硬解出答案。
代码
cv::Point2f recursive_bezier(const vector<cv::Point2f>& control_points, float t)
{
// TODO: Implement de Casteljau's algorithm
auto& p0 = control_points[0];
auto& p1 = control_points[1];
auto& p2 = control_points[2];
auto& p3 = control_points[3];
auto pp0 = t * p0 + (1 - t) * p1;
auto pp1 = t * p1 + (1 - t) * p2;
auto pp2 = t * p2 + (1 - t) * p3;
auto ppp0 = t * pp0 + (1 - t) * pp1;
auto ppp1 = t * pp1 + (1 - t) * pp2;
auto ans = t * ppp0 + (1 - t) * ppp1;
return cv::Point2f(ans);
}
void bezier(const 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(float t=0;t<1;t+=0.001)
{
auto point=recursive_bezier(control_points, t);
window.at<cv::Vec3b>(point.y, point.x)[1] = 255;
}
}