using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Runtime.Serialization;
|
using System.Security.Permissions;
|
|
namespace Hydro.Model
|
{
|
[DataContract]
|
public class Point2d : ICloneable
|
{
|
[DataMember]
|
public double X { get { return x; } set { x = value; } }
|
[DataMember]
|
public double Y { get { return y; } set { y = value; } }
|
|
|
protected double x;
|
protected double y;
|
|
#region 构造函数
|
public Point2d() { }
|
|
|
|
public Point2d(double x, double y)
|
{
|
this.x = x;
|
this.y = y;
|
}
|
public Point2d(int x, int y)
|
{
|
this.x = x;
|
this.y = y;
|
}
|
public Point2d(decimal x, decimal y)
|
{
|
this.x = Convert.ToDouble(x);
|
this.y = Convert.ToDouble(y);
|
}
|
public Point2d(Point rhs)
|
{
|
this.X = rhs.X;
|
this.Y = rhs.Y;
|
}
|
public Point2d(Point2d rhs)
|
{
|
this.X = rhs.X;
|
this.Y = rhs.Y;
|
}
|
public Point2d(string strPara)
|
{
|
if (string.IsNullOrEmpty(strPara))
|
return;
|
string[] strPara_split_array = strPara.Split(',');
|
if (strPara_split_array.Count() < 2)
|
return;
|
X = Convert.ToDouble(strPara_split_array[0]);
|
Y = Convert.ToDouble(strPara_split_array[1]);
|
}
|
public static Point2d ToParameter(string strPara)
|
{
|
if (string.IsNullOrEmpty(strPara))
|
return null;
|
string[] strPara_split_array = strPara.Split(',');
|
if (strPara_split_array.Count() < 2)
|
return null;
|
Point2d pt = new Point2d();
|
pt.X = Convert.ToDouble(strPara_split_array[0]);
|
pt.Y = Convert.ToDouble(strPara_split_array[1]);
|
return pt;
|
}
|
|
#endregion
|
|
#region ToString
|
public override string ToString()
|
{
|
return string.Format("X:{0},Y:{1}", X, Y );
|
}
|
|
#endregion
|
|
#region Clone
|
public Point2d Clone() //对外提供一个创建自身的浅表副本的能力
|
{
|
return new Point2d(x, y);
|
}
|
object ICloneable.Clone()
|
{
|
return this.Clone();
|
}
|
public override bool Equals(object obj)
|
{
|
Point2d rhs = obj as Point2d;
|
return this.X == rhs.X && this.Y == rhs.Y;
|
}
|
public static Point2d Copy(Point2d pt)
|
{
|
if (pt == null)
|
return null;
|
return new Point2d(pt);
|
}
|
public static List<Point2d> Copy(List<Point2d> list)
|
{
|
if (list == null)
|
return null;
|
List<Point2d> newList = new List<Point2d>();
|
foreach (var pt in list)
|
newList.Add(new Point2d(pt));
|
|
return newList;
|
}
|
#endregion
|
|
/// <summary>
|
/// 偏置
|
/// </summary>
|
/// <param name="x"></param>
|
/// <param name="y"></param>
|
/// <returns></returns>
|
public Point2d Offset(double x, double y)
|
{
|
return new Point2d(this.x + x, this.y + y);
|
}
|
|
|
|
/// <summary>
|
/// Return the HashCode from the base class.
|
/// </summary>
|
/// <returns></returns>
|
public override int GetHashCode()
|
{
|
return base.GetHashCode();
|
}
|
|
/// <summary>
|
/// Missing values are represented internally using <see cref="System.Double.MaxValue"/>.
|
/// </summary>
|
public const double Missing = Double.MaxValue;
|
|
/// <summary>
|
/// The default format to be used for displaying model values via the
|
/// <see cref="ToString()"/> method.
|
/// </summary>
|
public const string DefaultFormat = "N2";
|
|
#region Serialization
|
|
public const int schema = 11;
|
|
protected Point2d(SerializationInfo info, StreamingContext context)
|
{
|
|
X = info.GetDouble("X");
|
Y = info.GetDouble("Y");
|
}
|
|
[SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
|
public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
|
{
|
info.AddValue("schema", schema);
|
info.AddValue("X", X);
|
info.AddValue("Y", Y);
|
}
|
public string ToDsString()
|
{
|
return ToDsString(this);
|
}
|
|
public static string ToDsString(Point2d point)
|
{
|
return string.Format("{0},{1}", point.X, point.Y );
|
}
|
|
|
#endregion
|
|
#region Properties
|
|
/// <summary>
|
/// Readonly value that determines if either the X or the Y
|
/// coordinate in this PointPair is a missing value.
|
/// </summary>
|
/// <returns>true if either value is missing</returns>
|
public bool IsMissing()
|
{
|
return this.X == Point2d.Missing || this.Y == Point2d.Missing ;
|
}
|
|
/// <summary>
|
/// Readonly value that determines if either the X or the Y
|
/// coordinate in this PointPair is an invalid (not plotable) value.
|
/// It is considered invalid if it is missing (equal to System.Double.MaxWholeAxis),
|
/// Infinity, or NaN.
|
/// </summary>
|
/// <returns>true if either value is invalid</returns>
|
public bool IsInvalid()
|
{
|
return this.X == Point2d.Missing ||
|
this.Y == Point2d.Missing ||
|
Double.IsInfinity(this.X) ||
|
Double.IsInfinity(this.Y) ||
|
Double.IsNaN(this.X) ||
|
Double.IsNaN(this.Y) ;
|
|
}
|
public bool IsZeroPt()
|
{
|
if ((Math.Abs(x) < 0.00001) && (Math.Abs(y) < 0.00001) )
|
return true;
|
else
|
return false;
|
}
|
/// <summary>
|
/// static method to determine if the specified model value is invalid.
|
/// </summary>
|
/// <remarks>The value is considered invalid if it is <see cref="PointPairBase.Missing"/>,
|
/// <see cref="Double.PositiveInfinity"/>, <see cref="Double.NegativeInfinity"/>
|
/// or <see cref="Double.NaN"/>.</remarks>
|
/// <param name="value">The value to be checked for validity.</param>
|
/// <returns>true if the value is invalid, false otherwise</returns>
|
public static bool IsValueInvalid(double value)
|
{
|
return (value == Point2d.Missing ||
|
Double.IsInfinity(value) ||
|
Double.IsNaN(value));
|
}
|
|
#endregion
|
|
|
public enum eSortType
|
{
|
X,
|
Y
|
}
|
public enum eCompareType
|
{
|
X,
|
Y,
|
ALL
|
}
|
|
//排序与比较
|
public class Comparer : IComparer<Hydro.Model.Point2d>
|
{
|
private Hydro.Model.Point2d.eSortType sortType;
|
public Comparer(Hydro.Model.Point2d.eSortType type)
|
{
|
sortType = type;
|
}
|
|
#region IComparer<FeatPoint> 成员
|
int IComparer<Hydro.Model.Point2d>.Compare(Hydro.Model.Point2d obj1, Hydro.Model.Point2d obj2)
|
{
|
if (sortType == Hydro.Model.Point2d.eSortType.X)
|
{
|
if (Math.Abs(obj1.X - obj2.X) < 0.00001)
|
{
|
return 0;
|
}
|
else if (obj1.X > obj2.X)
|
{
|
return 1;
|
}
|
else
|
{
|
return -1;
|
}
|
}
|
|
else
|
{
|
if (Math.Abs(obj1.Y - obj2.Y) < 0.00001)
|
{
|
return 0;
|
}
|
else if (obj1.Y > obj2.Y)
|
{
|
return 1;
|
}
|
else
|
{
|
return -1;
|
}
|
}
|
}
|
|
#endregion
|
}
|
|
//比较相同:主要用与LIST的Contains方法和Distinct
|
public class EqualComparer : IEqualityComparer<Hydro.Model.Point2d>
|
{
|
private double ignoreDis = 0.00001;
|
public Hydro.Model.Point2d.eCompareType CompareType = eCompareType.X;//比较类型
|
public EqualComparer()
|
{
|
ignoreDis = 0.00001;
|
}
|
public EqualComparer(double dis, Hydro.Model.Point2d.eCompareType compareType)
|
{
|
ignoreDis = dis;
|
CompareType = compareType;
|
}
|
|
public bool Equals(Hydro.Model.Point2d lhs, Hydro.Model.Point2d rhs)
|
{
|
switch (CompareType)
|
{
|
case eCompareType.X:
|
if (Math.Abs(lhs.X - rhs.X) < ignoreDis)
|
return true;
|
break;
|
case eCompareType.Y:
|
if (Math.Abs(lhs.Y - rhs.Y) < ignoreDis)
|
return true;
|
break;
|
case eCompareType.ALL:
|
if (Math.Abs(lhs.X - rhs.X) < ignoreDis && Math.Abs(lhs.Y - rhs.Y) < ignoreDis )
|
return true;
|
break;
|
}
|
return false;
|
}
|
|
public int GetHashCode(Hydro.Model.Point2d obj)
|
{
|
//return obj.X.GetHashCode() + obj.Y.GetHashCode();
|
return 0;
|
|
}
|
}
|
|
//距离
|
public double Distance(Point2d pt)
|
{
|
return Math.Sqrt((pt.X - this.X) * (pt.X - this.X)
|
+ (pt.Y - this.Y) * (pt.Y - this.Y)
|
|
);
|
}
|
}
|
|
|
|
|
}
|