コード例 #1
0
ファイル: PolygonEx.cs プロジェクト: cs-util/GeometryEx
        /// <summary>
        /// Returns a list of Vector3 points representing the corners of the Polygon's orthogonal bounding box.
        /// </summary>
        public static List <Vector3> BoxCorners(this Polygon polygon)
        {
            var box = new CompassBox(polygon);

            return(new List <Vector3>
            {
                box.SW,
                box.SE,
                box.NE,
                box.NW
            });
        }
コード例 #2
0
ファイル: Shaper.cs プロジェクト: cs-util/GeometryEx
        /// <summary>
        /// Creates a rectilinear Polygon in the specified adjacent quadrant to the supplied Polygon's bounding box.
        /// </summary>
        /// <param name="area">Desired area of the new Polygon.</param>
        /// <param name="orient">Relative cardinal direction in which the new Polygon will be placed.</param>
        /// <returns>
        /// A new Polygon.
        /// </returns>
        public static Polygon AdjacentArea(Polygon polygon, double area, Orient orient)
        {
            if (polygon == null)
            {
                return(null);
            }
            var    box = new CompassBox(polygon);
            double sizeX;
            double sizeY = 0.0;

            if (orient == Orient.N || orient == Orient.S)
            {
                sizeX = box.SizeX;
                sizeY = area / box.SizeX;
            }
            else
            {
                sizeX = area / box.SizeY;
                sizeY = box.SizeY;
            }
            Vector3 origin = Vector3.Origin;

            switch (orient)
            {
            case Orient.N:
                origin = box.NW;
                break;

            case Orient.E:
                origin = box.SE;
                break;

            case Orient.S:
                origin = new Vector3(box.SW.X, box.SW.Y - sizeY);
                break;

            case Orient.W:
                origin = new Vector3(box.SW.X - sizeX, box.SW.Y);
                break;
            }
            return
                (new Polygon
                 (
                     new[]
            {
                origin,
                new Vector3(origin.X + sizeX, origin.Y),
                new Vector3(origin.X + sizeX, origin.Y + sizeY),
                new Vector3(origin.X, origin.Y + sizeY)
            }
                 ));
        }
コード例 #3
0
ファイル: CoordinateGrid.cs プロジェクト: cs-util/GeometryEx
        /// <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);
                }
            }
        }