using System; using System.Collections.Generic; using System.Linq; using System.Text; using DHI.OpenMI2.Sdk.Backbone; using DHI.OpenMI2.Sdk.Spatial; namespace MikeSheInOpenDA.Spatial { public class XYZGeometryTools { public const double EPSILON = 1e-5; /// /// Returns the distance between the two points. /// /// Point /// Point /// Point to point distance public static double CalculatePointToPointDistance2D(IXYLayerPoint p1, IXYLayerPoint p2) { double dx = p1.X - p2.X; double dy = p1.Y - p2.Y; return Math.Sqrt(dx * dx + dy * dy); } /// /// Returns the distance between the two points. /// /// Point /// Point /// Point to point distance public static double CalculatePointToPointDistance2D(IXYLayerPoint p1, ICoordinate p2) { double dx = p1.X - p2.X; double dy = p1.Y - p2.Y; return Math.Sqrt(dx * dx + dy * dy); } public static bool IsPointInPolygon(double x, double y, XYPolygon polygon) { if (x > polygon.GetX(0) && x < polygon.GetX(1) && y > polygon.GetY(0) && y < polygon.GetY(2)) { return true; } else { return false; } } /// /// Finds if the given point is located in the model grid. /// Cycles through every grid rectangle. /// /// point to check if it's within the grid. /// the model grid. /// public static bool IsPointInModelPlain(ICoordinate pt, IDictionary modelCoordinates) { return modelCoordinates.Values.Any(rec => rec.PointInObject(new XYLayerPoint(pt.X, pt.Y, 0))); } /// /// Finds if the given point is located in the model grid. /// Cycles through every grid rectangle. /// /// point to check if it's within the grid. /// the model grid. /// public static bool IsPointInModelPlain(IXYLayerPoint pt, IDictionary modelCoordinates) { return modelCoordinates.Values.Any(rec => rec.PointInObject(new XYLayerPoint(pt.X, pt.Y, pt.Layer))); } /// /// Model Index for given point /// /// point to check if it's within the grid. /// the model grid. /// public static int ModelIndexForPoint(IXYLayerPoint pt, IDictionary modelCoordinates) { foreach(KeyValuePair modelGrid in modelCoordinates) { if ( modelGrid.Value.PointInObject( pt ) ) { return modelGrid.Key; } } return -999; } } }