Line Drawing (OOP Though!)

BlitzMax Forums/BlitzMax Beginners Area/Line Drawing (OOP Though!)

Matt Vinyl(Posted 2011) [#1]
Hi all, I thought this would be quite straight-forward at first, but again, I'm struggling.

The user can click anywhere on the screen placing a 'node', these are stored in a list. When the user has placed at least two nodes, I'd like a line created between the points. I'd also like this line stored in a list (the type has the fields x1,y1,x2,y2).

SuperStrict

SetGraphicsDriver GLMax2DDriver() 'set graphics driver

Global graphicsx:Int=800 'screen width pixels
Global graphicsy:Int=600 'screen height pixels
Global screenmode:Byte=0 'screen mode 0=windowed, >0=fullscreen

Graphics graphicsx,graphicsy,screenmode 'graphics setup

'********************
'*** Nodes & Rays ***
'********************
Global nodelist:TList=CreateList() 'list of all nodes

Type node
	Field x:Int 'physical x location
	Field y:Int 'physical y location
End Type

Global raylist:TList=CreateList() 'list of all rays

Type ray
	Field x1:Int 'physical x start location
	Field y1:Int 'physical y start location
	Field x2:Int 'physical x end location
	Field y2:Int 'physical y end location
End Type

Function placenode() 'create a new node
	If MouseHit(1) Then
		Local newnode:node=New node
		newnode.x=MouseX()
		newnode.y=MouseY()
		ListAddLast(nodelist,newnode)
		If CountList(nodelist)>0 Then
			If CountList(nodelist) Mod 2=0 Then
		
			Else
				Local newray:ray=New ray
				newray.x1=newnode.x
				newray.y1=newnode.y
				ListAddLast(raylist,newray)
			End If
		End If
	End If
End Function

Function removenode() 'remove previously placed node
	If MouseHit(2) Then 
		If ListIsEmpty(nodelist)=False Then
			nodelist.removelast
		End If
	End If
End Function

Function drawnode() 'draw nodes
	SetColor 255,255,255
	For Local a:node=EachIn nodelist
		Plot a.x,a.y
	Next
	If CountList(nodelist)>0 Then
		For Local b:ray=EachIn raylist
			DrawLine b.x1,b.y1,b.x2,b.y2
		Next
	End If
End Function

'**********************
'*** MAIN GAME LOOP ***
'**********************
While Not KeyHit(KEY_ESCAPE)
	Cls
	placenode()
	removenode()
	drawnode()
	debug()
	Flip
Wend

'*************
'*** DEBUG ***
'*************

Function debug()
	Local a:Int=CountList(nodelist)
	DrawText a,0,0
	Local b:Int=CountList(raylist)
	DrawText b,0,16
End Function


Am I approaching this the right way? It obviously doesn't work right yet - struggling with creating new objects whereby I need to populate them with information from the previously created one (start and end positions of the line).

Sorry if that doesn't make too much sense, or my code looks horrendous! :p


GfK(Posted 2011) [#2]
You're storing the coordinates of each nodes, then each line which connects the nodes together... so you're storing the same data twice, in two different places. At some point that's going to become unmanageable.

I would structure it something like this:
Type node
  Field x:Int
  Field y:Int
End Type

Type ray
  Field startNode:node
  Field endNode:node
end Type

That way you've got the node data which is instantiated in the ray object.


Matt Vinyl(Posted 2011) [#3]
Cheers GFK - you've come to my rescue many-a-time over the years! ;) I think I've already reached the unmanageable state, hence my posting. That's good advice there, will tweak appropriately.