Click here to Skip to main content
15,886,689 members
Articles / Programming Languages / C# 4.0

A Complex - Yet Quite Intelligible - Pathfinding in C#

Rate me:
Please Sign up or sign in to vote.
4.96/5 (41 votes)
30 Aug 2013CPOL21 min read 59.5K   2.5K   88  
A semi-analytic 2D path-finding for large dynamic scenarios.
using System;
using System.Drawing;

namespace YinYang.CodeProject.Projects.SimplePathfinding.PathFinders.AStar
{
    public class Node : IComparable<Node>, IEquatable<Node>
    {
        #region | Properties |

        /// <summary>
        /// Gets the node coordinates in a map.
        /// </summary>
        public Point Point { get; private set; }

        /// <summary>
        /// Gets the origin (the node from which this node was opened).
        /// </summary>
        public Node Origin { get; private set; }

        /// <summary>
        /// Gets the actual score (distance to a finish).
        /// </summary>
        public Int32 Score { get; private set; }

        private Int32 EstimatedScore { get; set; }

        /// <summary>
        /// Determiens whether the node was already processed (true) or not.
        /// </summary>
        public Boolean IsClosed { get; private set; }

        #endregion

        #region | Constructors |

        /// <summary>
        /// Initializes a new instance of the <see cref="Node" /> struct.
        /// </summary>
        /// <param name="point">The point.</param>
        /// <param name="score">The score.</param>
        /// <param name="estimatedScore">The estimated score.</param>
        /// <param name="origin">The origin.</param>
        public Node(Point point, Int32 score = 0, Int32 estimatedScore = 0, Node origin = null)
        {
            Point = point;
            Origin = origin;
            IsClosed = false;

            Score = score;
            EstimatedScore = estimatedScore;
        }

        #endregion

        #region | Methods |

        /// <summary>
        /// Marks node as closed.
        /// </summary>
        public void MarkClosed()
        {
            IsClosed = true;
        }

        #endregion

        #region << IComparable >>

        /// <summary>
        /// See <see cref="IComparable{T}.CompareTo"/> for more details.
        /// </summary>
        public Int32 CompareTo(Node other)
        {
            return EstimatedScore.CompareTo(other.EstimatedScore);
        }

        #endregion

        #region << IEquatable >>

        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <param name="other">An object to compare with this object.</param>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false.
        /// </returns>
        public Boolean Equals(Node other)
        {
            return Point == other.Point;
        }

        #endregion

        #region << Object >>

        /// <summary>
        /// Returns a <see cref="System.String" /> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String" /> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            return string.Format("X = {0}, Y = {1}, Score = {2}, Estimated score = {3}", Point.X, Point.Y, Score, EstimatedScore);
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Czech Republic Czech Republic
Contacts: EMAIL - smartk8@gmail.com

Comments and Discussions