GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Silverback/LevelEditor/Pathfinding/AStar.cs at master · SilverbackSoPra/Silverback

Expand file tree

/

AStar.cs

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
110 lines (83 loc) · 3.06 KB
/

AStar.cs

Copy path

File metadata and controls

110 lines (83 loc) · 3.06 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms.VisualStyles;
using Assimp;
using Microsoft.Xna.Framework;
namespace LevelEditor.Pathfinding
{
public static class AStar
{
private static CompareVertexCost mCompareVertexCost = new CompareVertexCost();
public static Vertex Search(Vertex start, Vertex end, IEnumerable<Vertex> vertices)
{
var open = new SortedSet<Vertex>(new CompareVertexCost());
var closed = new HashSet<Vertex>();
foreach (var vertex in vertices)
{
vertex.mHeuristicCost = float.MaxValue;
vertex.mCost = float.MaxValue;
}
var currentNode = start;
currentNode.mCost = 0.0f;
currentNode.mHeuristicCost = Vector2.DistanceSquared(start.Position, end.Position);
var firstNode = currentNode;
open.Add(firstNode);
while (open.Count > 0)
{
currentNode = open.Min;
if (currentNode.Equals(end))
{
break;
}
open.Remove(open.Min);
closed.Add(currentNode);
// Get all Nodes containing currentNode.mVertex
var neighbours = currentNode.mAdjacentVertices;
// Iterate over all successors of currentNode.mVertex
foreach (var neighbour in neighbours)
{
if (closed.Contains(neighbour))
{
continue;
}
var cost = currentNode.mCost + Vector2.DistanceSquared(currentNode.Position, neighbour.Position);
if (!open.Contains(neighbour))
{
open.Add(neighbour);
}
else if (cost >= neighbour.mCost)
{
continue;
}
open.Remove(neighbour);
neighbour.mPreviousVertex = currentNode;
neighbour.mCost = cost;
neighbour.mHeuristicCost = cost + Vector2.DistanceSquared(neighbour.Position, end.Position);
open.Add(neighbour);
}
}
while (currentNode.mPreviousVertex != null)
{
currentNode.mPreviousVertex.mNextVertex = currentNode;
currentNode = currentNode.mPreviousVertex;
}
return firstNode;
}
private class CompareVertexCost : IComparer<Vertex>
{
public int Compare(Vertex x, Vertex y)
{
if (x.mHeuristicCost < y.mHeuristicCost)
{
return -1;
}
if (x.mHeuristicCost > y.mHeuristicCost)
{
return 1;
}
return 0;
}
}
}
}
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.