Ejemplo n.º 1
0
 public bool Equals(Box other)
 {
     if (ReferenceEquals(null, other)) return false;
     if (ReferenceEquals(this, other)) return true;
     return other.x1 == x1 && other.y1 == y1 && other.x2 == x2 && other.y2 == y2;
 }
Ejemplo n.º 2
0
 private void AddToCurrentBox(Box box, int x1, int y1, int x2, int y2)
 {
     currentBox = currentBox == null ? new Box(x1, y1, x2, y2) : new Box(currentBox.X1, currentBox.Y1, x2, y2);
 }
Ejemplo n.º 3
0
 private void TerminateBox(Box box, List<Box> allMergedBoxes)
 {
     if (currentBox == null) return;
     allMergedBoxes.Add(currentBox);
     currentBox = null;
 }
Ejemplo n.º 4
0
        public void ArbitraryStuff()
        {
            var boundingBox = new Box(0, 0, 500, 500);
            var picture = new Box(60, 60, 100, 100);
            var picture2 = new Box(300, 80, 400, 200);
            var picture3 = new Box(10, 130, 100, 200);

            var pictures = new List<Box> {picture, picture2, picture3};
            var horizontalLines = new List<HorizontalLine>{ new HorizontalLine(boundingBox.Y1), new HorizontalLine(boundingBox.Y2)};
            var verticalLines = new List<VerticalLine>{new VerticalLine(boundingBox.X1), new VerticalLine(boundingBox.X2)};
            pictures.ForEach(box =>
                                 {
                                     horizontalLines.Add(new HorizontalLine(box.Y1));
                                     horizontalLines.Add(new HorizontalLine(box.Y2));
                                     verticalLines.Add(new VerticalLine(box.X1));
                                     verticalLines.Add(new VerticalLine(box.X2));
                                 });

            horizontalLines.Sort((line1,line2) =>
                                     {
                                         if (line1.Y == line2.Y) return 0;
                                         if (line1.Y < line2.Y) return -1;
                                         return 1;
                                     });
            verticalLines.Sort((line1,line2) =>
                                     {
                                         if (line1.X == line2.X) return 0;
                                         if (line1.X < line2.X) return -1;
                                         return 1;
                                     });

            var allMergedBoxes = new List<Box>();
            foreach (var horizontalLine in horizontalLines)
            {
                if (horizontalLines.Last() == horizontalLine) break;
                var top = horizontalLine;
                var bottom = horizontalLines[horizontalLines.IndexOf(horizontalLine) + 1];
                currentBox = null;

                foreach (var verticalLine in verticalLines)
                {
                    if (verticalLines.Last() == verticalLine)
                    {
                        TerminateBox(currentBox, allMergedBoxes);
                        break;
                    }
                    var left = verticalLine;
                    var right = verticalLines[verticalLines.IndexOf(verticalLine) + 1];
                    if (new Box(left.X, top.Y, right.X, bottom.Y).OverlapsWith(pictures))
                    {
                        TerminateBox(currentBox, allMergedBoxes);
                        continue;
                    }
                    AddToCurrentBox(currentBox, left.X, top.Y, right.X, bottom.Y);
                }
            }

            allMergedBoxes.ForEach(box => Console.Out.WriteLine(box));
        }