Exemplo n.º 1
0
        /// <summary>
        /// Returns the perpendicular distance from this Line to the supplied Vector3 point.
        /// </summary>
        /// <param name="point">Vector3 representing a point.</param>
        /// <returns>A double.</returns>
        public static double PerpendicularDistanceTo(this Line line, Vector3 point)
        {
            if (line.PointOnLine(point, true))
            {
                return(0.0);
            }
            var area = Math.Abs(Shaper.MakePolygon(new[] { line.Start, line.End, point }.ToList()).Area());

            return(area / (line.Length() * 0.5));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Returns a new Polygon rotated around a supplied Vector3 by the specified angle in degrees.
        /// </summary>
        /// <param name="pivot">The Vector3 base point of the rotation.</param>
        /// <param name="angle">The desired rotation angle in degrees.</param>
        /// <returns>
        /// A new Polygon.
        /// </returns>
        public static Polygon Rotate(this Polygon polygon, Vector3 pivot, double angle)
        {
            var theta    = angle * (Math.PI / 180);
            var vertices = new List <Vector3>();

            foreach (Vector3 vertex in polygon.Vertices)
            {
                var rX = (Math.Cos(theta) * (vertex.X - pivot.X)) - (Math.Sin(theta) * (vertex.Y - pivot.Y)) + pivot.X;
                var rY = (Math.Sin(theta) * (vertex.X - pivot.X)) + (Math.Cos(theta) * (vertex.Y - pivot.Y)) + pivot.Y;
                var rZ = vertex.Z;
                vertices.Add(new Vector3(rX, rY, rZ));
            }
            return(Shaper.MakePolygon(vertices));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates a new CCW Polygon of the same vertices with the start point now at the indexed Polygon vertex.
        /// </summary>
        /// <param name="start">The index of the point from which to start the new Polygon.</param>
        /// <returns>A new Polygon.</returns>
        public static Polygon RewindFrom(this Polygon polygon, int start)
        {
            var vertices = polygon.Vertices;

            if (start < 0 || start > vertices.Count - 1)
            {
                return(null);
            }
            var points = new List <Vector3>()
            {
                vertices[start]
            };
            var i = start + 1;

            while (i < vertices.Count)
            {
                points.Add(vertices[i % vertices.Count]);
                i++;
            }
            return(Shaper.MakePolygon(points));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Returns a List of Polygons derived from the Polygon's straight skeleton.
        /// </summary>
        /// <returns>A List of Polygons.</Line></returns>
        public static List <Polygon> Jigsaw(this Polygon polygon)
        {
            var vertices2d = new List <Vector2d>();

            foreach (var vertex in polygon.Vertices)
            {
                vertices2d.Add(new Vector2d(vertex.X, vertex.Y));
            }
            var skeleton = SkeletonBuilder.Build(vertices2d);
            var polygons = new List <Polygon>();

            foreach (var edgeResult in skeleton.Edges)
            {
                var vertices = new List <Vector3>();
                foreach (var vertex in edgeResult.Polygon)
                {
                    vertices.Add(new Vector3(vertex.X, vertex.Y, 0.0));
                }
                polygons.Add(Shaper.MakePolygon(vertices));
            }
            return(polygons.OrderBy(p => p.Centroid()).ToList());
        }
Exemplo n.º 5
0
 /// <summary>
 /// Creates a Polygon with a reduced quantity of points tested against a deviation tolerance.
 /// </summary>
 /// <param name="tolerance">The deviation tolerance for inclusion in the final Vector3 List.</param>
 /// <returns>
 /// A new Polygon.
 /// </returns>
 public static Polygon Straighten(this Polygon polygon, double tolerance)
 {
     return(Shaper.MakePolygon(SimplifyNet.Straighten(polygon.Vertices.ToList(), tolerance)));
 }
Exemplo n.º 6
0
 /// <summary>
 /// Reduces Polygon vertices.
 /// </summary>
 /// <param name="tolerance">The tolerated deviation to include a vertex.</param>
 /// <returns>A new Polyline.</returns>
 public static Polygon Simplify(this Polygon polygon, double minLength, double filletFactor = 1.0)
 {
     return(Shaper.MakePolygon(Shaper.Simplify(polygon.Vertices.ToList(), minLength, filletFactor)));
 }
Exemplo n.º 7
0
        /// <summary>
        /// Creates a rotated 2D grid of Vector3 points from the supplied Polygon, axis intervals, and angle.
        /// </summary>
        /// <param name="perimeter">Polygon boundary of the point grid.</param>
        /// <param name="xInterval">Spacing of the grid along the x-axis.</param>
        /// <param name="yInterval">Spacing of the grid along the y-axis.</param>
        /// <param name="angle">Rotation of the grid around the Polygon centroid.</param>
        /// <returns>
        /// A new CoordGrid.
        /// </returns>
        public CoordinateGrid(Polygon polygon, double xInterval = 1.0, double yInterval = 1.0, double angle = 0.0)
        {
            if (polygon == null)
            {
                return;
            }
            random    = new Random();
            Allocated = new List <Vector3>();
            Available = new List <Vector3>();
            Perimeter = Shaper.MakePolygon(polygon.Vertices.ToList());
            var centroid = polygon.Centroid();
            var box      = new CompassBox(polygon);
            var points   = new List <Vector3>();

            // Northeast quadrant
            var x = centroid.X + (xInterval * 0.5);
            var y = centroid.Y + (yInterval * 0.5);

            while (y <= box.NW.Y)
            {
                while (x <= box.SE.X)
                {
                    points.Add(new Vector3(x, y));
                    x += xInterval;
                }
                x  = centroid.X + (xInterval * 0.5);
                y += yInterval;
            }

            // Northwest quadrant
            x = centroid.X - (xInterval * 0.5);
            y = centroid.Y + (yInterval * 0.5);
            while (y <= box.NW.Y)
            {
                while (x >= box.SW.X)
                {
                    points.Add(new Vector3(x, y));
                    x -= xInterval;
                }
                x  = centroid.X - (xInterval * 0.5);
                y += yInterval;
            }

            // Southeast quadrant
            x = centroid.X + (xInterval * 0.5);
            y = centroid.Y - (yInterval * 0.5);
            while (y >= box.SW.Y)
            {
                while (x <= box.SE.X)
                {
                    points.Add(new Vector3(x, y));
                    x += xInterval;
                }
                x  = centroid.X + (xInterval * 0.5);
                y -= yInterval;
            }

            // Southwest quadrant
            x = centroid.X - (xInterval * 0.5);
            y = centroid.Y - (yInterval * 0.5);
            while (y >= box.SW.Y)
            {
                while (x >= box.SW.X)
                {
                    points.Add(new Vector3(x, y));
                    x -= xInterval;
                }
                x  = centroid.X - (xInterval * 0.5);
                y -= yInterval;
            }
            foreach (var pnt in points)
            {
                var point = pnt.Rotate(centroid, angle);
                if (polygon.Covers(point))
                {
                    Available.Add(point);
                }
            }
        }