gpt4 book ai didi

java - 如何在libgdx java中使用渐变颜色绘制曲线

转载 作者:搜寻专家 更新时间:2023-11-01 07:44:27 25 4
gpt4 key购买 nike

我想像这张图一样用渐变色画一条曲线gradient color curve image

这是我的曲线绘制代码,但此代码仅适用于一种颜色。

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);

但是我需要用渐变颜色绘制曲线,我搜索了很多,还没有找到任何解决方案。

所以请帮助我。

最佳答案

您可以使用开始颜色和结束颜色之间的线性插值找到每个部分的颜色。像这样:

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();
}

关于java - 如何在libgdx java中使用渐变颜色绘制曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47608606/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com