Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Threading
- Imports System.ComponentModel
- Imports Pathfinder
- Public Class Form1
- Private Const NODE_SIZE_X As Integer = 25
- Private Const NODE_SIZE_Y As Integer = 25
- Private Const GRID_SIZE_X As Integer = 20
- Private Const GRID_SIZE_Y As Integer = 15
- Private mPathFinder As Pathfinder.Pathfinder
- Dim GridSize As Size
- Dim Nodes As List(Of Node)
- Dim StartNode As Node
- Dim GoalNode As Node
- Dim MPosition As Point
- Dim MousePoint As Point
- Dim WithEvents PathWorker As BackgroundWorker
- ' A global unique random generator'
- Dim rng As Random
- Private Sub Form1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
- DrawPositionedNode(e)
- End Sub
- Private Sub Form1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
- DrawPositionedNode(e)
- End Sub
- Sub DrawPositionedNode(e As System.Windows.Forms.MouseEventArgs)
- MPosition = e.Location
- MousePoint.X = (MPosition.X - NODE_SIZE_X / 2) / NODE_SIZE_X
- MousePoint.Y = (MPosition.Y - NODE_SIZE_Y / 2) / NODE_SIZE_Y
- If e.Button = Windows.Forms.MouseButtons.Left Then
- Dim mAt As Node = mPathFinder.GetNode(MousePoint.X, MousePoint.Y)
- If mAt IsNot Nothing Then
- If rdoStart.Checked Then
- StartNode.Tile = MousePoint
- End If
- If rdoGoal.Checked Then
- GoalNode.Tile = MousePoint
- End If
- If rdoNone.Checked Then
- If mAt.Weight > 0 Then mAt.Weight -= 0.2
- If mAt.Weight < 0 Then mAt.Weight = 0
- End If
- If rdoWall.Checked Then
- If mAt.Weight < 3 Then mAt.Weight += 0.2
- If mAt.Weight > 3 Then mAt.Weight = 3
- End If
- End If
- End If
- Invalidate()
- End Sub
- Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
- GridSize = New Size(NODE_SIZE_X * GRID_SIZE_X, NODE_SIZE_Y * GRID_SIZE_Y)
- Nodes = New List(Of Node)
- mPathFinder = New Pathfinder.Pathfinder(Nodes)
- chkDiagonal.Checked = mPathFinder.UseDiagonal
- chkManhattan.Checked = mPathFinder.UseManhattan
- chkCutCorners.Checked = mPathFinder.UseCutCorners
- MPosition = New Point
- MousePoint = New Point
- rng = New Random
- Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint Or ControlStyles.DoubleBuffer, True)
- Reset()
- End Sub
- Sub Reset()
- StartNode = New Node(New Point(1, 2), Nothing, 0, 0, 0)
- GoalNode = New Node(New Point(rng.Next(0, GRID_SIZE_X - 1), 2), Nothing, 0, 0, 0)
- Nodes.Clear()
- mPathFinder.ClearResults()
- For x As Integer = 0 To GRID_SIZE_X - 1
- For y As Integer = 0 To GRID_SIZE_Y - 1
- Dim Weight As Integer = 0
- If x = 3 And y >= 0 And y < GRID_SIZE_Y Then
- ' Selection of pure numbers sequence or mixed one
- Dim pureNumbers = rng.Next(0, 4)
- Weight = pureNumbers
- End If
- 'Dim pureNumbers = rng.Next(1, 4)
- 'Weight = pureNumbers
- Dim newNode As New Node(New Point(x, y), Nothing, 0, 0, Weight)
- Nodes.Add(newNode)
- Next
- Next
- Invalidate()
- End Sub
- Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
- lblPathLength.Text = String.Format("Path Length: {0}", mPathFinder.PathList.Count)
- lblClosedList.Text = String.Format("Closed List: {0}", mPathFinder.CloseList.Count)
- lblOpenList.Text = String.Format("Open List: {0}", mPathFinder.OpenList.Count)
- Dim pen As New Pen(Color.FromArgb(10, 255, 255, 255))
- Dim fnt As New Font(Me.Font.FontFamily, 8.0)
- e.Graphics.FillRectangle(Brushes.Black, New Rectangle(0, 0, GridSize.Width, GridSize.Height))
- For x As Integer = 0 To GRID_SIZE_X - 1
- Dim xp As Integer = x * NODE_SIZE_X
- e.Graphics.DrawLine(pen, xp, 0, xp, GridSize.Height)
- For y As Integer = 0 To GRID_SIZE_Y - 1
- Dim yp As Integer = y * NODE_SIZE_Y
- e.Graphics.DrawLine(pen, 0, yp, GridSize.Width, yp)
- 'e.Graphics.DrawString(String.Format("{0},{1}", x, y), fnt, Brushes.LightCyan, New PointF(xp, yp))
- Next
- Next
- For Each n As Node In Nodes
- Dim x As Integer = n.Tile.X
- Dim y As Integer = n.Tile.Y
- Dim xp As Integer = x * NODE_SIZE_X
- Dim yp As Integer = y * NODE_SIZE_Y
- Dim nc As SolidBrush = Brushes.White
- Dim sw As Double = NODE_SIZE_X * 0.1
- Dim sh As Double = NODE_SIZE_Y * 0.1
- Select Case n.Weight
- Case Is <= 0
- nc = New SolidBrush(Color.Gray)
- Case Is < 1
- nc = New SolidBrush(Color.Pink)
- Case Is < 2
- nc = New SolidBrush(Color.Orange)
- Case Is < 3
- nc = New SolidBrush(Color.OrangeRed)
- Case Else
- nc = New SolidBrush(Color.Red)
- End Select
- e.Graphics.FillRectangle(nc, New Rectangle(xp + sw / 2, yp + sh / 2, NODE_SIZE_X - sw, NODE_SIZE_Y - sh))
- If n.Tile = StartNode.Tile Then
- sw = NODE_SIZE_X * 0.1
- sh = NODE_SIZE_Y * 0.1
- nc = New SolidBrush(Color.Green)
- e.Graphics.FillRectangle(nc, New Rectangle(xp + sw / 2, yp + sh / 2, NODE_SIZE_X - sw, NODE_SIZE_Y - sh))
- End If
- If n.Tile = GoalNode.Tile Then
- sw = NODE_SIZE_X * 0.1
- sh = NODE_SIZE_Y * 0.1
- nc = New SolidBrush(Color.Yellow)
- e.Graphics.FillRectangle(nc, New Rectangle(xp + sw / 2, yp + sh / 2, NODE_SIZE_X - sw, NODE_SIZE_Y - sh))
- End If
- If mPathFinder.OnCloseList(n) Then
- sw = NODE_SIZE_X * 0.5
- sh = NODE_SIZE_Y * 0.5
- nc = New SolidBrush(Color.Purple)
- e.Graphics.FillRectangle(nc, New Rectangle(xp + sw / 2, yp + sh / 2, NODE_SIZE_X - sw, NODE_SIZE_Y - sh))
- End If
- If mPathFinder.OnOpenList(n) Then
- sw = NODE_SIZE_X * 0.6
- sh = NODE_SIZE_Y * 0.6
- nc = New SolidBrush(Color.DarkSeaGreen)
- e.Graphics.FillRectangle(nc, New Rectangle(xp + sw / 2, yp + sh / 2, NODE_SIZE_X - sw, NODE_SIZE_Y - sh))
- End If
- If mPathFinder.OnPathList(n) Then
- sw = NODE_SIZE_X * 0.7
- sh = NODE_SIZE_Y * 0.7
- nc = New SolidBrush(Color.Blue)
- e.Graphics.FillRectangle(nc, New Rectangle(xp + sw / 2, yp + sh / 2, NODE_SIZE_X - sw, NODE_SIZE_Y - sh))
- End If
- Next
- Dim m_x As Integer = MousePoint.X
- Dim m_y As Integer = MousePoint.Y
- Dim m_xp As Integer = m_x * NODE_SIZE_X
- Dim m_yp As Integer = m_y * NODE_SIZE_Y
- Dim br As Brush = Brushes.Orange
- If rdoWall.Checked Then br = Brushes.Red
- If rdoStart.Checked Then br = Brushes.Green
- If rdoGoal.Checked Then br = Brushes.Yellow
- If rdoNone.Checked Then br = Brushes.Orange
- e.Graphics.FillRectangle(br, New Rectangle(m_xp + 2, m_yp + 2, NODE_SIZE_X - 4, NODE_SIZE_Y - 4))
- Dim mNode As Node = mPathFinder.GetNode(m_x, m_y)
- If mNode IsNot Nothing Then
- e.Graphics.DrawString(String.Format("{0}", mNode.Weight), fnt, Brushes.White, New PointF(m_xp, m_yp))
- End If
- End Sub
- Private Sub Form1_SizeChanged(sender As Object, e As System.EventArgs) Handles Me.SizeChanged
- Invalidate()
- End Sub
- Private Sub btnFindPath_Click(sender As System.Object, e As System.EventArgs) Handles btnFindPath.Click
- PathWorker = New BackgroundWorker
- PathWorker.WorkerReportsProgress = True
- PathWorker.RunWorkerAsync()
- End Sub
- Private Sub btnReset_Click(sender As System.Object, e As System.EventArgs) Handles btnReset.Click
- Reset()
- End Sub
- Private Sub PathWorker_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles PathWorker.DoWork
- mPathFinder.FindPath(StartNode.Tile, GoalNode.Tile)
- End Sub
- Private Sub PathWorker_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles PathWorker.ProgressChanged
- Invalidate()
- End Sub
- Private Sub PathWorker_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles PathWorker.RunWorkerCompleted
- Invalidate()
- End Sub
- Private Sub chkDiagonal_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkDiagonal.CheckedChanged
- If mPathFinder IsNot Nothing Then
- mPathFinder.UseDiagonal = chkDiagonal.Checked
- End If
- End Sub
- Private Sub chkManhattan_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkManhattan.CheckedChanged
- If mPathFinder IsNot Nothing Then
- mPathFinder.UseManhattan = chkManhattan.Checked
- End If
- End Sub
- Private Sub chkCutCorners_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkCutCorners.CheckedChanged
- If mPathFinder IsNot Nothing Then
- mPathFinder.UseCutCorners = chkCutCorners.Checked
- End If
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement