Point.cs
上传用户:sex100000
上传日期:2013-11-09
资源大小:1377k
文件大小:12k
- // 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;
- using System.Runtime.Serialization;
- using System.Runtime.InteropServices;
- namespace SharpMap.Geometries
- {
- /// <summary>
- /// A Point is a 0-dimensional geometry and represents a single location in 2D coordinate space. A Point has a x coordinate
- /// value and a y-coordinate value. The boundary of a Point is the empty set.
- /// </summary>
- [Serializable]
- public class Point : Geometry, IComparable<Point>
- {
- private double _X;
- private double _Y;
- private bool _IsEmpty = false;
- /// <summary>
- /// Sets whether this object is empty
- /// </summary>
- protected bool SetIsEmpty
- {
- set { _IsEmpty = value; }
- }
- /// <summary>
- /// Initializes a new Point
- /// </summary>
- /// <param name="x">X coordinate</param>
- /// <param name="y">Y coordinate</param>
- public Point(double x, double y)
- {
- _X = x; _Y = y;
- }
- /// <summary>
- /// Initializes a new empty Point
- /// </summary>
- public Point() : this(0, 0) { _IsEmpty = true; }
- /// <summary>
- /// Returns a point based on degrees, minutes and seconds notation.
- /// For western or southern coordinates, add minus '-' in front of all longitude and/or latitude values
- /// </summary>
- /// <param name="longDegrees">Longitude degrees</param>
- /// <param name="longMinutes">Longitude minutes</param>
- /// <param name="longSeconds">Longitude seconds</param>
- /// <param name="latDegrees">Latitude degrees</param>
- /// <param name="latMinutes">Latitude minutes</param>
- /// <param name="latSeconds">Latitude seconds</param>
- /// <returns>Point</returns>
- public static Point FromDMS(double longDegrees, double longMinutes, double longSeconds,
- double latDegrees, double latMinutes, double latSeconds)
- {
- return new Point(longDegrees + longMinutes / 60 + longSeconds / 3600,
- latDegrees + latMinutes / 60 + latSeconds / 3600);
- }
- /// <summary>
- /// Returns a 2D <see cref="Point"/> instance from this <see cref="Point3D"/>
- /// </summary>
- /// <returns><see cref="Point"/></returns>
- public Point AsPoint()
- {
- return new Point(_X, _Y);
- }
- /// <summary>
- /// Gets or sets the X coordinate of the point
- /// </summary>
- public double X
- {
- get
- {
- if (!_IsEmpty)
- return _X;
- else throw new ApplicationException("Point is empty");
- }
- set { _X = value; _IsEmpty = false; }
- }
- /// <summary>
- /// Gets or sets the Y coordinate of the point
- /// </summary>
- public double Y
- {
- get
- {
- if (!_IsEmpty)
- return _Y;
- else throw new ApplicationException("Point is empty");
- }
- set { _Y = value; _IsEmpty = false; }
- }
- /// <summary>
- /// Returns part of coordinate. Index 0 = X, Index 1 = Y
- /// </summary>
- /// <param name="index"></param>
- /// <returns></returns>
- public virtual double this[uint index]
- {
- get
- {
- if (_IsEmpty)
- throw new ApplicationException("Point is empty");
- else if (index == 0)
- return this.X;
- else if
- (index == 1)
- return this.Y;
- else
- throw (new System.Exception("Point index out of bounds"));
- }
- set
- {
- if (index == 0)
- this.X = value;
- else if (index == 1)
- this.Y = value;
- else
- throw (new System.Exception("Point index out of bounds"));
- _IsEmpty = false;
- }
- }
- /// <summary>
- /// Returns the number of ordinates for this point
- /// </summary>
- public virtual int NumOrdinates
- {
- get { return 2; }
- }
- /// <summary>
- /// Transforms the point to image coordinates, based on the map
- /// </summary>
- /// <param name="map">Map to base coordinates on</param>
- /// <returns>point in image coordinates</returns>
- public System.Drawing.PointF TransformToImage(Map map)
- {
- return SharpMap.Utilities.Transform.WorldtoMap(this, map);
- }
- #region Operators
- /// <summary>
- /// Vector + Vector
- /// </summary>
- /// <param name="v1">Vector</param>
- /// <param name="v2">Vector</param>
- /// <returns></returns>
- public static Point operator +(Point v1, Point v2)
- { return new Point(v1.X + v2.X, v1.Y + v2.Y); }
- /// <summary>
- /// Vector - Vector
- /// </summary>
- /// <param name="v1">Vector</param>
- /// <param name="v2">Vector</param>
- /// <returns>Cross product</returns>
- public static Point operator -(Point v1, Point v2)
- { return new Point(v1.X - v2.X, v1.Y - v2.Y); }
- /// <summary>
- /// Vector * Scalar
- /// </summary>
- /// <param name="m">Vector</param>
- /// <param name="d">Scalar (double)</param>
- /// <returns></returns>
- public static Point operator *(Point m, double d)
- { return new Point(m.X * d, m.Y * d); }
- #endregion
- #region "Inherited methods from abstract class Geometry"
- /// <summary>
- /// Checks whether this instance is spatially equal to the Point 'o'
- /// </summary>
- /// <param name="p">Point to compare to</param>
- /// <returns></returns>
- public virtual bool Equals(Point p)
- {
- return p != null && p.X == _X && p.Y == _Y && this._IsEmpty == p.IsEmpty();
- }
- /// <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()
- {
- return _X.GetHashCode() ^ _Y.GetHashCode() ^ _IsEmpty.GetHashCode();
- }
- /// <summary>
- /// The inherent dimension of this Geometry object, which must be less than or equal to the coordinate dimension.
- /// </summary>
- public override int Dimension
- {
- get { return 0; }
- }
- /// <summary>
- /// If true, then this Geometry represents the empty point set,