[英]Drawing Bezier curves using De Casteljau Algorithm in C++ , OpenGL
[英]How do I specify the specific the control points in a De Casteljau algorithm, if I am using nested for loops to iterate?
我知道使用De Casteljau的算法并不是绘制Bezier曲线的最佳方法,但是我需要对它进行赋值,我要根据以下方程式定义我的算法(来自Drexel)。
哪里:
定义控制点。
我正在尝试定义执行算法的功能,但在哪里/如何结合控制点方面却步履维艰。 控制点由用户定义; 当它们与程序交互时,单击鼠标左键将添加一个新的控制点。 我的函数当前如下所示:
2Dpoint deCast(float t)
{
2Dpoint tempDC // Temporary value of point passed back to OpenGL draw function
tempDC.x = 0; tempDC.y = 0 // Initialize temporary value
int r,i;
int n = C->B.size(); // C is pointer to B vector, which is where the control points are stored in a 2D vector
for (r = 1; r<n, r++)
{
for (i = 0; i<n-r; i++)
{
// Calculation of deCast points goes here
}
}
}
其中2Dpoint
只是2Dpoint
文件定义的结构, C
是指向控制点位置的指针,这些控制点存储在称为B
的2Dpoint
结构中(即控制点向量的i
值由C -> B[i].x
访问) C -> B[i].x
和C -> B[i].y
)。 当在我的draw
函数中实现该函数时,将向该函数提供t
,如下所示。
void draw()
{
glColor3f(0.0f, 1.0f, 0.0f);
glLineWidth(2.0f);
glBegin(GL_LINE_STRIP);
float DCiter = 0;
while (DCiter <= 1.0)
{
2Dpoint DC = decast(DCiter);
glVertex2f(DC.x, DC.y);
DCiter = DCiter + 0.01;
}
}
您也必须将deCasteljau函数传递给您的分数,因为这就是de Casteljau算法起作用的全部原因(您甚至可以在数学公式中显示出这一点:它显示“下一个点=前两个点的加权和”) )。 由于需要在点之间进行线性插值,因此需要使用点=)
用伪代码:
deCastestljau (t, points):
// the following code is destructive, so remember to be sensible
points = shallowCopy(points);
// de Casteljau's algorithm in four lines:
while points.length > 1:
for i=0 to points.length-2:
points[i] = (1-t) * points[i] + (t) * points[i+1]
points.pop()
// and we're done.
return points[0]
最后:从控制点开始,然后进行线性插值遍历,以便以距离比t
获得“连续坐标之间”的所有点(例如,如果t为0.25,则找到每个线性插值为1 /从某个点到下一个点的距离的第4个)。
该操作产生的新点集比以前少1。 然后,您一次又一次地执行此操作,直到剩下一分。 那就是你的曲线点。 这适用于任何阶的贝塞尔曲线。
它也与“绘制”曲线几乎没有关系,但是,de Casteljau的算法是一种有效计算曲线上单个点的方法,它对绘制整个曲线没有任何要求,也没有任何真正的关系。 那是一个完全不同的任务,对于它来说,de Casteljau的算法只是找到使绘制算法起作用所需的曲线上点的众多选择之一。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.