#Region Project Attributes
#MainFormWidth: 600
#MainFormHeight: 400
#End Region
Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Dim can As Canvas
Dim clear_btn As Button
Type vertices(x As Float, y As Float)
Dim All_verts As List
Dim line_length As Double = 0.0
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.SetFormStyle("UNIFIED")
MainForm.Show
clear_btn.Initialize("clear")
can.Initialize("can")
All_verts.Initialize
MainForm.RootPane.AddNode(can,0,0,MainForm.Width,MainForm.Height)
MainForm.RootPane.AddNode(clear_btn,0,0,100,30)
clear_btn.Text = "clear"
clear_btn.TextSize = 14
drawtext(line_length)
End Sub
Sub drawtext(length As Double)
can.DrawText("Line length: " & NumberFormat2(length,1,1,1,False),120,20,fx.CreateFont("Arial",14,False, False),fx.Colors.Black,"LEFT")
End Sub
Sub clear_MouseClicked (EventData As MouseEvent)
All_verts.Clear
can.ClearRect(0,0,MainForm.Width,MainForm.Height)
line_length = 0.0
drawtext(line_length)
End Sub
Sub MainForm_MouseClicked (EventData As MouseEvent)
Dim v1 As vertices
v1.x = EventData.X
v1.y = EventData.Y
All_verts.Add(v1) 'on every mouse click we add a new vertices to the main vertices list array
can.ClearRect(0,0,MainForm.Width,MainForm.Height) 'we clear on every mouse click the canvas
line_length = 0.0
Prims_Algorithm 'start algorithm
End Sub
Sub Prims_Algorithm
Dim unreached_list, reached_list As List 'we create 2 list (reached and unreached)
unreached_list.Initialize
reached_list.Initialize
For i = 0 To All_verts.Size - 1 'first we move all dots that are stored in the main list array to "unreached" dots list array!
Dim v1 As vertices = All_verts.Get(i)
If i = 0 Then can.DrawCircle(v1.x,v1.y,6,fx.Colors.Red,True,0) Else can.DrawCircle(v1.x,v1.y,6,fx.Colors.Black,True,0) 'first dot will be our starting point so we draw it red
unreached_list.Add(v1) 'add all dots to unreached list!
Next
reached_list.Add(unreached_list.Get(0)) 'first dot is our start point so we copy first dot and move it to reached list!
unreached_list.RemoveAt(0) 'and here we delete it from the unreached list!
Do While unreached_list.Size > 0
Dim shortest_dist As Double = 100000.0 'we set here the shortest_distance to a very high number to avoid it to be shorter then the longest real distance between 2 dots
Dim rindex, uindex As Int 'backup our shortest line between reached dot and unreached dot
For i = 0 To reached_list.Size - 1
For j = 0 To unreached_list.Size - 1
Dim v2 As vertices = reached_list.Get(i)
Dim v3 As vertices = unreached_list.Get(j)
Dim d As Double = distance(v2.x,v3.x,v2.y,v3.y)
If d < shortest_dist Then 'here we are looking for the shortest "reached dot" and "unreached dot" if it is found it will store the index of both and draw a line after we go through all reached dots
shortest_dist = d
rindex = i 'backup index of reached dot
uindex = j 'backup index of unreached dot
End If
Next
Next
Dim v4, v5 As vertices
v4 = reached_list.Get(rindex)
v5 = unreached_list.Get(uindex)
can.DrawLine(v4.x,v4.y,v5.x,v5.y,fx.Colors.Black,2)
reached_list.Add(v5) 'here we add the unreached dot to the reached dot list
unreached_list.RemoveAt(uindex) 'now that dot is reached so we need to delete it from the unreached list array
line_length = line_length + shortest_dist 'we calculate the sum of all lines
Loop
drawtext(line_length)
End Sub
Sub distance(x1 As Float, x2 As Float, y1 As Float, y2 As Float) As Double
Return Sqrt(Power(x2-x1,2)+Power(y2-y1,2)) 'simple distance calculation
End Sub