forum.boolean.name

forum.boolean.name (http://forum.boolean.name/index.php)
-   Полезные функции (http://forum.boolean.name/forumdisplay.php?f=17)
-   -   Гексогонал-сетка.Проверка позиции мыши (http://forum.boolean.name/showthread.php?t=18036)

polopok 29.03.2013 19:19

Гексогонал-сетка.Проверка позиции мыши
 
Вот ,возможно пригодиться код для создания гексогональной сетки .
Ячейка сетки длина\высота соттветственно Х\Х (тоесть 50 на 50 или 60 на 60 и т.д. ...).
Код:


Graphics 800,600,32 ,2

imgtile= CreateImage(41,41)
SetBuffer ImageBuffer(imgtile)
Color 250,50,50
Line 0,10, 20,0
Line 20,0, 40,10
Line 40,10,40,30
Line 40,30 ,20,40
Line 20,40,0,30
Line 0,30,0,10
Color 255,255,255
imgtile0=CreateImage(41,21)
SetBuffer ImageBuffer(imgtile0) ;картинка мыши
        Color 5,0,255
        Rect  10,0,20,20,0
        Color 255,255,255
SetBuffer BackBuffer()




d =40;  :dd =(d/2) : ddd =(dd/2) : mm=d/4*3: rr=d+dd
tilex=10  : tiley=10 ;количество ячеек    i x j
sx=  0

FlushMouse
While Not KeyHit(1)
ClsColor 135,160,160
Cls
mx# = MouseX() : my# = MouseY()

        ; управление отображением сетки
        If KeyDown(205) Then sx=1  ;  <--  -->
        If KeyDown(203) Then sx=0
       
        ; рисуем гексо-сетку
        For j=0 To tiley
        For i=0 To tilex
               
                x = (d*i+(j Mod 2)*d/2)
                y = (j*d/4*3)
                       
                If sx =0 Then       
                DrawImage imgtile,x ,y
                Else If sx=1
                Rect x ,y,d,d,0
                EndIf
                Text x+10 ,y+15,i+" : "+j
        Next
        Next
       
        ;qx# = Floor#()
        qy# = Floor# (( my# )  / (d+0.15) * 4/3 - 0.15 ); вычисление номера ячейки по вертикали
        qx# = Floor# (( mx#  ) / d - (qy# Mod 2)/2 ); вычисление номера ячейки по горизонтали
        ; округление результата
        qqx = Int(qx#)
        qqy = Int(qy#)
        ; вычисление координат ячейки по номеру  ячейки
        xx = qqx * d +(qqy Mod 2)*d/2
        yy = qqy* d/4*3       

        ; инфо
        Color 50,50,50
        Rect 5,115,145,65
        Color 255,255,255
       
        Text 10, 120,"Mouse X = "+mx+" : Y = "+my+" ."
        Text 10, 134,"Coordinate X = "+xx+" | Y =  "+yy+" ."
        Text 10, 154,"Position i = "+qqx+" : j = "+qqy+" ."
               
Flip
Wend


FreeImage  imgtile
FreeImage  imgtile0

End

вид
/\/\
| | |
\/\/

polopok 31.03.2013 04:01

Ответ: Гексогонал-сетка.Проверка позиции мыши
 
А теперь другой вид
_
/ \
\_/

Код:


Graphics 800,600,32 ,2

imgtile=CreateImage(41,41)
SetBuffer ImageBuffer(imgtile) ;картинка гекса
        Color 125,22,22
        Line 0,20,10,0
        Line 10,0,30,0
        Line 30,0,40,20
        Line 40,20,30,40
        Line 30,40,10,40
        Line 10,40,0,20
        Color 255,255,255
imgtile0=CreateImage(41,21)
SetBuffer ImageBuffer(imgtile0) ;картинка мыши
        Color 5,0,255
        Rect  10,0,20,20,0
        Color 255,255,255
SetBuffer BackBuffer()




d =40;  :dd =(d/2) : ddd =(dd/2) : mm=d/4*3: rr=d+dd
tilex=16  : tiley=10 ;количество ячеек    i x j
sx=  0

FlushMouse
While Not KeyHit(1)
ClsColor 135,160,160
Cls
mx# = MouseX() : my# = MouseY()

        ; управление отображением сетки
        If KeyDown(205) Then sx=1  ;  <--  -->
        If KeyDown(203) Then sx=0
       
        ; рисуем гексо-сетку
        For j=0 To tilex
        For i=0 To tiley
               
                y = (d*i+(j Mod 2)*d/2)
                x = (j*d/4*3)
                       
                If sx =0 Then       
                DrawImage imgtile,x ,y
                Else If sx=1
                Rect x ,y,d,d,0
                EndIf
                Text x+10 ,y+15,i+" : "+j
        Next
        Next
       
        ;qx# = Floor#()
        qx# = Floor# (( mx# )  / (d+0.15) * 4/3 - 0.15 ); вычисление номера ячейки по вертикали
        qy# = Floor# (( my#  ) / d - (qx# Mod 2)/2 ); вычисление номера ячейки по горизонтали
        ; округление результата
        qqy = Int(qx#)
        qqx = Int(qy#)
        ; вычисление координат ячейки по номеру  ячейки
        xx = qqx * d +(qqy Mod 2)*d/2
        yy = qqy* d/4*3       

        ; инфо
        Color 50,50,50
        Rect 5,115,145,65
        Color 255,255,255
       
        Text 10, 120,"Mouse X = "+mx+" : Y = "+my+" ."
        Text 10, 134,"Coordinate X = "+xx+" | Y =  "+yy+" ."
        Text 10, 154,"Position i = "+qqx+" : j = "+qqy+" ."
               
Flip
Wend


FreeImage  imgtile
FreeImage  imgtile0

End



Часовой пояс GMT +4, время: 22:49.

vBulletin® Version 3.6.5.
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Перевод: zCarot