gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:34:56 24 4
gpt4 key购买 nike

我目前正在使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com