This repository has been archived by the owner on Mar 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
79b4b90
commit 9cdd7f6
Showing
9 changed files
with
112 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/Avans.FlatGalaxy.Simulation/PathFinding/Data/DijkstraEdge.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace Avans.FlatGalaxy.Simulation.PathFinding.Data | ||
{ | ||
public class DijkstraEdge | ||
{ | ||
public DijkstraNode Node { get; set; } | ||
|
||
public double Weight { get; set; } | ||
|
||
public DijkstraEdge(DijkstraNode node, double weight) | ||
{ | ||
Node = node; | ||
Weight = weight; | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/Avans.FlatGalaxy.Simulation/PathFinding/Data/DijkstraGraph.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Avans.FlatGalaxy.Models.CelestialBodies; | ||
|
||
namespace Avans.FlatGalaxy.Simulation.PathFinding.Data | ||
{ | ||
public class DijkstraGraph | ||
{ | ||
public List<DijkstraNode> Nodes { get; } | ||
|
||
public DijkstraGraph(List<Planet> planets) | ||
{ | ||
Nodes = planets.Select(planet => new DijkstraNode(planet)).ToList(); | ||
|
||
foreach (var node in Nodes) | ||
{ | ||
foreach (var neighbour in node.Planet.Neighbours) | ||
{ | ||
var neighbourNode = Nodes.First(node1 => node1.Planet == neighbour); | ||
|
||
node.Neighbours.Add(new(neighbourNode, node.Planet.DistanceTo(neighbour))); | ||
} | ||
} | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/Avans.FlatGalaxy.Simulation/PathFinding/Data/DijkstraNode.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Avans.FlatGalaxy.Models.CelestialBodies; | ||
|
||
namespace Avans.FlatGalaxy.Simulation.PathFinding.Data | ||
{ | ||
public class DijkstraNode | ||
{ | ||
public Planet Planet { get; } | ||
|
||
public KeyValuePair<Planet, double> Weight { get; set; } | ||
|
||
public List<DijkstraEdge> Neighbours { get; } | ||
|
||
public bool Visited { get; set; } | ||
|
||
public DijkstraNode(Planet planet) | ||
{ | ||
Neighbours = new(); | ||
Weight = new(null, double.PositiveInfinity); | ||
Planet = planet; | ||
} | ||
} | ||
} |
44 changes: 38 additions & 6 deletions
44
src/Avans.FlatGalaxy.Simulation/PathFinding/DijkstraPathFinder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,48 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Avans.FlatGalaxy.Models.CelestialBodies; | ||
using Avans.FlatGalaxy.Simulation.PathFinding.Data; | ||
|
||
namespace Avans.FlatGalaxy.Simulation.PathFinding | ||
{ | ||
public class DijkstraPathFinder : PathFinder | ||
{ | ||
protected override List<Planet> Find(Planet start, Planet end) | ||
protected override List<Planet> Find(Planet start, Planet end, List<Planet> planets) | ||
{ | ||
return null; | ||
} | ||
var graph = new DijkstraGraph(planets); | ||
var startNode = graph.Nodes.First(node => node.Planet == start); | ||
startNode.Weight = new(null, 0); | ||
var unvisited = new List<DijkstraNode> { startNode }; | ||
|
||
while (unvisited.Any()) | ||
{ | ||
var node = unvisited.OrderBy(node1 => node1.Weight.Value).First(); | ||
|
||
foreach (var neighbour in node.Neighbours) | ||
{ | ||
if (neighbour.Weight + node.Weight.Value < neighbour.Node.Weight.Value) | ||
{ | ||
neighbour.Node.Weight = new(node.Planet, neighbour.Weight + node.Weight.Value); | ||
|
||
private static double GetDistance(Planet origin, Planet target) => Math.Pow(origin.CenterX - target.CenterX, 2) + Math.Pow(origin.CenterY - target.CenterY, 2); | ||
if (!unvisited.Contains(neighbour.Node) && !neighbour.Node.Visited) | ||
{ | ||
unvisited.Add(neighbour.Node); | ||
} | ||
} | ||
} | ||
node.Visited = true; | ||
unvisited.Remove(node); | ||
} | ||
|
||
var endNode = graph.Nodes.First(node => node.Planet == end); | ||
var path = new List<DijkstraNode> { endNode }; | ||
while (endNode.Weight.Key != null) | ||
{ | ||
endNode = endNode.Neighbours.OrderBy(edge => edge.Node.Weight.Value).First().Node; | ||
path.Add(endNode); | ||
} | ||
|
||
return path.Select(node => node.Planet).ToList(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters