1
\$\begingroup\$

I know most of you would suggest me to draw a filled polygon by using combination of triangles and rectangles , but that is not possible for complex shapes . I want to basically draw a generic shape on screen , here it is enter image description here

The above shape needs to be drawn in generic form , i.e -> the width of each trapezium's upper edge is fixed . -> the lower edge of the overall compound shape is a constant unit greater than upper edge. -> this whole compound shape can have arbitrary length and thus number of individual trapeziums that compose the compound shape is not fixed. I wish to draw each colored trapezium as a filled polygon . Is there any way of achieving this

\$\endgroup\$
1
  • \$\begingroup\$ How is it not possible to create this shape with triangles? for that matter, how is it not possible for any polygon of any shape? \$\endgroup\$
    – Attackfarm
    Jul 9, 2014 at 19:16

1 Answer 1

2
\$\begingroup\$

Copied from here: https://stackoverflow.com/questions/15733442/drawing-filled-polygon-with-libgdx

Basically, you define a PolygonSpriteBatch using a PolygonSprite and a PolygonRegion:

PolygonSprite poly;
PolygonSpriteBatch polyBatch;
Texture textureSolid;

You then create them like this, giving the region a texture and the 4 corners coordinates (for the texture, you can load a plain solid colored texture, gray or red in your case):

PolygonRegion polyReg = new PolygonRegion(new TextureRegion(textureSolid),
  new float[] {
    x1, y1,
    x2, y2,
    x3, y3,
    x4, y4
});
poly = new PolygonSprite(polyReg);
poly.setOrigin(oX, oY);
polyBatch = new PolygonSpriteBatch();

And draw it like this:

polyBatch.begin();
poly.draw(polyBatch);
polyBatch.end();

Hope I helped!

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .