aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMario Zechner <contact@badlogicgames.com>2016-05-11 10:42:42 +0200
committerMario Zechner <contact@badlogicgames.com>2016-05-11 10:42:42 +0200
commit86e61a315f482c36d1e0380c094d1f35401d4b12 (patch)
tree957b19036e5de3fe4fba38be862dd5b675f7db31
parenta8f38b532ef0992a48d58708290c835e14b8a153 (diff)
parent908ee97172607b2345a2ce8ce06e743b009a6a2f (diff)
downloadlibgdx-86e61a315f482c36d1e0380c094d1f35401d4b12.tar.gz
Merge pull request #4008 from jhnn/master
ShapeRenderer draw rotated Ellipses
-rw-r--r--gdx/src/com/badlogic/gdx/graphics/glutils/ShapeRenderer.java47
-rw-r--r--tests/gdx-tests/src/com/badlogic/gdx/tests/ShapeRendererTest.java8
2 files changed, 55 insertions, 0 deletions
diff --git a/gdx/src/com/badlogic/gdx/graphics/glutils/ShapeRenderer.java b/gdx/src/com/badlogic/gdx/graphics/glutils/ShapeRenderer.java
index 4e4d00111..30fac3b55 100644
--- a/gdx/src/com/badlogic/gdx/graphics/glutils/ShapeRenderer.java
+++ b/gdx/src/com/badlogic/gdx/graphics/glutils/ShapeRenderer.java
@@ -930,6 +930,53 @@ public class ShapeRenderer implements Disposable {
}
}
}
+
+ /** Calls {@link #ellipse(float, float, float, float, float, int)} by estimating the number of segments needed for a smooth ellipse. */
+ public void ellipse (float x, float y, float width, float height, float rotation) {
+ ellipse(x, y, width, height, rotation, Math.max(1, (int)(12 * (float)Math.cbrt(Math.max(width * 0.5f, height * 0.5f)))));
+ }
+
+ /** Draws an ellipse using {@link ShapeType#Line} or {@link ShapeType#Filled}. */
+ public void ellipse (float x, float y, float width, float height, float rotation, int segments) {
+ if (segments <= 0) throw new IllegalArgumentException("segments must be > 0.");
+ check(ShapeType.Line, ShapeType.Filled, segments * 3);
+ float colorBits = color.toFloatBits();
+ float angle = 2 * MathUtils.PI / segments;
+
+ rotation = MathUtils.PI * rotation / 180f;
+ float sin = MathUtils.sin(rotation);
+ float cos = MathUtils.cos(rotation);
+
+ float cx = x + width / 2, cy = y + height / 2;
+ float x1 = width * 0.5f;
+ float y1 = 0;
+ if (shapeType == ShapeType.Line) {
+ for (int i = 0; i < segments; i++) {
+ renderer.color(colorBits);
+ renderer.vertex(cx + cos * x1 - sin * y1, cy + sin * x1 + cos * y1, 0);
+
+ x1 = (width * 0.5f * MathUtils.cos((i + 1) * angle));
+ y1 = (height * 0.5f * MathUtils.sin((i + 1) * angle));
+
+ renderer.color(colorBits);
+ renderer.vertex(cx + cos * x1 - sin * y1, cy + sin * x1 + cos * y1, 0);
+ }
+ } else {
+ for (int i = 0; i < segments; i++) {
+ renderer.color(colorBits);
+ renderer.vertex(cx + cos * x1 - sin * y1, cy + sin * x1 + cos * y1, 0);
+
+ renderer.color(colorBits);
+ renderer.vertex(cx, cy, 0);
+
+ x1 = (width * 0.5f * MathUtils.cos((i + 1) * angle));
+ y1 = (height * 0.5f * MathUtils.sin((i + 1) * angle));
+
+ renderer.color(colorBits);
+ renderer.vertex(cx + cos * x1 - sin * y1, cy + sin * x1 + cos * y1, 0);
+ }
+ }
+ }
/** Calls {@link #cone(float, float, float, float, float, int)} by estimating the number of segments needed for a smooth
* circular base. */
diff --git a/tests/gdx-tests/src/com/badlogic/gdx/tests/ShapeRendererTest.java b/tests/gdx-tests/src/com/badlogic/gdx/tests/ShapeRendererTest.java
index 157865943..cb9aac5d9 100644
--- a/tests/gdx-tests/src/com/badlogic/gdx/tests/ShapeRendererTest.java
+++ b/tests/gdx-tests/src/com/badlogic/gdx/tests/ShapeRendererTest.java
@@ -85,6 +85,10 @@ public class ShapeRendererTest extends GdxTest {
renderer.setColor(Color.MAGENTA);
renderer.triangle(-0.1f, 0.1f, -0.6f, -0.1f, -0.3f, 0.5f);
+ renderer.setColor(Color.GOLD);
+ renderer.ellipse(0.7f, -0.1f, 0.3f, 0.1f, 45f, 40);
+ renderer.ellipse(0.7f, -0.1f, 0.3f, 0.1f, 135f);
+
renderer.end();
} else {
renderer.begin(ShapeType.Line);
@@ -109,6 +113,10 @@ public class ShapeRendererTest extends GdxTest {
renderer.setColor(Color.CYAN);
renderer.curve(0.0f, 0.25f, 0.2f, 0.3f, 0.3f, 0.6f, 0.1f, 0.5f, 30);
+
+ renderer.setColor(Color.GOLD);
+ renderer.ellipse(0.7f, -0.1f, 0.3f, 0.1f, 45f, 40);
+ renderer.ellipse(0.7f, -0.1f, 0.3f, 0.1f, 135f);
renderer.end();
}