LineString.cs
上传用户:sex100000
上传日期:2013-11-09
资源大小:1377k
文件大小:11k
- // 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.Collections.ObjectModel;
- 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 Collection<Point> _Vertices;
- /// <summary>
- /// Initializes an instance of a LineString from a set of vertices
- /// </summary>
- /// <param name="vertices"></param>
- public LineString(Collection<Point> vertices)
- {
- _Vertices = vertices;
- }
- /// <summary>
- /// Initializes an instance of a LineString
- /// </summary>
- public LineString() : this(new Collection<Point>()) { }
- /// <summary>
- /// Gets or sets the collection of vertices in this Geometry
- /// </summary>
- public virtual Collection<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,