我正在绘制二次贝塞尔曲线,但我不确定它为什么会这样。几乎看起来好像有另一个控制点影响曲线,但只有 3 个控制点,并且该公式仅适用于二次曲线。
这是它正在做的一些图片:http://imgur.com/a/wOWSe
和代码:
Point Bezier::evaluate(float t)
{
// [x,y]=(1–t)^2*2P0+2(1–t)t*P1+t^2*P2
Point P;
P.x = (1 - t)*(1 - t)*points[0].x +
2*(1 - t)*(1 - t)*t*points[1].x +
t*t*points[2].x;
P.y = (1 - t)*(1 - t)*points[0].y +
2*(1 - t)*(1 - t)*t*points[1].y +
t*t*points[2].y;
return P;
}
void Bezier::drawCurve()
{
glColor3d(red, green, blue);
Point lastPoint = points[0];
for (float t = 0.0; t <= 1.0; t += 0.01)
{
Point currentPoint = evaluate(t);
drawLine(lastPoint.x, lastPoint.y, currentPoint.x, currentPoint.y);
lastPoint = currentPoint;
}
}
void Bezier::drawHandles()
{
glColor3d(red, green, blue);
for (int i = 0; i < 3; i++)
{
drawCircle(points[i].x, points[i].y, points[i].radius);
}
}
标题
class Point
{
public:
float x;
float y;
float radius = 5;
};
class Bezier
{
public:
Bezier(float startX, float startY, float endX, float endY, float red, float green, float blue);
Point evaluate(float time);
void drawCurve();
void drawHandles();
Point points[3];
float red;
float green;
float blue;
};
在此先感谢您的帮助!
最佳答案
你使用的公式是错误的
P.x = (1 - t)*(1 - t)*points[0].x +
2*(1 - t)*t*points[1].x +
t*t*points[2].x;
P.y = (1 - t)*(1 - t)*points[0].y +
2*(1 - t)*t*points[1].y +
t*t*points[2].y;
你在第二个学期有一个额外的(1-t)
。
关于c++ - 我的贝塞尔曲线有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43813290/