2016-11-10 67 views
2

我有以下六角形網格並試圖從中心計算度到每個邊緣六邊形(淺藍色): enter image description here如何獲得正確表示的極座標?

藍色突出顯示的十六進制爲0度正確,以及象限(右下)是正確的。這裏是我的角度計算方法:

private static function calculateAngle(hex1:Hexagon, hex2:Hexagon):Number { 
    // hex1 is always passed in as the grid center or start 
    var diffY:Number = Math.abs(hex2.center.y) - Math.abs(hex1.center.y); 
    var diffX:Number = Math.abs(hex2.center.x) - Math.abs(hex1.center.x); 
    var radians:Number = Math.atan(diffY/diffX); 

    return radians * 180/Math.PI; 
} 

爲什麼剩餘角度(每個六角形中的文本)不正確?

+1

「中心」屬性是「點」對象嗎? – null

+0

@null:是'center'是一個'Point'對象。 – IAbstract

+0

以爲在['Point'](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/geom/Point.html)中有角度的吸氣劑,但只有一個靜態方法polar(len:Number,angle:Number):Point'從極座標構造'Point'。沒有建立一個現有的極座標的方式。 – null

回答

2

你真的接近正確;您只需要補償atan的週期性。執行此操作的標準方法是使用atan2,它返回(-pi, pi]中的帶符號角度,而不是[0, pi)中的無符號角度。你可以這樣做是這樣的:

var radians:Number = Math.atan2(
    hex2.center.y - hex1.center.y, hex2.center.x - hex1.center.x); 

注意,我在裏面不包括呼叫abs:需要這些值的符號性的atan2知道哪個象限它了!

編輯:如果你正在尋找[0, pi]的角度,這表示中央十六進制和藍色高亮十六進制之間的最小角度,你可以採取的atan2結果的絕對值:return Math.abs(radians) * 180/Math.PI;這個問題讓你不清楚你要求哪一個。

+0

我這樣做,但左角應該是'> 180'爲負:'-173','-166' ...'-6'。所以我*規範化*:'如果(弧度<180)返回弧度+ 360' – IAbstract