你的想法背後計算座標,據我所知是正確的。我只能猜測,你得到奇怪座標的原因是因爲你一遍又一遍地重複編輯同一個位置(儘管因爲你只提供了一部分代碼片段,我不能保證這是如此)。
行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