我正在嘗試創建基於網格的遊戲。到目前爲止,我有一個六角形的基於區塊的網格,以協調方案,如下圖所示:在六角形網格中捕捉到最近的六角形中心
col 0
| col 1
| | col 2
| | |
__ | __ __ __ __
/00\__/02\__/04\__/06\__/08\__
\__/01\__/03\__/05\__/07\__/09\--- row 0
/10\__/12\__/14\__/16\__/18\__/
\__/11\__/13\__/15\__/17\__/19\--- row 1
/20\__/22\__/24\__/26\__/28\__/
\__/21\__/23\__/25\__/27\__/29\--- row 2
/30\__/32\__/34\__/36\__/38\__/
\__/ \__/ \__/ \__/ \__/ --- row 3
,看起來像這樣在現實生活中只是隨機顏色的每一個六邊形:
什麼我正在努力弄清楚的是,當用戶點擊一個六邊形時,我如何確定他們點擊了哪個六邊形?
到目前爲止,我已經嘗試過的代碼如下:
private: System::Void MyForm_MouseDown(System::Object^ sender,
System::Windows::Forms::MouseEventArgs^ e) {
int CloseI=0,CloseJ=0;
CloseJ = FindNearesetX(e->X);
CloseI = FindNearesetY(e->Y);
//Grid[down(y)][along(x)]
P1.X = Grid[CloseI][CloseJ].GetX();
P1.Y = Grid[CloseI][CloseJ].GetY();
} // END MOUSE DOWN EVENT
int FindNearesetX(int ActualX){
int ClosestJPos;
ClosestJPos = ((ActualX-Grid[0][0].GetX())/(1.5*HexSideLength));
return ClosestJPos;
}//END FIND NEAREST X
int FindNearesetY(int ActualY){
int ClosestIPos;
ClosestIPos = ((ActualY-Grid[0][0].getY())/(HexHeight));
return ClosestIPos;
}//END FIND NEAREST Y
private: System::Void MyForm_MouseMove(System::Object^ sender,
System::Windows::Forms::MouseEventArgs^ e) {
this->Invalidate();
P2.X = e->X;
P2.Y = e->Y;
} // END MOUSE MOVE EVENT
然而,這並沒有工作,我怎麼想,這是因爲當用戶點擊一個六邊形的中心點左側它卡到他們點擊的那個左邊的六邊形,並且如果他們點擊所有奇數列上的中心點上面,它就會對齊他們點擊的那個上面的六邊形。
我一直堅持這個爲期2天,真的想弄明白。 謝謝
[六角形網格,你如何找到一個點是在六邊形?(可能的重複http://stackoverflow.com/questions/7705228/hexagonal-grids-how-do-you-find-其中-hexagon-a-point-is-in) – Troyseph
我發佈這個問題已經2年了,但我確實記得看到那篇文章,我相信它沒有解決我的問題(儘管我確定它有幫助),但是,然而,對於我來說,記得它爲什麼沒有回答我的問題的具體內容太長了。奇怪的是,在這麼長時間後看到這篇文章的活動! aha – JabbaWook