// Copyright (C) Stichting Deltares 2016. All rights reserved. // // This file is part of Ringtoets. // // Ringtoets 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 3 of the License, or // (at your option) any later version. // // This program 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 this program. If not, see . // // All names, logos, and references to "Deltares" are registered trademarks of // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. using System; using MathNet.Numerics.LinearAlgebra.Double; namespace Core.Common.Base.Geometry { /// /// Defines a mathematical point in 2D Euclidean space. /// public class Point2D { /// /// Creates a new instance of , with set to 0 /// and set to 0. /// public Point2D() {} /// /// Creates a new instance of , with set to /// and set to . /// /// The x coordinate to set. /// The y coordinate to set. public Point2D(double x, double y) { X = x; Y = y; } /// /// Gets or sets the x coordinate. /// public double X { get; private set; } /// /// Gets or sets the y coordinate. /// public double Y { get; private set; } /// /// Determines the 2D vector defined by the difference of two . /// /// Head of the vector. /// Tail of the vector. /// A 2D vector. public static Vector operator -(Point2D p1, Point2D p2) { var result = new DenseVector(2); result[0] = p1.X - p2.X; result[1] = p1.Y - p2.Y; return result; } /// /// Gets the euclidean distance from this point to another. /// /// The second point. /// A value of 0 or greater. /// When is null. public double GetEuclideanDistanceTo(Point2D secondPoint) { if (secondPoint == null) { throw new ArgumentNullException("secondPoint"); } Vector vector = this - secondPoint; return Math.Sqrt(vector.DotProduct(vector)); } public override bool Equals(object obj) { if (ReferenceEquals(null, obj)) { return false; } if (ReferenceEquals(this, obj)) { return true; } if (obj.GetType() != GetType()) { return false; } return Equals((Point2D)obj); } public override int GetHashCode() { unchecked { var hashCode = X.GetHashCode(); hashCode = (hashCode * 397) ^ Y.GetHashCode(); return hashCode; } } public override string ToString() { return String.Format("({0}, {1})", X, Y); } /// /// Compares the with based on and . /// /// A to compare with. /// True if the coordinates of the matches the coordinate of . False otherwise. private bool Equals(Point2D other) { return X.Equals(other.X) && Y.Equals(other.Y); } } }