libgdx drawing arc curve(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
...
最简单的方法可能是复制整个函数并至少去掉我标记的两行:CENTER
和 LINE 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 绘制圆弧曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:libgdx 绘制圆弧曲线
- 如何指定 CORS 的响应标头? 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 转换 ldap 日期 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- 获取数字的最后一位 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01