java - 使用 libgdx 确定矩形中 4 个多边形的触摸/单击

标签 java geometry libgdx

我目前正在使用 libgdx,并尝试从 矩形 获取 4 个相等的多边形:

Rectangle myRect = Rectangle(0, 0, 171, 171);

我正在寻找代表 矩形 每条边的 4 个多边形 enter image description here

这是我第一天使用这个引擎,我对我的几何形状有点生疏,所以我正在寻求任何可以获得的帮助。本质上,我将使用这些多边形来确定指定的 X,Y 对是否在其中。

感谢您的帮助。

最佳答案

你可以很容易地找到矩形的中点,只需平均高度和宽度即可。从那里您可以手动构建一个多边形,从一个角跳到另一个角再到中点。由于舍入,您会损失一些精度,但如果需要 double ,可以使用 getX()getWidth()

public Polygon[] findTris(Rectangle rectangle){
    //Creating a list of the x points of the rectangle, ordered clockwise.
    new int[] xpoints = new int[5];
    xpoints[0] = rectangle.x;
    xpoints[1] = rectangle.x+rectangle.width;
    xpoints[2] = rectangle.x+rectangle.width;
    xpoints[3] = rectangle.x;
    xpoints[4] = rectangle.x;

    //Doing the same for y points.
    int[] ypoints = new int[5];
    ypoints[0] = rectangle.y;
    ypoints[1] = rectangle.y;
    ypoints[2] = rectangle.y+rectangle.height;
    ypoints[3] = rectangle.y+rectangle.height;
    ypoints[4] = rectangle.y;

    //Finding the midpoint.
    int midx = (rectangle.x+rectangle.width)/2;
    int midy = (rectangle.y+rectangle.height)/2;

    //Creating an array to hold the polygons.
    Polygon[] polys = new Polygon[4];

    //Creating the polygons.
    for(int i = 0; i < 4; i++){
        int[] triXPoints = {xpoints[i], xpoints[i+1], midx};
        int[] triYPoints = {ypoints[i], ypoints[i+1], midy};
        polys[i] = Polygon(xpoints,ypoints,3);
    }
    return polys;
}

现在可以正常工作了,但如果您只想找到正方形中的鼠标位置,则可以使用鼠标 map 。鼠标 map 是一个在您希望能够识别鼠标的每个区域中具有明显不同颜色的图像。您可以将 map 存储为 BufferedImage ,并且每当您需要查找鼠标区域时中,您可以在 BufferedImage 上的适当位置获取缓冲图像的颜色。
想法如下:
/image/iFPsl.png

关于java - 使用 libgdx 确定矩形中 4 个多边形的触摸/单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21359322/

相关文章:

java - AndroidLauncher 找不到 Assets ,但 DesktopLauncher 可以

java - 如何在 Spring-Shell 中屏蔽输入字符串

java - 如何在应用程序启动时调用 AIDL 服务

c - 带视频的opencv圆检测

javascript - Openlayers 几何形状在缩小时看起来扭曲

java - 递归地创建 Apollo 垫圈[有解决方案]

java - 在 LibGDX 中使用抽象屏幕

java - 拖入 libgdx 闪烁

java - 如何使用 Selenium WebDriver 测试 XSS 漏洞?

java - 有没有办法在 Maven 尝试解决它们之前安装 Maven 依赖项?