linestring.cs

来自「Sharp Map 用于制作GIS系统S harp Map 用于制作GIS系统S」· CS 代码 · 共 126 行

CS
126
字号
// Copyright 2005, 2006 - Morten Nielsen (www.iter.dk)
//
// This file is part of SharpMap.
// SharpMap is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// SharpMap is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.

// You should have received a copy of the GNU Lesser General Public License
// along with SharpMap; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 

using System;
using System.Collections.Generic;
using System.Text;

namespace SharpMap.Geometries
{
	/// <summary>
	/// A LineString is a Curve with linear interpolation between points. Each consecutive pair of points defines a
	/// line segment.
	/// </summary>
	[Serializable]
	public class LineString : Curve
	{
		private List<Point> _Vertices;

		/// <summary>
		/// Initializes an instance of a LineString from a set of vertices
		/// </summary>
		/// <param name="vertices"></param>
		public LineString(List<Point> vertices)
		{
			_Vertices = vertices;
		}

		/// <summary>
		/// Initializes an instance of a LineString
		/// </summary>
		public LineString() : this(new List<Point>()) { }

		/// <summary>
		/// Gets or sets the collection of vertices in this Geometry
		/// </summary>
		public virtual List<Point> Vertices
		{
			get { return _Vertices; }
			set { _Vertices = value; }
		}

		#region OpenGIS Methods

		/// <summary>
		/// Returns the specified point N in this Linestring.
		/// </summary>
		/// <remarks>This method is supplied as part of the OpenGIS Simple Features Specification</remarks>
		/// <param name="N"></param>
		/// <returns></returns>
		public Point Point(int N)
		{
			return _Vertices[N];
		}

		/// <summary>
		/// The number of points in this LineString.
		/// </summary>
		/// <remarks>This method is supplied as part of the OpenGIS Simple Features Specification</remarks>
		public virtual int NumPoints
		{
			get { return _Vertices.Count; }
		}
		#endregion
		
		/// <summary>
		/// Transforms the linestring to image coordinates, based on the map
		/// </summary>
		/// <param name="map">Map to base coordinates on</param>
		/// <returns>Linestring in image coordinates</returns>
		public System.Drawing.PointF[] TransformToImage(Map map)
		{
			System.Drawing.PointF[] v = new System.Drawing.PointF[_Vertices.Count];
			for (int i = 0; i < this.Vertices.Count; i++)
				v[i] = SharpMap.Utilities.Transform.WorldtoMap(_Vertices[i], map);
			return v;
		}	


		#region "Inherited methods from abstract class Geometry"

		/// <summary>
		/// Checks whether this instance is spatially equal to the LineString 'l'
		/// </summary>
		/// <param name="l">LineString to compare to</param>
		/// <returns>true of the objects are spatially equal</returns>
		public bool Equals(LineString l)
		{
			if (l == null)
				return false;
			if (l.Vertices.Count != this.Vertices.Count)
				return false;
			for(int i = 0; i < l.Vertices.Count; i++)
				if (!l.Vertices[i].Equals(this.Vertices[i]))
					return false;
			return true;
		}

		/// <summary>
		/// Serves as a hash function for a particular type. <see cref="GetHashCode"/> is suitable for use 
		/// in hashing algorithms and data structures like a hash table.
		/// </summary>
		/// <returns>A hash code for the current <see cref="GetHashCode"/>.</returns>
		public override int GetHashCode()
		{
			int hash = 0;
			for (int i = 0; i < Vertices.Count; i++)
				hash = hash ^ Vertices[i].GetHashCode();
			return hash;
		}

		/// <summary>
		/// If true, then this Geometry represents the empty point set, 

⌨️ 快捷键说明

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