本文介绍了libgdx 绘制圆弧曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

libgdx 的 arc 函数不是画圆弧而是画一个饼段(即有 2 条线连接到圆弧的原点)

The arc function of libgdx instead of drawing a arc draws a pie segment (ie. has 2 lines connecting to the arc's origin)

shapeRenderer.begin(ShapeType.Line);
shapeRenderer.arc(x, y, radius, 30, 120);
shapeRenderer.end();

有没有办法解决这个问题,让libgdx可以画出类似于html5 canvas arc函数的圆弧曲线?

Is there a solution to this problem so that libgdx can draw an arc curve similar to the html5 canvas arc function?

推荐答案

阅读源码,这似乎是内置行为:

Reading the source code, this seems built-in behavior:

/** Draws an arc using {@link ShapeType#Line} or {@link ShapeType#Filled}. */
public void arc (float x, float y, float radius, float start, float degrees, int segments) {
    if (segments <= 0) throw new IllegalArgumentException("segments must be > 0.");
    float colorBits = color.toFloatBits();
    float theta = (2 * MathUtils.PI * (degrees / 360.0f)) / segments;
    float cos = MathUtils.cos(theta);
    float sin = MathUtils.sin(theta);
    float cx = radius * MathUtils.cos(start * MathUtils.degreesToRadians);
    float cy = radius * MathUtils.sin(start * MathUtils.degreesToRadians);

    if (shapeType == ShapeType.Line) {
        check(ShapeType.Line, ShapeType.Filled, segments * 2 + 2);

        renderer.color(colorBits);
        renderer.vertex(x, y, 0);           <--- CENTER
        renderer.color(colorBits);
        renderer.vertex(x + cx, y + cy, 0); <--- LINE TO START POINT
        for (int i = 0; i < segments; i++) {
            renderer.color(colorBits);
            renderer.vertex(x + cx, y + cy, 0);
            float temp = cx;
            cx = cos * cx - sin * cy;
            cy = sin * temp + cos * cy;
            renderer.color(colorBits);
            renderer.vertex(x + cx, y + cy, 0);
        }
        renderer.color(colorBits);
        renderer.vertex(x + cx, y + cy, 0); <-- LINE TO END POINT
...

最简单的方法可能是复制整个函数并至少去掉我标记的两行:CENTERLINE TO END POINT,以及每个上面的随附 renderer.color(.. 行.

Easiest is probably to copy the entire function and at least throw out two of the lines I marked: CENTER and LINE TO END POINT, along with the accompanying renderer.color(.. line above each.

(您也可以删除 LINE TO START POINT - 它似乎设置曲线的起点,但实际上也是在循环内完成,所以它是多余的.)

(You can also delete the LINE TO START POINT – it appears to set the starting point for the curve, but that's actually also done inside the loop, so it's redundant.)

该函数有一个填充弧"的第二部分(我同意,它应该被正确命名为饼"或楔"),但你不需要在这里,因为它会完全一样.

The function has a second part for a filled "arc" (I agree, it should have properly been named "pie" or "wedge"), but you don't need that here because it would do exactly the same.

如果你让它工作,你可以将它推荐给 libgdx 的维护者,例如在 libgdx 的贡献论坛.

If you get it to work, you could propose it to libgdx's maintainers, for example on libgdx's Contributions Forum.

这篇关于libgdx 绘制圆弧曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 23:25