Draw Grid

Find quick help here to get you started with Hollywood
oceanarts
Posts: 111
Joined: Mon May 27, 2024 10:42 pm

Draw Grid

Post 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
Development System : Imac G5, MorphOs 3.19
plouf
Posts: 666
Joined: Sun Feb 04, 2018 11:51 pm
Location: Athens,Greece

Re: Draw Grid

Post 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
Christos
Flinx
Posts: 342
Joined: Sun Feb 14, 2021 9:54 am
Location: Germany

Re: Draw Grid

Post 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.
oceanarts
Posts: 111
Joined: Mon May 27, 2024 10:42 pm

Re: Draw Grid

Post 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! :D
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.
Development System : Imac G5, MorphOs 3.19
Flinx
Posts: 342
Joined: Sun Feb 14, 2021 9:54 am
Location: Germany

Re: Draw Grid

Post 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?
oceanarts
Posts: 111
Joined: Mon May 27, 2024 10:42 pm

Re: Draw Grid

Post 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. :?
Development System : Imac G5, MorphOs 3.19
Flinx
Posts: 342
Joined: Sun Feb 14, 2021 9:54 am
Location: Germany

Re: Draw Grid

Post 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
Flinx
Posts: 342
Joined: Sun Feb 14, 2021 9:54 am
Location: Germany

Re: Draw Grid

Post 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.
oceanarts
Posts: 111
Joined: Mon May 27, 2024 10:42 pm

Re: Draw Grid

Post 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. :)
Development System : Imac G5, MorphOs 3.19
User avatar
jPV
Posts: 734
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: Draw Grid

Post 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...
Post Reply