/// <inheritdoc />
        /// <summary>
        /// </summary>
        /// <param name="polygon"></param>
        /// <param name="outline"></param>
        /// <param name="penWidth"></param>
        protected override void DrawRoom(PolygonGrid2D polygon, List <Tuple <Vector2Int, bool> > outline, float penWidth)
        {
            // Draw polygon
            data.Append($"    <polygon points=\"");

            foreach (var point in polygon.GetPoints())
            {
                data.Append($"{GetX(point)},{GetY(point)} ");
            }

            data.Append($"\" style=\"fill:rgb(211,211,211);stroke:rgb(211,211,211)\" />");
            data.AppendLine();

            // Draw path
            data.Append($"    <path d=\"");

            var lastPoint = outline[outline.Count - 1].Item1;

            data.Append($"M {GetX(lastPoint)} {GetY(lastPoint)} ");

            foreach (var pair in outline)
            {
                var point = pair.Item1;

                data.Append(pair.Item2 ? $"L {GetX(point)} {GetY(point)} " : $"M {GetX(point)} {GetY(point)} ");
            }

            data.Append($"\" fill=\"none\" stroke=\"black\" stroke-linecap=\"square\" stroke-width=\"{penWidth}\" />");
            data.AppendLine();
        }
Example #2
0
        protected void DrawRoomBackground(PolygonGrid2D polygon, Color color)
        {
            var polyPoints = polygon.GetPoints().Select(point => new Point(point.X, point.Y)).ToList();

            graphics.FillPolygon(new SolidBrush(color), polyPoints.ToArray());
            graphics.DrawPolygon(new Pen(color, 0.1f), polyPoints.ToArray());
        }
Example #3
0
        protected override void DrawRoom(PolygonGrid2D polygon, List <Tuple <Vector2Int, bool> > outline, float penWidth)
        {
            var polyPoints = polygon.GetPoints().Select(point => new Point(point.X, point.Y)).ToList();

            graphics.FillPolygon(Brushes.LightGray, polyPoints.ToArray());

            var lastPoint = outline[outline.Count - 1].Item1;
            var pen       = new Pen(Color.Black, penWidth)
            {
                EndCap   = LineCap.Flat,
                StartCap = LineCap.Flat
            };

            foreach (var pair in outline)
            {
                var point = pair.Item1;

                if (pair.Item2)
                {
                    graphics.DrawLine(pen, lastPoint.X, lastPoint.Y, point.X, point.Y);
                }

                lastPoint = point;
            }
        }
