这是我的曲线绘制代码,但此代码仅适用于一种颜色。
public class Test extends ApplicationAdapter{
//create paths
private Bezier<Vector2> path1;
private ShapeRenderer sr;
@Override
public void create () {
// set up random control points
int width = Gdx.graphics.getWidth();
int height = Gdx.graphics.getHeight();
int points = 4;
Vector2[] controlPoints = new Vector2[points];
for (int i = 0; i < points; i++) {
int x = (int) (Math.random() * width) ;
int y = (int) (Math.random() * height);
Vector2 point = new Vector2(x, y);
controlPoints[i] = point;
}
path1 = new Bezier<Vector2>(controlPoints);
sr = new ShapeRenderer();
sr.setAutoShapeType(true);
}
@Override
public void render () {
Gdx.gl.glClearColor(0f, 0f, 0f, 0f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
sr.begin();
sr.setColor(Color.WHITE);
//draw path1
for(int i = 0; i < 100; ++i){
float t = i /100f;
// create vectors to store start and end points of this section of the curve
Vector2 st = new Vector2();
Vector2 end = new Vector2();
// get the start point of this curve section
path1.valueAt(st,t);
// get the next start point(this point's end)
path1.valueAt(end, t-0.01f);
// draw the curve
sr.line(st.x, st.y, end.x, end.y);
}
sr.end();
}
@Override
public void dispose () {
}
}
我可以通过以下两种方法使用渐变色绘制一个矩形和一条线
shapeRenderer.filledRect(x, y, width, height, lightBlue, lightBlue, darkBlue, darkBlue);
和
shapeRenderer.line(x, y, x2, y2, Color.RED, Color.GREEN);
但是我需要使用渐变色绘制曲线,我已经搜索了很多并且还没有找到任何解决方案。
所以请帮助我。
答案 0 :(得分:4)
您可以使用开始和结束颜色之间的线性插值为每个部分找到颜色。像这样:
Color color = new Color();
@Override
public void render() {
Gdx.gl.glClearColor(0f, 0f, 0f, 0f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
sr.begin();
Color startColor = Color.YELLOW;
Color endColor = Color.RED;
//draw path1
for (int i = 0; i < 100; ++i) {
float t = i / 100f;
//interpolate linearly between start and end colors
sr.setColor(color
.set(startColor)
.lerp(endColor, t)
);
// create vectors to store start and end points of this section of the curve
Vector2 st = new Vector2();
Vector2 end = new Vector2();
// get the start point of this curve section
path1.valueAt(st, t);
// get the next start point(this point's end)
path1.valueAt(end, t - 0.01f);
// draw the curve
sr.line(st.x, st.y, end.x, end.y);
}
sr.end();
}