forked from TheAlgorithms/C-Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.cs
More file actions
164 lines (141 loc) · 5.77 KB
/
Copy pathNode.cs
File metadata and controls
164 lines (141 loc) · 5.77 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
namespace AStar
{
/// <summary>
/// A Node(former Location)
/// Contains Positional and other information about a single node.
/// </summary>
public class Node : IComparable<Node>, IEquatable<Node>
{
// Constructors
/// <summary>
/// Initializes a new instance of the <see cref="Node"/> class.
/// </summary>
/// <param name="position">Position of the node.</param>
/// <param name="traversable">Flag if the node is traversable.</param>
/// <param name="traverseMultiplier">Multiplier for traversal costs.</param>
public Node(VecN position, bool traversable, double traverseMultiplier)
{
Traversable = traversable;
Position = position;
TraversalCostMultiplier = traverseMultiplier;
}
// Properties
/// <summary>
/// Gets the Total cost of the Node.
/// The Current Costs + the estimated costs.
/// </summary>
public double TotalCost => EstimatedCost + CurrentCost;
/// <summary>
/// Gets or sets the Distance between this node and the target node.
/// </summary>
public double EstimatedCost { get; set; }
/// <summary>
/// Gets a value indicating whether how costly it is to traverse over this node.
/// </summary>
public double TraversalCostMultiplier { get; }
/// <summary>
/// Gets or sets a value indicating whether to go from the start node to this node.
/// </summary>
public double CurrentCost { get; set; }
/// <summary>
/// Gets or sets the state of the Node
/// Can be Unconsidered(Default), Open and Closed.
/// </summary>
public NodeState State { get; set; }
/// <summary>
/// Gets a value indicating whether the node is traversable.
/// </summary>
public bool Traversable { get; }
/// <summary>
/// Gets or sets a list of all connected nodes.
/// </summary>
public Node[] ConnectedNodes { get; set; } = new Node[0];
/// <summary>
/// Gets or sets he "previous" node that was processed before this node.
/// </summary>
public Node? Parent { get; set; }
/// <summary>
/// Gets the positional information of the node.
/// </summary>
public VecN Position { get; }
/// <summary>
/// TODO.
/// </summary>
/// <param name="left">TODO 2.</param>
/// <param name="right">TODO 3.</param>
/// <returns>TODO 4.</returns>
public static bool operator ==(Node left, Node right) => left?.Equals(right) != false;
/// <summary>
/// TODO.
/// </summary>
/// <param name="left">TODO 2.</param>
/// <param name="right">TODO 3.</param>
/// <returns>TODO 4.</returns>
public static bool operator >(Node left, Node right) => left.CompareTo(right) > 0;
/// <summary>
/// TODO.
/// </summary>
/// <param name="left">TODO 2.</param>
/// <param name="right">TODO 3.</param>
/// <returns>TODO 4.</returns>
public static bool operator <(Node left, Node right) => left.CompareTo(right) < 0;
/// <summary>
/// TODO.
/// </summary>
/// <param name="left">TODO 2.</param>
/// <param name="right">TODO 3.</param>
/// <returns>TODO 4.</returns>
public static bool operator !=(Node left, Node right) => !(left == right);
/// <summary>
/// TODO.
/// </summary>
/// <param name="left">TODO 2.</param>
/// <param name="right">TODO 3.</param>
/// <returns>TODO 4.</returns>
public static bool operator <=(Node left, Node right) => left.CompareTo(right) <= 0;
/// <summary>
/// TODO.
/// </summary>
/// <param name="left">TODO 2.</param>
/// <param name="right">TODO 3.</param>
/// <returns>TODO 4.</returns>
public static bool operator >=(Node left, Node right) => left.CompareTo(right) >= 0;
/// <summary>
/// Compares the Nodes based on their total costs.
/// Total Costs: A* Pathfinding.
/// Current: Djikstra Pathfinding.
/// Estimated: Greedy Pathfinding.
/// </summary>
/// <param name="other">The other node.</param>
/// <returns>A comparison between the costs.</returns>
public int CompareTo(Node other) => TotalCost.CompareTo(other.TotalCost);
/// <summary>
/// Equals Override.
/// </summary>
/// <param name="obj">The object to be checked against.</param>
/// <returns>True if Equal, False if Not Equal.</returns>
public override bool Equals(object obj) => (obj is Node other) && Equals(other);
/// <summary>
/// Useless override to shut up the automated testing.
/// </summary>
/// <returns>the default hash value.</returns>
public override int GetHashCode() => base.GetHashCode();
/// <summary>
/// Override for IEquatable.
/// </summary>
/// <param name="other">The object to be checked against.</param>
/// <returns>True if Equal, False if not Equal.</returns>
public bool Equals(Node other) => CompareTo(other) == 0;
/// <summary>
/// Returns the distance to the other node.
/// </summary>
/// <param name="other">The other node.</param>
/// <returns>Distance between this and other.</returns>
public double DistanceTo(Node other)
{
// Since we are only using the distance in comparison with other distances, we can skip using Math.Sqrt
return Position.SqrDistance(other.Position);
}
}
}