如何绘制由四个三角形组成的正方形? (libgdx)

时间:2015-05-06 02:49:28

标签: java libgdx shapes

我有一个案例,我需要画出由四个三角形组成的正方形,如下图所示:

enter image description here

三角形参数存储在JDBC中,我知道如何在Libgdx中绘制形状,但这种形状对我来说似乎有点棘手,任何帮助或任何关于如何做到这一点的想法将不胜感激。 (我不是要求为我编写代码)

2 个答案:

答案 0 :(得分:1)

这是一个简单的例子,我不能呃测试,因为我现在无法访问libgdx,但我认为,作为一个想法,你可以提供帮助。

void draw(float x, float y, float width, float height, Color color) {


    if (idx==verts.length)
        flush();

    //assuming (0, 0) is lower left, and Y is up

    //bottom left vertex
    verts[idx++] = x;           //Position(x, y) 
    verts[idx++] = y;
    verts[idx++] = color.r;             //Color(r, g, b, a)
    verts[idx++] = color.g;
    verts[idx++] = color.b;
    verts[idx++] = color.a;

    //top left vertex
    verts[idx++] = x;           //Position(x, y) 
    verts[idx++] = y + height;
    verts[idx++] = color.r;             //Color(r, g, b, a)
    verts[idx++] = color.g;
    verts[idx++] = color.b;
    verts[idx++] = color.a;

    //bottom right vertex
    verts[idx++] = x + (width / 2);         //Position(x, y) 
    verts[idx++] = y + (height / 2);
    verts[idx++] = color.r;             //Color(r, g, b, a)
    verts[idx++] = color.g;
    verts[idx++] = color.b;
    verts[idx++] = color.a;
            //2
            //|\
            //| \ 3
            //| /
            //|/
            //1

    //
    verts[idx++] = x + width;       //Position(x, y) 
    verts[idx++] = y + height;
    verts[idx++] = color.r;             //Color(r, g, b, a)
    verts[idx++] = color.g;
    verts[idx++] = color.b;
    verts[idx++] = color.a;

    //
    verts[idx++] = x;           //Position(x, y) 
    verts[idx++] = y + height;
    verts[idx++] = color.r;             //Color(r, g, b, a)
    verts[idx++] = color.g;
    verts[idx++] = color.b;
    verts[idx++] = color.a;

    //
    verts[idx++] = x + (width / 2);         //Position(x, y) 
    verts[idx++] = y + (height / 2);
    verts[idx++] = color.r;             //Color(r, g, b, a)
    verts[idx++] = color.g;
    verts[idx++] = color.b;
    verts[idx++] = color.a;

            //2_____1
            // \   /
            //  \ /
            //   3
            //
            //

            //
    verts[idx++] = x + width;       //Position(x, y) 
    verts[idx++] = y;
    verts[idx++] = color.r;             //Color(r, g, b, a)
    verts[idx++] = color.g;
    verts[idx++] = color.b;
    verts[idx++] = color.a;

    //
    verts[idx++] = x + width;       //Position(x, y) 
    verts[idx++] = y + height;
    verts[idx++] = color.r;             //Color(r, g, b, a)
    verts[idx++] = color.g;
    verts[idx++] = color.b;
    verts[idx++] = color.a;

    //
    verts[idx++] = x + (width / 2);         //Position(x, y) 
    verts[idx++] = y + (height / 2);
    verts[idx++] = color.r;             //Color(r, g, b, a)
    verts[idx++] = color.g;
    verts[idx++] = color.b;
    verts[idx++] = color.a;

            //      2
            //     /|
            //   3/ |
            //    \ |
            //     \|
            //      1

    //
    verts[idx++] = x;                   //Position(x, y) 
    verts[idx++] = y;
    verts[idx++] = color.r;             //Color(r, g, b, a)
    verts[idx++] = color.g;
    verts[idx++] = color.b;
    verts[idx++] = color.a;

    //
    verts[idx++] = x + width;       //Position(x, y) 
    verts[idx++] = y;
    verts[idx++] = color.r;             //Color(r, g, b, a)
    verts[idx++] = color.g;
    verts[idx++] = color.b;
    verts[idx++] = color.a;

    //
    verts[idx++] = x + (width / 2);         //Position(x, y) 
    verts[idx++] = y + (height / 2);
    verts[idx++] = color.r;             //Color(r, g, b, a)
    verts[idx++] = color.g;
    verts[idx++] = color.b;
    verts[idx++] = color.a;

            //   3
            //  /\
            // /__\
            // 1  2 
    flush();
}

注意:当我写作时,答案没有被记住,这个例子是针对libgdx的网格。

只有您必须调整代码才能在每个三角形中绘制纹理,或者为您的外观绘制颜色。

如果您需要,可以使用更多代码使其发挥作用,发表评论,以及我是否可以说你。

如果没有,它适合你,评论,我会删除

答案 1 :(得分:1)

使用四次调用ShapeRenderer.triangle()

示例(未经测试)......

// Assumes you set a shapes roperty in your create method
// e.g. this.shapes = new ShapeRenderer();

public void square(float x, float y, float width, float height, Color color) {

    float centerX = width / 2;
    float centerY = height / 2;
    float x2 = x + width;
    float y2 = y + height;

    shapes.begin(ShapeRenderer.ShapeType.Filled);
    shapes.triangle(x, y, centerX, centerY, x2, y, color, color, color);
    shapes.triangle(x2, y, centerX, centerY, x2, y2, color, color, color);
    shapes.triangle(x2, y2, centerX, centerY, x, y2, color, color, color);
    shapes.triangle(x, y2, centerX, centerY, x, y, color, color, color);
}

我希望这会有所帮助