Page 1 of 2
Draw Grid
Posted: Fri Nov 15, 2024 10:07 am
by oceanarts
I fancied myself very smart, when I managed to reduce a whole lot of Line instructions down to only one using Block... EndBlock within a For... Next loop.
As I wanted it to be as flexible as possible I used a lot of variables, and now I've confused myself. Can someone review this code and see if it's doing what supposed to do. It seems to work as it supposed to; draw a grid filling the display no matter its size. But you never know, and surely there's a better more efficient way. Also, is there a way to retrieve the display width/heigh from the the
@DISPLAY pre-processor set at the start?
Code: Select all
@VERSION 10,0
@DISPLAY{WIDTH = 640, HEIGHT = 480, MODE = "WINDOWED", TITLE = "GRID", COLOR = #BLACK} ;Create the window
SetFont(#MONOSPACE, 20) ; Inbuilt fonts are: SANS, SERIF, MONOSPACE
SetFontStyle(#BOLD|#ANTIALIAS) ; Styles: BOLD, ITALIC, NORMAL, UNDERLINED
SetFontColor(#WHITE)
/* === SET VARIABLES === */
GS = 32 ; GRID-SIZE
GC = $5f8b56 ; GRID-COLOR
LT = 1 ; LINE-THICKNESS
GW = 640 / GS ; GRID-WIDTH
GH = 480 / GS ; GRID-HEIGHT
Function p_DrawGrid_H()
for x = 1 to GH
block
for l = 1 to x
Line (0, GS*l, GW*GS, GS*l, GC, {Thickness = LT})
next
endblock
Next
EndFunction
Function p_DrawGrid_V()
for x = 1 to GW
block
for l = 1 to x
Line (GS*l, 0, GS*l, GW*GS, GC, {Thickness = LT})
next
endblock
Next
EndFunction
BeginDoubleBuffer()
EscapeQuit(True)
/* MAIN LOOP */
Repeat
Cls(#BLACK)
p_DrawGrid_H()
p_DrawGrid_V()
Flip()
Forever
EndDoubleBuffer()
End
Re: Draw Grid
Posted: Fri Nov 15, 2024 4:30 pm
by plouf
nothing to review it works so its good
for second question
Code: Select all
GW = GetAttribute(#DISPLAY, 1, #ATTRRAWWIDTH ) / GS ; GRID-WIDTH
GH = GetAttribute(#DISPLAY, 1, #ATTRRAWHEIGHT) / GS ; GRID-HEIGHT
Re: Draw Grid
Posted: Fri Nov 15, 2024 6:31 pm
by Flinx
But for what should the Block/Endblock be good? Your code works without them too, because there is nothing you have to encapsulate.
Re: Draw Grid
Posted: Fri Nov 15, 2024 6:59 pm
by oceanarts
plouf wrote: ↑Fri Nov 15, 2024 4:30 pm
nothing to review it works so its good
for second question
Code: Select all
GW = GetAttribute(#DISPLAY, 1, #ATTRRAWWIDTH ) / GS ; GRID-WIDTH
GH = GetAttribute(#DISPLAY, 1, #ATTRRAWHEIGHT) / GS ; GRID-HEIGHT
Thanks!
Flinx wrote: ↑Fri Nov 15, 2024 6:31 pm
But for what should the Block/Endblock be good? Your code works without them too, because there is nothing you have to encapsulate.
That's what I thought... But a single For... Next doesn't seem to draw anything (unless I'm using it wrong) , that's why I put the Block there.
Re: Draw Grid
Posted: Fri Nov 15, 2024 7:45 pm
by Flinx
oceanarts wrote: ↑Fri Nov 15, 2024 6:59 pm
But a single For... Next doesn't seem to draw anything (unless I'm using it wrong) , that's why I put the Block there.
I have tried your code and commented out the Block commands, and this did work (on Windows). Maybe you had another mistake in it?
Re: Draw Grid
Posted: Fri Nov 15, 2024 8:43 pm
by oceanarts
Flinx wrote: ↑Fri Nov 15, 2024 7:45 pm
oceanarts wrote: ↑Fri Nov 15, 2024 6:59 pm
But a single For... Next doesn't seem to draw anything (unless I'm using it wrong) , that's why I put the Block there.
I have tried your code and commented out the Block commands, and this did work (on Windows). Maybe you had another mistake in it?
If this is what you mean:
Code: Select all
Function p_DrawGrid_H()
for x = 1 to GH
;block
;for l = 1 to x
Line (0, GS*l, GW*GS, GS*l, GC, {Thickness = LT})
;next
;endblock
Next
EndFunction
Function p_DrawGrid_V()
for x = 1 to GW
;block
;for l = 1 to x
Line (GS*l, 0, GS*l, GW*GS, GC, {Thickness = LT})
;next
;endblock
Next
EndFunction
Then it doesn't work on my end (Linux). Weird.

Re: Draw Grid
Posted: Sat Nov 16, 2024 12:13 am
by Flinx
No, I was only talking about the block command lines.
But now I've had a closer look at the code. Of course you need only one loop. The confusion probably comes from the misleading comments on the variables.
Code: Select all
@VERSION 10,0
@DISPLAY{WIDTH = 640, HEIGHT = 480, MODE = "WINDOWED", TITLE = "GRID", COLOR = #BLACK} ;Create the window
SetFont(#MONOSPACE, 20) ; Inbuilt fonts are: SANS, SERIF, MONOSPACE
SetFontStyle(#BOLD|#ANTIALIAS) ; Styles: BOLD, ITALIC, NORMAL, UNDERLINED
SetFontColor(#WHITE)
/* === SET VARIABLES === */
GS = 32 ; GRID-SIZE
GC = $5f8b56 ; GRID-COLOR
LT = 1 ; LINE-THICKNESS
GW = 640 / GS ; Number of vertical lines
GH = 480 / GS ; Number of horizontal lines
Function p_DrawGrid_H()
For Local x = 1 To GH
Line (0, GS*x, GW*GS, GS*x, GC, {Thickness = LT})
Next
EndFunction
Function p_DrawGrid_V()
For Local x = 1 To GW
Line (GS*x, 0, GS*x, GH*GS, GC, {Thickness = LT})
Next
EndFunction
BeginDoubleBuffer()
EscapeQuit(True)
/* MAIN LOOP */
Repeat
Cls(#BLACK)
p_DrawGrid_H()
p_DrawGrid_V()
Flip()
Forever
EndDoubleBuffer()
End
Re: Draw Grid
Posted: Sat Nov 16, 2024 12:21 am
by Flinx
But that's not perfect yet because there are unnecessary multiplications in the loop. You can replace the GW*GS and GH*GS by the size values.
Re: Draw Grid
Posted: Sat Nov 16, 2024 6:20 am
by oceanarts
Flinx wrote: ↑Sat Nov 16, 2024 12:13 am
No, I was only talking about the block command lines.
But now I've had a closer look at the code. Of course you need only one loop. The confusion probably comes from the misleading comments on the variables.
Code: Select all
@VERSION 10,0
@DISPLAY{WIDTH = 640, HEIGHT = 480, MODE = "WINDOWED", TITLE = "GRID", COLOR = #BLACK} ;Create the window
SetFont(#MONOSPACE, 20) ; Inbuilt fonts are: SANS, SERIF, MONOSPACE
SetFontStyle(#BOLD|#ANTIALIAS) ; Styles: BOLD, ITALIC, NORMAL, UNDERLINED
SetFontColor(#WHITE)
/* === SET VARIABLES === */
GS = 32 ; GRID-SIZE
GC = $5f8b56 ; GRID-COLOR
LT = 1 ; LINE-THICKNESS
GW = 640 / GS ; Number of vertical lines
GH = 480 / GS ; Number of horizontal lines
Function p_DrawGrid_H()
For Local x = 1 To GH
Line (0, GS*x, GW*GS, GS*x, GC, {Thickness = LT})
Next
EndFunction
Function p_DrawGrid_V()
For Local x = 1 To GW
Line (GS*x, 0, GS*x, GH*GS, GC, {Thickness = LT})
Next
EndFunction
BeginDoubleBuffer()
EscapeQuit(True)
/* MAIN LOOP */
Repeat
Cls(#BLACK)
p_DrawGrid_H()
p_DrawGrid_V()
Flip()
Forever
EndDoubleBuffer()
End
Fantastic! Thanks a lot for pointing it out. Feels a lot cleaner and more proper now.

Re: Draw Grid
Posted: Mon Nov 18, 2024 9:42 am
by jPV
Flinx wrote: ↑Sat Nov 16, 2024 12:21 am
But that's not perfect yet because there are unnecessary multiplications in the loop. You can replace the GW*GS and GH*GS by the size values.
+1
It would be a good practise to do as little calculations as possible inside loops always, especially avoiding multiplications and divisions. Even if it wouldn't be looped that many times like here, but when you do it always, you do it automatically for bigger loops too then
It would probably be more optimal by removing the duplicate calcultaion of GS*x from the loop too
In this case:
Code: Select all
Function p_DrawGrid_H()
Local dw = GW*GS
For Local x = 1 To GH
Local lx = GS*x
Line (0, lx, dw, lx, GC, {Thickness = LT})
Next
EndFunction
Or:
Code: Select all
DW = GetAttribute(#DISPLAY, 1, #ATTRWIDTH)
GW = DW / GS ; Number of vertical lines
Function p_DrawGrid_H()
For Local x = 1 To GH
Local lx = GS*x
Line (0, lx, DW, lx, GC, {Thickness = LT})
Next
EndFunction
etc...