你的想法背后计算坐标,据我所知是正确的。我只能猜测,你得到奇怪坐标的原因是因为你一遍又一遍地重复编辑同一个位置(尽管因为你只提供了一部分代码片段,我不能保证这是如此)。
行Location spawnPoint = mapCenter
不会创建新位置,它只会创建一个名为spawnPoint
的引用,指向mapCenter
。
位置的add
方法也不会返回新的Location
实例。由于应该通过将x和y分量添加到中心位置来找到多边形的每个顶点,您必须复制或克隆mapCenter
变量,以便不编辑/更改地图的原始中心。我假设您使用循环来创建/查找多边形的每个顶点位置,并且不会复制变量mapCenter
,这将发生:
第1次迭代:角度为0º,将40
添加到x坐标的spawnPoint
(这改变mapCenter
)和0
至spawnPoint
的z坐标。假设地图的中心原本是0,0,0,坐标现在是40,0,0(这仍然是正确的)。
第二迭代:角为90°,加上0
到X的spawnPoint
坐标(再一次改变centerMap
,这我们在最后一次迭代中已经编辑)的spawnPoint
和40
协调与z。现在mapCenter
的坐标是40,0,40,这是不正确的。我们应该将新组件添加到mapCenter
的新副本中。
修复此使用Location spawnPoint = mapCenter.clone()
。示例代码:
public static List<Location> getPolygonVertices(float radius, int edges, Location center) {
List<Location> vertices = new ArrayList<Location>();
float angleDelta = 360/edges; // The change in angle measure we use each iteration
for (int i = 0; i < edges; i++) { // For each vertix we want to find
float angle = angleDelta * i; // Calculate the angle by multiplying the change (angleDelta) with the number of the edge
Location corner = center.clone(); // Clone the center of the map
corner.add(Math.cos(Math.toRadians(angle)) * radius, 0, Math.sin(Math.toRadians(angle)) * radius); // Add the x and z components to the copy
vertices.add(corner);
}
return vertices;
}
您可以使用此方法像这样:
List<Location> vertices = getPolygonVertices(40, 4, mapCenter);
,它会返回到正确的位置([40 | 0],[0 | 40],[-40 | 0 ],[0 | -40])。
“它没有工作”没有提供任何相关信息来帮助解决您的问题。 – Unihedron
您的预期分数适用于45°,-45°,-135°,135°半径为56.5的角度。你应该得到的是(40,0),(0,40),(-40,0),(0,-40)。或者(28,28),( - 28,28)等'teamAngle = angle *(teamNumber + 0.5)'。 – LutzL