2011-09-18 119 views
3

我正在尋找算法來計算落在矩形區域內的六邊形,無論是否裁剪。在矩形內計算六邊形?

我瞭解以下內容:

rectWidth = 1280; 
rectHeight = 720; 
hexRadius = 50; // middle to corner 
hexHeight = hexRadius * 2; 
hexShortSpan = hexRadius * 0.5; 
hexLongSpan = cos(radians(30)) * hexRadius; 
hexWidth = hexLongSpan * 2; 
hexSide = hexRadius + hexShortSpan; // this is not a side but side + shortSpan for horizontal rows 

想不通國防部運算得到正確的結果。

float A = rectWidth/hexWidth; 
float B = rectHeight/hexSide; 
float hexCount = A * B +????; 
// etc. etc. not sure about the rest... 

羅布

下面是一個圖片...

http://moggach.com/media/img/hexGrid.jpg

+0

你對六角形定位有什麼瞭解?你有'錨'六角?什麼是定位?像我畫或旋轉? – Ante

+0

不知道你'錨'的意思是什麼'尖',即。旋轉 – mogga

+0

帶錨我的意思是在矩形中定義一個六邊形的位置。它可以是輸入參數,也可以有一些請求。在圖片中,矩形的左下角也是六角形的「左下角」。所有配置都適用嗎? – Ante

回答

0

計算六邊形的區域,並與六邊形的區域劃分矩形的面積。

六邊形的面積是

A * A *(3 * SQRT(3))/ 2

其中a是六邊形的一邊的長度。

+0

雖然這不能解釋切割爲六角形的區域 - 如果三角形六邊形合計到六邊形區域,則計爲一個。 .. – Per

+0

是啊,這是隻有整個六角形區域,並且不知道矩形兩邊的裁剪六邊形 – mogga

+0

誰給了這個評價?這不是一個解決方案。 – mogga

0

提示:計算一邊的六角的數量。例如。計算所有十六進制右角(X):...

在第一個六邊形中間的左上角,情況不太一般。它可以與計數來解決:

  • 多少不吉利的東西都在第一排(由上邊界分割)
  • 多少不吉利的東西都在第二行(僅低於第一個)
  • 有多少行的第一行型(每秒從第一行)
  • 多少行是第二行類型(每秒從第二行)

的這是代碼蟒版本:

from math import ceil, cos, sqrt 
rectWidth = 1280 
rectHeight = 720 
hexRadius = 50 
hexHeight = hexRadius * 2 
hexHalfHeight = hexRadius/2 
hexWidth = hexRadius * sqrt(3) 

# First one is corner, ceil() calculates others in row 
num_in_first_row = 1 + ceil((rectWidth - hexWidth) /hexWidth) 
num_in_second_row = ceil(rectWidth/hexWidth) 

# First one is corner, ceil() starts from upper point of next hex on left border 
num_of_first_rows = 1 + ceil((rectHeight - 2 * hexRadius)/(hexHeight + hexRadius)) 
# Starts from upper point of first hex in second row. Coords (hexWidth/2., hexHalfHeight) 
num_of_second_rows = ceil((rectHeight - hexHalfHeight)/(hexHeight + hexRadius)) 

num = num_in_first_row * num_of_first_rows + num_in_second_row * num_of_second_rows 
print num 
+0

尋找一個編程解決方案 - 通過並找出每個六邊形中的哪個頂角位於矩形內並非不可能但效率不高 – mogga

+0

您只需找到第一個(可能是輸入參數),因爲相鄰的列位於3/2邊緣長度距離,垂直於sqrt(3)/ 4邊緣長度。你已經擁有的價值。 – Ante

+0

還應該提到我正在處理尖尖的六邊形網格佈局。 (旋轉90度) – mogga