⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 node.cs

📁 实现A*算法的C#源代码
💻 CS
字号:
/*
    作者:Tom Xu(tsing_feng@163.com)
    创建时间:2006-11-03 13:40
    内容描述:生成邻接矩阵的所需要的数据结构
*/

using System;
using System.Collections ;
using System.Collections.Generic;
namespace Iaspec.GIS.Common
{
	
	public class Node:IComparable<Node>,IEquatable<Node>
	{

		public double TotalCost
		{
			get 
			{
				return g+h;
			}
			set
			{
				TotalCost = value;
			}
		}

		/// <summary>
		/// Cost between the current node to original node
		/// </summary>
        private double g;

        public double G
        {
            get { return g; }
            set { g = value; }
        }
        
        /// <summary>
        /// Linear Distance between the current node to denstination node
        /// </summary>
        private double h;

        public double H
        {
            get { return h; }
            set { h = value; }
        }
        private double x;

        public double X
        {
            get { return x; }
            set { x = value; }
        }
        private double y;

        public double Y
        {
            get { return y; }
            set { y = value; }
        }

        private int _id;
        public int ID
        {
            get { return _id; }
        }
		
		private Node _goalNode;
		private Node _parentNode;
        public Node ParentNode
        {
            get { return _parentNode; }
            set { _parentNode = value; }
        }
		private double _gCost;

        /// <summary>
        ///  Constructor
        /// </summary>
		public Node(Node parentNode, Node goalNode, double gCost,double x,double y,int ID)
		{
			this._parentNode = parentNode;
			this._goalNode = goalNode;
			this._gCost = gCost;
            this._id = ID;
            this.x = x;
            this.y = y;
            this.g = (_parentNode != null) ? _parentNode.g + _gCost : _gCost;
            this.h = (_goalNode != null) ? (int)Euclidean_H() : 0;           
		}
	

        /// <summary>
        /// Distance between current node to denstination node
        /// </summary>
        /// <returns></returns>
		private double Euclidean_H()
		{
			double xd = this.x - this._goalNode.x ;
			double yd = this.y - this._goalNode.y ;
            return Math.Sqrt(xd * xd + yd * yd);
		}

        #region IComparable<Node> Members

        public int CompareTo(Node other)
        {
            double d = this.TotalCost - other.TotalCost;
            if (d > 0)
                return 1;
            else if (d == 0)
                return 0;
            else
                return -1;
        }

        #endregion

        #region IEquatable<Node> Members

        public bool Equals(Node other)
        {
            if (other != null)
                return x == other.x && y == other.y;
            else
                return false;
        }

        #endregion
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -