libgdx shaperenderer旋转如何查找旋转矩形的起始,结束X,Y坐标?

时间:2014-09-18 18:26:44

标签: libgdx

我有一个问题。我可以将矩形旋转一定的角度,但是通过这个旋转,起始的X和Y坐标不会改变矩形,只会改变矩形上的视图。那么怎么可能在旋转过程中找出旋转矩形的旋转起始,结束X和Y坐标(对不起这句话中的太多旋转:P)

我希望有人能帮助我。

感谢。

1 个答案:

答案 0 :(得分:0)

/** @param x the x of the rectangle
 *  @param y the y of the rectangle
 *  @param width the width of the rectangle
 *  @param height the height of the rectangle
 *  @param radians the desired rotation of the rectangle (in radians)
 *  @param output The array to store the results in. A new one will be created if it is null or its length is less than 8.
 *  @return the given output array with the rotated vertices as in [x1, y1, x2, y2, x3, y3, x4, y4] starting from the given offset */
public static float[] rotate(float x, float y, float width, float height, float radians, float[] output, int offset) {
    if(output == null || offset + 8 > output.length - 1)
        output = new float[8];
    // http://www.monkeycoder.co.nz/Community/posts.php?topic=3935
    float rad = (float) (Math.sqrt(height * height + width * width) / 2.);
    float theta = com.badlogic.gdx.math.MathUtils.atan2(height, width);
    float x0 = (float) (rad * Math.cos(theta + radians));
    float y0 = (float) (rad * Math.sin(theta + radians));
    float x1 = (float) (rad * Math.cos(-theta + radians));
    float y1 = (float) (rad * Math.sin(-theta + radians));
    float offsetX = x + width / 2, offsetY = y + height / 2;
    output[offset] = offsetX + x0;
    output[offset + 1] = offsetY + y0;
    output[offset + 2] = offsetX + x1;
    output[offset + 3] = offsetY + y1;
    output[offset + 4] = offsetX - x0;
    output[offset + 5] = offsetY - y0;
    output[offset + 6] = offsetX - x1;
    output[offset + 7] = offsetY - y1;
    return output;
}