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

How to draw a square composed of four triangles ? ( libgdx )

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

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

这是一个简单的例子,我没法测试,因为我现在无法访问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.

的网格

只是您需要调整代码以在每个三角形中绘制纹理,或者根据您的外观绘制颜色。

如果你需要更多的代码来让它工作,请评论,如果我可以的话,我会告诉你。

如果不行,对你有用,评论,我会删除

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

希望对您有所帮助