我正在尝试使用libgdx绘制贝塞尔曲线。
Vector2 pixel0 = Line.CalculateBezierPoint(0, p0, p1, p2, p3);
System.out.print(pixel0.x);
for(int i = 1; i <= 10; i++)
{
System.out.print(pixel0.x);
float t = i / (float) 10;
Vector2 pixel1 = Line.CalculateBezierPoint(t, p0, p1, p2, p3); //after this line pixel0 equals pixel1, why?
//batch.draw(rect, pixel.y, pixel.x, 3, 3);
//shapeRenderer = new ShapeRenderer();
Gdx.gl20.glLineWidth(2);
shapeRenderer.setProjectionMatrix(camera.combined);
shapeRenderer.begin(ShapeType.Line);
shapeRenderer.setColor(255,255,255,255);
shapeRenderer.line(pixel0, pixel1);
shapeRenderer.end();
pixel0 = pixel1;
}
public class Line {
public static Vector2 CalculateBezierPoint(float t, Vector2 p0, Vector2 p1, Vector2 p2, Vector2 p3) {
float u = 1 - t;
float tt = t*t;
float uu = u*u;
float uuu = uu * u;
float ttt = tt * t;
Vector2 p = p0.scl(uuu);
p.add(p1.scl(3 * uu * t));
p.add(p2.scl(3 * u * tt));
p.add(p3.scl(ttt));
return p;
}
}
如果pixel0 = pixel1存在或不存在则无关紧要。每次pixel0都是像素一样,但是怎么样,为什么?
编辑; 工作代码:
Vector2 pixel0 = new Vector2(Line.CalculateBezierPoint(0, p0, p1, p2, p3));
for(int i = 1; i <= 2; i++)
{
float t = i / (float) 2;
Vector2 pixel1 = new Vector2(Line.CalculateBezierPoint(t, p0, p1, p2, p3));
//batch.draw(rect, pixel.y, pixel.x, 3, 3);
//shapeRenderer = new ShapeRenderer();
Gdx.gl20.glLineWidth(2);
shapeRenderer.setProjectionMatrix(camera.combined);
shapeRenderer.begin(ShapeType.Line);
shapeRenderer.setColor(255,255,255,255);
shapeRenderer.line(pixel0, pixel1);
shapeRenderer.end();
pixel0 = pixel1;
}
CalculateBezierPoint是一个静态函数,我试图每次都释放新函数,并且它是有效的。 但现在它绘制的是简单的线条,而不是曲线。 我用过:http://devmag.org.za/2011/04/05/bzier-curves-a-tutorial/ 并将CalculateBezierPoint函数转换为Java。有人检查一下,如果我做得好吗?