// 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 System.Globalization; using Core.Common.Base.Properties; using MathNet.Numerics.LinearAlgebra; using MathNet.Numerics.LinearAlgebra.Double; namespace Core.Common.Base.Geometry { /// /// Defines a mathematical, immutable point in 2D Euclidean space. /// public sealed class 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. /// Thrown when either /// or is null. public static Vector operator -(Point2D p1, Point2D p2) { if (p1 == null) { throw new ArgumentNullException("p1"); } if (p2 == null) { throw new ArgumentNullException("p2"); } var result = new DenseVector(2); result[0] = p1.X - p2.X; result[1] = p1.Y - p2.Y; return result; } /// /// Determines the new 2D point given a point and a 2D vector. /// /// The point. /// The 2D vector. /// /// A 2D point. /// /// Thrown when is /// not a 2D vector. /// Thrown when /// or is null. public static Point2D operator +(Point2D point, Vector vector) { if (point == null) { throw new ArgumentNullException("point"); } if (vector == null) { throw new ArgumentNullException("vector"); } if (vector.Count != 2) { string message = string.Format(CultureInfo.CurrentCulture, Resources.Point2D_AddVector_Vector_must_be_2D_but_has_Dimensionality_0_, vector.Count); throw new ArgumentException(message, "vector"); } double x = point.X + vector[0]; double y = point.Y + vector[1]; return new Point2D(x, y); } /// /// Gets the euclidean distance from this point to another. /// /// The second point. /// A value of 0 or greater. /// Thrown 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(CultureInfo.CurrentCulture, "({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); } } }