2017-10-23 2 views
-1

Ich muss ein Kreisdiagramm in libgdx für eine Finanzanwendung erstellen. Können Sie mir bitte ein Beispiel oder Code-Snippet zeigen, um eine Idee zu haben?Einen Kreis und einen Bogen in libgdx zeichnen

+1

Mögliche Duplikat [Libgdx Zeichnung arc curve] (https://stackoverflow.com/questions/30699321/libgdx-drawing-arc-curve) – Aryan

Antwort

0

Sie shaperenderer verwenden können: lesen diese classMethods shaperendere hat:

  • Bogen (float x, float y, float Radius, Start float, float Grad)

  • Kreis (float x, float y, float Radius)

  • Kurve (float x1, y1 float, float CX1, float CY1, float CX2, float Cy2, Schwimmers x2, y2 float, int Segmente)

  • ....

ODER

public class Arc extends ShapeRenderer{ 

    private final ImmediateModeRenderer renderer; 
    private final Color color = new Color(1, 1, 1, 1); 

    public Arc(){ 
     renderer = super.getRenderer(); 
    } 

    public void arc (float x, float y, float radius, float start, float degrees) { 
    int segments = (int)(6 * (float)Math.cbrt(radius) * (degrees/360.0f)); 

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

    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); 
    } 
    } 
//// or 
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 
... 
} 
+0

Wenn ich Ihren Code ausprobiert habe, funktioniert es gut, aber beim Zeichnen des fortlaufenden Bogens für Kreisdiagramm gibt es eine Lücke, wenn ich Segmente verwende. Warum ? – iappmaker

+0

@iappmaker hat nicht verstanden, was du meinst. Bitte erläutern Sie mehr –

+0

Wenn ein Bogen gezeichnet wird, ist der gekrümmte Pfad nicht glatt .. es ist verwürfelt .. jeder Weg zu glätten – iappmaker