Example #4
0
        /// <inheritdoc />
        protected override void DrawRoom(PolygonGrid2D polygon, List <Tuple <Vector2Int, bool> > outline, float penWidth)
        {
            var polyPoints = polygon.GetPoints().Select(point => new Point(point.X, point.Y)).ToList();
            var offset     = polygon.BoundingRectangle.A;

            innerBrush.TranslateTransform(offset.X, offset.Y);
            graphics.FillPolygon(innerBrush, polyPoints.ToArray());
            innerBrush.ResetTransform();

            var lastPoint = outline[outline.Count - 1].Item1;

            foreach (var pair in outline)
            {
                var point = pair.Item1;

                if (pair.Item2)
                {
                    graphics.DrawLine(outlinePen, lastPoint.X, lastPoint.Y, point.X, point.Y);
                }

                lastPoint = point;
            }
        }
        private void DrawOutline(PolygonGrid2D polygon, List <OutlineSegment> outlineSegments)
        {
            var polyPoints = polygon.GetPoints().Select(point => new Point(point.X, point.Y)).ToList();
            var offset     = polygon.BoundingRectangle.A;

            innerBrush.TranslateTransform(offset.X, offset.Y);
            graphics.FillPolygon(new SolidBrush(Color.FromArgb(248, 248, 244)), polyPoints.ToArray());
            innerBrush.ResetTransform();

            var rectangles = partitioning.GetPartitions(polygon);
            var points     = new HashSet <Vector2Int>();

            foreach (var rectangle in rectangles)
            {
                for (int i = rectangle.A.X; i <= rectangle.B.X; i++)
                {
                    for (int j = rectangle.A.Y; j <= rectangle.B.Y; j++)
                    {
                        points.Add(new Vector2Int(i, j));
                    }
                }
            }

            var gridPen = new Pen(Color.FromArgb(100, 100, 100), scale * 0.50f);

            gridPen.DashStyle   = DashStyle.Dash;
            gridPen.DashPattern = new float[] { 1.2f / 10, 3.1f };
            gridPen.DashOffset  = 0.5f;

            //foreach (var point in points)
            //{
            //    var right = point + new Vector2Int(1, 0);
            //    var bottom = point + new Vector2Int(0, -1);

            //    if (points.Contains(right))
            //    {
            //        graphics.DrawLine(gridPen, point.X, point.Y, right.X, right.Y);
            //    }

            //    if (points.Contains(bottom))
            //    {
            //        graphics.DrawLine(gridPen, point.X, point.Y, bottom.X, bottom.Y);
            //    }
            //}

            for (var i = 0; i < outlineSegments.Count; i++)
            {
                var current  = outlineSegments[i];
                var previous = outlineSegments[Mod(i - 1, outlineSegments.Count)];
                var next     = outlineSegments[Mod(i + 1, outlineSegments.Count)];

                outlinePen.StartCap = previous.IsDoor ? LineCap.Square : LineCap.Round;
                outlinePen.EndCap   = next.IsDoor ? LineCap.Square : LineCap.Round;

                if (!current.IsDoor)
                {
                    var from = current.Line.From;
                    var to   = current.Line.To;

                    graphics.DrawLine(outlinePen, from.X, from.Y, to.X, to.Y);
                }
            }
        }
        /// <inheritdoc />
        public List <RectangleGrid2D> GetPartitions(PolygonGrid2D polygon)
        {
            IList <Vector2Int> points = polygon.GetPoints();

            var vertices = new List <Vertex>();

            var prev = points[points.Count - 3];
            var curr = points[points.Count - 2];
            var next = points[points.Count - 1];

            // Process vertices
            for (var i = 0; i < points.Count; i++)
            {
                prev = curr;
                curr = next;
                next = points[i];
                bool concave;

                if (prev.X == curr.X)
                {
                    if (curr.X == next.X)
                    {
                        throw new InvalidOperationException("Should not happen as polygons must be normalized");
                    }

                    var dir0 = prev.Y < curr.Y;
                    var dir1 = curr.X > next.X;

                    concave = dir0 == dir1;
                }
                else
                {
                    if (next.Y == curr.Y)
                    {
                        throw new InvalidOperationException("Should not happen as polygons must be normalized");
                    }

                    var dir0 = prev.X < curr.X;
                    var dir1 = curr.Y > next.Y;

                    concave = dir0 != dir1;
                }

                var vertex = new Vertex(curr, i - 1, concave);
                vertices.Add(vertex);
            }

            // Build interval trees for segments
            var horizontalSegments = new List <Segment>();
            var verticalSegments   = new List <Segment>();

            for (var i = 0; i < vertices.Count; i++)
            {
                var from = vertices[i];
                var to   = vertices[(i + 1) % vertices.Count];

                if (from.Point.X == to.Point.X)
                {
                    verticalSegments.Add(new Segment(from, to, false));
                }
                else
                {
                    horizontalSegments.Add(new Segment(from, to, true));
                }

                from.Next = to;
                to.Prev   = from;
            }

            var horizontalTree = new RangeTree <int, Segment>(horizontalSegments, new SegmentComparer());
            var verticalTree   = new RangeTree <int, Segment>(verticalSegments, new SegmentComparer());

            //Find horizontal and vertical diagonals
            var horizontalDiagonals = GetDiagonals(vertices, verticalTree, false);
            var verticalDiagonals   = GetDiagonals(vertices, horizontalTree, true);

            //Find all splitting edges
            var splitters = FindSplitters(horizontalDiagonals, verticalDiagonals);

            foreach (var splitter in splitters)
            {
                SplitSegment(splitter);
            }

            //Split all concave vertices
            SplitConvave(vertices);

            var rectangles = FindRegions(vertices);

            return(rectangles);
        }
Example #7
0
 public Polygon2D(PolygonGrid2D polygon)
 {
     points      = polygon.GetPoints().Select(x => (Vector2Int)x.ToUnityIntVector3()).ToList();
     gridPolygon = polygon;
 }
Example #8
0
        protected void DrawRoomBackground(PolygonGrid2D polygon)
        {
            var polyPoints = polygon.GetPoints().Select(point => new Point(point.X, point.Y)).ToList();

            graphics.FillPolygon(new SolidBrush(Color.FromArgb(248, 248, 244)), polyPoints.ToArray());
        }