嗨!我有一個有趣的問題。
我已經在HTML畫布上繪製的一系列六邊形的(忽略點)
對於每個六邊形我有它的X和其中心的y座標,這就是我如何識別它們。
我存儲在數組中,其中我存儲六邊形
function Hexagon()
{
this.shape
this.x
this.y
}
六邊形稱爲六邊形我具有與鑑於這種給定的六邊形
function get_adjacent_hexagons(hexagon)
{
var x = hexagon.x
var y = hexagon.y
var adjacents = []
for(var i=0; i<editor_hexagons.length; i++)
{
if(editor_hexagons[i].x === x && editor_hexagons[i].y == y - (2*s))
{
adjacents.push(editor_hexagons[i])
}
if(editor_hexagons[i].x === x && editor_hexagons[i].y == y + (2*s))
{
adjacents.push(editor_hexagons[i])
}
if(editor_hexagons[i].x === x - (s*1.5) && editor_hexagons[i].y == y - s)
{
adjacents.push(editor_hexagons[i])
}
if(editor_hexagons[i].x === x - (s*1.5) && editor_hexagons[i].y == y + s)
{
adjacents.push(editor_hexagons[i])
}
if(editor_hexagons[i].x === x + (s*1.5) && editor_hexagons[i].y == y - s)
{
adjacents.push(editor_hexagons[i])
}
if(editor_hexagons[i].x === x + (s*1.5) && editor_hexagons[i].y == y + s)
{
adjacents.push(editor_hexagons[i])
}
}
return adjacents
}
的所有相鄰六邊形返回一個對象的函數信息,是否有可能做一個算法來檢查是否所有的六邊形都連接
這就是說,如果沒有六邊形或一組六邊形?
例如,在圖片中提供了它們都已連接。
----- -----編輯
這似乎是工作
function check_connection()
{
visited = []
visit_hexagon(hexagons[0])
}
function visit_hexagon(hexagon)
{
var not_visited = true
for(var i=0; i<visited.length; i++)
{
if(visited[i] === hexagon)
{
not_visited = false
break
}
}
if(not_visited)
{
visited.push(hexagon)
var adjacents = get_adjacent_hexagons(hexagon)
for(var i=0; i<adjacents.length; i++)
{
visit_hexagon(adjacents[i])
}
}
}
是的,當然這是可能的。然而,鑑於你的'get_adjacent_hexagons'實現,我懷疑它會有效。 – Bergi
'hexagons'數組和'editor_hexagons'變量有什麼區別?爲什麼'adjacents'是一個全局變量? – Bergi
@Bergi你是對的,通過editor_hexagons我的意思是六邊形,讓我編輯它。 – madprops