Advertisement
caLLowCreation

Pathfinder 2D in VB - Usage

Oct 20th, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 9.80 KB | None | 0 0
  1. Imports System.Threading
  2. Imports System.ComponentModel
  3. Imports Pathfinder
  4.  
  5. Public Class Form1
  6.     Private Const NODE_SIZE_X As Integer = 25
  7.     Private Const NODE_SIZE_Y As Integer = 25
  8.  
  9.     Private Const GRID_SIZE_X As Integer = 20
  10.     Private Const GRID_SIZE_Y As Integer = 15
  11.  
  12.     Private mPathFinder As Pathfinder.Pathfinder
  13.  
  14.     Dim GridSize As Size
  15.     Dim Nodes As List(Of Node)
  16.  
  17.     Dim StartNode As Node
  18.     Dim GoalNode As Node
  19.  
  20.     Dim MPosition As Point
  21.  
  22.     Dim MousePoint As Point
  23.  
  24.     Dim WithEvents PathWorker As BackgroundWorker
  25.     ' A global unique random generator'
  26.     Dim rng As Random
  27.  
  28.     Private Sub Form1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
  29.         DrawPositionedNode(e)
  30.     End Sub
  31.  
  32.     Private Sub Form1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
  33.         DrawPositionedNode(e)
  34.     End Sub
  35.  
  36.     Sub DrawPositionedNode(e As System.Windows.Forms.MouseEventArgs)
  37.         MPosition = e.Location
  38.         MousePoint.X = (MPosition.X - NODE_SIZE_X / 2) / NODE_SIZE_X
  39.         MousePoint.Y = (MPosition.Y - NODE_SIZE_Y / 2) / NODE_SIZE_Y
  40.         If e.Button = Windows.Forms.MouseButtons.Left Then
  41.  
  42.             Dim mAt As Node = mPathFinder.GetNode(MousePoint.X, MousePoint.Y)
  43.  
  44.             If mAt IsNot Nothing Then
  45.                 If rdoStart.Checked Then
  46.                     StartNode.Tile = MousePoint
  47.                 End If
  48.                 If rdoGoal.Checked Then
  49.                     GoalNode.Tile = MousePoint
  50.                 End If
  51.  
  52.                 If rdoNone.Checked Then
  53.                     If mAt.Weight > 0 Then mAt.Weight -= 0.2
  54.                     If mAt.Weight < 0 Then mAt.Weight = 0
  55.                 End If
  56.                 If rdoWall.Checked Then
  57.                     If mAt.Weight < 3 Then mAt.Weight += 0.2
  58.                     If mAt.Weight > 3 Then mAt.Weight = 3
  59.                 End If
  60.             End If
  61.  
  62.         End If
  63.  
  64.         Invalidate()
  65.     End Sub
  66.  
  67.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  68.         GridSize = New Size(NODE_SIZE_X * GRID_SIZE_X, NODE_SIZE_Y * GRID_SIZE_Y)
  69.  
  70.         Nodes = New List(Of Node)
  71.         mPathFinder = New Pathfinder.Pathfinder(Nodes)
  72.  
  73.         chkDiagonal.Checked = mPathFinder.UseDiagonal
  74.         chkManhattan.Checked = mPathFinder.UseManhattan
  75.         chkCutCorners.Checked = mPathFinder.UseCutCorners
  76.  
  77.         MPosition = New Point
  78.         MousePoint = New Point
  79.         rng = New Random
  80.  
  81.         Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint Or ControlStyles.DoubleBuffer, True)
  82.         Reset()
  83.     End Sub
  84.     Sub Reset()
  85.         StartNode = New Node(New Point(1, 2), Nothing, 0, 0, 0)
  86.         GoalNode = New Node(New Point(rng.Next(0, GRID_SIZE_X - 1), 2), Nothing, 0, 0, 0)
  87.  
  88.         Nodes.Clear()
  89.         mPathFinder.ClearResults()
  90.  
  91.         For x As Integer = 0 To GRID_SIZE_X - 1
  92.             For y As Integer = 0 To GRID_SIZE_Y - 1
  93.                 Dim Weight As Integer = 0
  94.                 If x = 3 And y >= 0 And y < GRID_SIZE_Y Then
  95.                     ' Selection of pure numbers sequence or mixed one
  96.                     Dim pureNumbers = rng.Next(0, 4)
  97.                     Weight = pureNumbers
  98.                 End If
  99.                 'Dim pureNumbers = rng.Next(1, 4)
  100.                 'Weight = pureNumbers
  101.                 Dim newNode As New Node(New Point(x, y), Nothing, 0, 0, Weight)
  102.                 Nodes.Add(newNode)
  103.             Next
  104.         Next
  105.         Invalidate()
  106.  
  107.     End Sub
  108.  
  109.     Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
  110.         lblPathLength.Text = String.Format("Path Length: {0}", mPathFinder.PathList.Count)
  111.         lblClosedList.Text = String.Format("Closed List: {0}", mPathFinder.CloseList.Count)
  112.         lblOpenList.Text = String.Format("Open List: {0}", mPathFinder.OpenList.Count)
  113.  
  114.         Dim pen As New Pen(Color.FromArgb(10, 255, 255, 255))
  115.         Dim fnt As New Font(Me.Font.FontFamily, 8.0)
  116.         e.Graphics.FillRectangle(Brushes.Black, New Rectangle(0, 0, GridSize.Width, GridSize.Height))
  117.         For x As Integer = 0 To GRID_SIZE_X - 1
  118.             Dim xp As Integer = x * NODE_SIZE_X
  119.             e.Graphics.DrawLine(pen, xp, 0, xp, GridSize.Height)
  120.             For y As Integer = 0 To GRID_SIZE_Y - 1
  121.                 Dim yp As Integer = y * NODE_SIZE_Y
  122.                 e.Graphics.DrawLine(pen, 0, yp, GridSize.Width, yp)
  123.  
  124.                 'e.Graphics.DrawString(String.Format("{0},{1}", x, y), fnt, Brushes.LightCyan, New PointF(xp, yp))
  125.             Next
  126.         Next
  127.  
  128.         For Each n As Node In Nodes
  129.             Dim x As Integer = n.Tile.X
  130.             Dim y As Integer = n.Tile.Y
  131.             Dim xp As Integer = x * NODE_SIZE_X
  132.             Dim yp As Integer = y * NODE_SIZE_Y
  133.  
  134.             Dim nc As SolidBrush = Brushes.White
  135.  
  136.             Dim sw As Double = NODE_SIZE_X * 0.1
  137.             Dim sh As Double = NODE_SIZE_Y * 0.1
  138.  
  139.  
  140.             Select Case n.Weight
  141.                 Case Is <= 0
  142.                     nc = New SolidBrush(Color.Gray)
  143.                 Case Is < 1
  144.                     nc = New SolidBrush(Color.Pink)
  145.                 Case Is < 2
  146.                     nc = New SolidBrush(Color.Orange)
  147.                 Case Is < 3
  148.                     nc = New SolidBrush(Color.OrangeRed)
  149.                 Case Else
  150.                     nc = New SolidBrush(Color.Red)
  151.             End Select
  152.             e.Graphics.FillRectangle(nc, New Rectangle(xp + sw / 2, yp + sh / 2, NODE_SIZE_X - sw, NODE_SIZE_Y - sh))
  153.  
  154.             If n.Tile = StartNode.Tile Then
  155.                 sw = NODE_SIZE_X * 0.1
  156.                 sh = NODE_SIZE_Y * 0.1
  157.                 nc = New SolidBrush(Color.Green)
  158.                 e.Graphics.FillRectangle(nc, New Rectangle(xp + sw / 2, yp + sh / 2, NODE_SIZE_X - sw, NODE_SIZE_Y - sh))
  159.             End If
  160.  
  161.             If n.Tile = GoalNode.Tile Then
  162.                 sw = NODE_SIZE_X * 0.1
  163.                 sh = NODE_SIZE_Y * 0.1
  164.                 nc = New SolidBrush(Color.Yellow)
  165.                 e.Graphics.FillRectangle(nc, New Rectangle(xp + sw / 2, yp + sh / 2, NODE_SIZE_X - sw, NODE_SIZE_Y - sh))
  166.             End If
  167.  
  168.             If mPathFinder.OnCloseList(n) Then
  169.                 sw = NODE_SIZE_X * 0.5
  170.                 sh = NODE_SIZE_Y * 0.5
  171.                 nc = New SolidBrush(Color.Purple)
  172.                 e.Graphics.FillRectangle(nc, New Rectangle(xp + sw / 2, yp + sh / 2, NODE_SIZE_X - sw, NODE_SIZE_Y - sh))
  173.             End If
  174.  
  175.             If mPathFinder.OnOpenList(n) Then
  176.                 sw = NODE_SIZE_X * 0.6
  177.                 sh = NODE_SIZE_Y * 0.6
  178.                 nc = New SolidBrush(Color.DarkSeaGreen)
  179.                 e.Graphics.FillRectangle(nc, New Rectangle(xp + sw / 2, yp + sh / 2, NODE_SIZE_X - sw, NODE_SIZE_Y - sh))
  180.             End If
  181.  
  182.             If mPathFinder.OnPathList(n) Then
  183.                 sw = NODE_SIZE_X * 0.7
  184.                 sh = NODE_SIZE_Y * 0.7
  185.                 nc = New SolidBrush(Color.Blue)
  186.                 e.Graphics.FillRectangle(nc, New Rectangle(xp + sw / 2, yp + sh / 2, NODE_SIZE_X - sw, NODE_SIZE_Y - sh))
  187.             End If
  188.         Next
  189.  
  190.         Dim m_x As Integer = MousePoint.X
  191.         Dim m_y As Integer = MousePoint.Y
  192.         Dim m_xp As Integer = m_x * NODE_SIZE_X
  193.         Dim m_yp As Integer = m_y * NODE_SIZE_Y
  194.         Dim br As Brush = Brushes.Orange
  195.         If rdoWall.Checked Then br = Brushes.Red
  196.         If rdoStart.Checked Then br = Brushes.Green
  197.         If rdoGoal.Checked Then br = Brushes.Yellow
  198.         If rdoNone.Checked Then br = Brushes.Orange
  199.         e.Graphics.FillRectangle(br, New Rectangle(m_xp + 2, m_yp + 2, NODE_SIZE_X - 4, NODE_SIZE_Y - 4))
  200.         Dim mNode As Node = mPathFinder.GetNode(m_x, m_y)
  201.         If mNode IsNot Nothing Then
  202.             e.Graphics.DrawString(String.Format("{0}", mNode.Weight), fnt, Brushes.White, New PointF(m_xp, m_yp))
  203.         End If
  204.     End Sub
  205.  
  206.     Private Sub Form1_SizeChanged(sender As Object, e As System.EventArgs) Handles Me.SizeChanged
  207.         Invalidate()
  208.     End Sub
  209.  
  210.     Private Sub btnFindPath_Click(sender As System.Object, e As System.EventArgs) Handles btnFindPath.Click
  211.         PathWorker = New BackgroundWorker
  212.         PathWorker.WorkerReportsProgress = True
  213.         PathWorker.RunWorkerAsync()
  214.     End Sub
  215.     Private Sub btnReset_Click(sender As System.Object, e As System.EventArgs) Handles btnReset.Click
  216.         Reset()
  217.     End Sub
  218.  
  219.     Private Sub PathWorker_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles PathWorker.DoWork
  220.         mPathFinder.FindPath(StartNode.Tile, GoalNode.Tile)
  221.     End Sub
  222.  
  223.     Private Sub PathWorker_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles PathWorker.ProgressChanged
  224.         Invalidate()
  225.     End Sub
  226.  
  227.     Private Sub PathWorker_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles PathWorker.RunWorkerCompleted
  228.         Invalidate()
  229.     End Sub
  230.  
  231.     Private Sub chkDiagonal_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkDiagonal.CheckedChanged
  232.         If mPathFinder IsNot Nothing Then
  233.             mPathFinder.UseDiagonal = chkDiagonal.Checked
  234.         End If
  235.     End Sub
  236.  
  237.     Private Sub chkManhattan_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkManhattan.CheckedChanged
  238.         If mPathFinder IsNot Nothing Then
  239.             mPathFinder.UseManhattan = chkManhattan.Checked
  240.         End If
  241.     End Sub
  242.  
  243.     Private Sub chkCutCorners_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkCutCorners.CheckedChanged
  244.         If mPathFinder IsNot Nothing Then
  245.             mPathFinder.UseCutCorners = chkCutCorners.Checked
  246.         End If
  247.     End Sub
  248. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement