private static Polygon PointsToPolygon(IEnumerable <Point2D> points) { List <Point2D> pointList = points.ToList(); Point2D firstPoint = pointList.First(); if (!firstPoint.Equals(pointList.Last())) { pointList.Add(firstPoint); } Coordinate[] coordinates = pointList.Select(p => new Coordinate(p.X, p.Y)).ToArray(); return(new Polygon(new LinearRing(coordinates))); }
/// <summary> /// Gets an indicator whether the given <paramref name="point"/> lies in the polygon. /// </summary> /// <param name="point">The point to check.</param> /// <param name="outerRing">The outer ring of the polygon.</param> /// <param name="innerRings">The inner rings of the polygon.</param> /// <returns><c>true</c> when the <paramref name="point"/> lies in the polygon; <c>false</c> otherwise.</returns> /// <exception cref="ArgumentNullException">Thrown when any parameter is <c>null</c>.</exception> public static bool PointInPolygon(Point2D point, IEnumerable <Point2D> outerRing, IEnumerable <IEnumerable <Point2D> > innerRings) { if (point == null) { throw new ArgumentNullException(nameof(point)); } if (outerRing == null) { throw new ArgumentNullException(nameof(outerRing)); } if (innerRings == null) { throw new ArgumentNullException(nameof(innerRings)); } Polygon outerPolygon = PointsToPolygon(outerRing); IEnumerable <Polygon> innerPolygons = innerRings.Select(PointsToPolygon).ToArray(); var polygon = new Polygon(outerPolygon.Shell, innerPolygons.Select(p => p.Shell).ToArray()); return(polygon.Covers(new Point(point.X, point.Y))); }
/// <summary> /// Transforms X coordinates in a 2D X, Y plane using: /// - A reference point as starting point of the line. /// - An offset at which the reference coincides with the X axis. /// - A rotation from North of the X coordinates around the origin after subtracting the offset in degrees. /// </summary> /// <param name="xCoordinates">The X coordinates of a line.</param> /// <param name="referencePoint">The point of reference where the line is transposed to.</param> /// <param name="offset">The offset at which the referencePoints coincides with the X axis.</param> /// <param name="rotation">The rotation from the North in degrees.</param> /// <returns>A collection of <see cref="Point2D"/> with the transformed X coordinates.</returns> /// <exception cref="ArgumentNullException">Thrown when <paramref name="xCoordinates"/> or /// <paramref name="referencePoint"/> is <c>null</c>.</exception> public static IEnumerable <Point2D> FromXToXY(IEnumerable <double> xCoordinates, Point2D referencePoint, double offset, double rotation) { if (xCoordinates == null) { throw new ArgumentNullException(nameof(xCoordinates), @"Cannot transform to coordinates without a source."); } if (referencePoint == null) { throw new ArgumentNullException(nameof(referencePoint), @"Cannot transform to coordinates without a reference point."); } return(xCoordinates.Select(coordinate => { var referenceVector = new Vector2D(referencePoint.X, referencePoint.Y); Vector2D pointVector = referenceVector + new Vector2D(0, coordinate - offset).Rotate(-rotation, AngleUnit.Degrees); return new Point2D(pointVector.X, pointVector.Y); }).ToArray()); }
public void Create_SoilProfile_ExpectedLayersWithSoil() { // Setup var outerRing1 = new Point2D[0]; var outerRing2 = new Point2D[0]; var outerRing3 = new Point2D[0]; var outerRing4 = new Point2D[0]; var outerRing5 = new Point2D[0]; var outerRing6 = new Point2D[0]; var soilProfile = new SoilProfile ( new[] { new SoilLayer(outerRing1, CreateRandomConstructionProperties(21, "Material 1"), new[] { new SoilLayer(outerRing2, CreateRandomConstructionProperties(22, "Material 2"), new[] { new SoilLayer(outerRing3, CreateRandomConstructionProperties(22, "Material 3"), Enumerable.Empty <SoilLayer>()) }), new SoilLayer(outerRing4, CreateRandomConstructionProperties(23, "Material 4"), Enumerable.Empty <SoilLayer>()) }), new SoilLayer(outerRing5, CreateRandomConstructionProperties(24, "Material 5"), new[] { new SoilLayer(outerRing6, CreateRandomConstructionProperties(25, "Material 6"), Enumerable.Empty <SoilLayer>()) }) }, Enumerable.Empty <PreconsolidationStress>() ); // Call LayerWithSoil[] layersWithSoil = LayerWithSoilCreator.Create(soilProfile, out IDictionary <SoilLayer, LayerWithSoil> layerLookup); // Assert SoilLayer layer1 = soilProfile.Layers.ElementAt(0); SoilLayer layer2 = layer1.NestedLayers.ElementAt(0); SoilLayer layer3 = layer2.NestedLayers.ElementAt(0); SoilLayer layer4 = layer1.NestedLayers.ElementAt(1); SoilLayer layer5 = soilProfile.Layers.ElementAt(1); SoilLayer layer6 = layer5.NestedLayers.ElementAt(0); Assert.AreEqual(6, layersWithSoil.Length); Assert.AreEqual(outerRing1, layersWithSoil[0].OuterRing); CollectionAssert.AreEqual(new[] { outerRing2, outerRing3, outerRing4 }, layersWithSoil[0].InnerRings); AssertSoilLayerProperties(layer1, layersWithSoil[0]); Assert.AreEqual(outerRing2, layersWithSoil[1].OuterRing); CollectionAssert.AreEqual(new[] { outerRing3 }, layersWithSoil[1].InnerRings); AssertSoilLayerProperties(layer2, layersWithSoil[1]); Assert.AreEqual(outerRing3, layersWithSoil[2].OuterRing); CollectionAssert.IsEmpty(layersWithSoil[2].InnerRings); AssertSoilLayerProperties(layer3, layersWithSoil[2]); Assert.AreEqual(outerRing4, layersWithSoil[3].OuterRing); CollectionAssert.IsEmpty(layersWithSoil[3].InnerRings); AssertSoilLayerProperties(layer4, layersWithSoil[3]); Assert.AreEqual(outerRing5, layersWithSoil[4].OuterRing); CollectionAssert.AreEqual(new[] { outerRing6 }, layersWithSoil[4].InnerRings); AssertSoilLayerProperties(layer5, layersWithSoil[4]); Assert.AreEqual(outerRing6, layersWithSoil[5].OuterRing); CollectionAssert.IsEmpty(layersWithSoil[5].InnerRings); AssertSoilLayerProperties(layer6, layersWithSoil[5]); SoilLayer[] originalSoilLayers = { layer1, layer2, layer3, layer4, layer5, layer6 }; Assert.AreEqual(layersWithSoil.Length, layerLookup.Count); for (var i = 0; i < layersWithSoil.Length; i++) { Assert.AreSame(originalSoilLayers[i], layerLookup.ElementAt(i).Key); Assert.AreSame(layersWithSoil.ElementAt(i), layerLookup.ElementAt(i).Value); } }