Click here to Skip to main content
15,886,258 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
{
    public abstract class BaseGraphSearchNode<TNode> where TNode : BaseGraphSearchNode<TNode>
    {
        #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 TNode Origin { get; protected 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="TNode" /> struct.
        /// </summary>
        /// <param name="point">The point.</param>
        /// <param name="origin">The origin.</param>
        protected BaseGraphSearchNode(Point point, TNode origin = null)
        {
            Point = point;
            Origin = origin;
            IsClosed = false;
        }

        #endregion

        #region | Methods |

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

        #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(TNode 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}", Point.X, Point.Y);
        }

        #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