Пример #1
0
        public void SimpleRectanglesDifferentHeights()
        {
            List <RectangleToPack <int> > rectangles = new List <RectangleToPack <int> >();

            rectangles.Add(new RectangleToPack <int>(new Rectangle(new Point(), new Point(1, 3)), 0));
            rectangles.Add(new RectangleToPack <int>(new Rectangle(new Point(), new Point(1, 2)), 1));
            rectangles.Add(new RectangleToPack <int>(new Rectangle(new Point(), new Point(1, 2)), 2));
            rectangles.Add(new RectangleToPack <int>(new Rectangle(new Point(), new Point(1, 1)), 3));
            rectangles.Add(new RectangleToPack <int>(new Rectangle(new Point(), new Point(1, 1)), 4));
            const double Scale = 100;

            foreach (var r in rectangles)
            {
                r.Rectangle = new Rectangle(new Point(), new Point(r.Rectangle.Width * Scale, r.Rectangle.Height * Scale));
            }

            // shuffle
            rectangles = rectangles.OrderBy(x => Guid.NewGuid()).ToList();

            RectanglePacking <int> rectanglePacking = new RectanglePacking <int>(rectangles, 3 * Scale);

            rectanglePacking.Run();
            Assert.AreEqual(3 * Scale, rectanglePacking.PackedWidth, "packing is wrong width");
            Assert.AreEqual(3 * Scale, rectanglePacking.PackedHeight, "packing is wrong height");
            Assert.IsFalse(IsOverlapping(rectangles), "There are overlaps between the packed rectangles");
            ShowDebugView(rectangles);
        }
 public void RectanglePackingTallRectAndTwoSquares()
 {
     List<RectangleToPack<int>> rectangles = new List<RectangleToPack<int>>();
     rectangles.Add(new RectangleToPack<int>(new Rectangle(new Point(0, 0), new Point(1, 2)), 0));
     rectangles.Add(new RectangleToPack<int>(new Rectangle(new Point(0, 0), new Point(1, 1)), 1));
     rectangles.Add(new RectangleToPack<int>(new Rectangle(new Point(0, 0), new Point(1, 1)), 2));
     RectanglePacking<int> rectanglePacking = new RectanglePacking<int>(rectangles, 2.0);
     rectanglePacking.Run();
     Assert.AreEqual(2, rectanglePacking.PackedWidth, "packing is wrong width");
     Assert.AreEqual(2, rectanglePacking.PackedHeight, "packing is wrong height");
     Assert.IsFalse(IsOverlapping(rectangles), "There are overlaps between the packed rectangles");
 }
 public void RectanglePackingNineSquares()
 {
     List<RectangleToPack<int>> rectangles = new List<RectangleToPack<int>>();
     for (int i = 0; i < 9; ++i)
     {
         rectangles.Add(new RectangleToPack<int>(new Rectangle(new Point(0, 0), new Point(1, 1)), i));
     }
     RectanglePacking<int> rectanglePacking = new RectanglePacking<int>(rectangles, 3.0);
     rectanglePacking.Run();
     Assert.AreEqual(3, rectanglePacking.PackedWidth, "packing is wrong width");
     Assert.AreEqual(3, rectanglePacking.PackedHeight, "packing is wrong height");
     Assert.IsFalse(IsOverlapping(rectangles), "There are overlaps between the packed rectangles");
 }
Пример #4
0
        public void RectanglePackingTallRectAndTwoSquares()
        {
            List <RectangleToPack <int> > rectangles = new List <RectangleToPack <int> >();

            rectangles.Add(new RectangleToPack <int>(new Rectangle(new Point(0, 0), new Point(1, 2)), 0));
            rectangles.Add(new RectangleToPack <int>(new Rectangle(new Point(0, 0), new Point(1, 1)), 1));
            rectangles.Add(new RectangleToPack <int>(new Rectangle(new Point(0, 0), new Point(1, 1)), 2));
            RectanglePacking <int> rectanglePacking = new RectanglePacking <int>(rectangles, 2.0);

            rectanglePacking.Run();
            Assert.AreEqual(2, rectanglePacking.PackedWidth, "packing is wrong width");
            Assert.AreEqual(2, rectanglePacking.PackedHeight, "packing is wrong height");
            Assert.IsFalse(IsOverlapping(rectangles), "There are overlaps between the packed rectangles");
        }
Пример #5
0
        public void RectanglePackingNineSquares()
        {
            List <RectangleToPack <int> > rectangles = new List <RectangleToPack <int> >();

            for (int i = 0; i < 9; ++i)
            {
                rectangles.Add(new RectangleToPack <int>(new Rectangle(new Point(0, 0), new Point(1, 1)), i));
            }
            RectanglePacking <int> rectanglePacking = new RectanglePacking <int>(rectangles, 3.0);

            rectanglePacking.Run();
            Assert.AreEqual(3, rectanglePacking.PackedWidth, "packing is wrong width");
            Assert.AreEqual(3, rectanglePacking.PackedHeight, "packing is wrong height");
            Assert.IsFalse(IsOverlapping(rectangles), "There are overlaps between the packed rectangles");
        }
Пример #6
0
        public void PowerLawRandomRectangles()
        {
            const int N    = 100;
            Random    rand = new Random(0);
            double    desiredAspectRatio             = 1;
            List <RectangleToPack <int> > rectangles = new List <RectangleToPack <int> >();
            double       area  = 0;
            const double Scale = 100;

            for (int i = 0; i < N; ++i)
            {
                double s      = Scale * Math.Pow(2, i / 10.0);
                double width  = s * rand.NextDouble();
                double height = s * rand.NextDouble();
                area += width * height;
                rectangles.Add(new RectangleToPack <int>(new Rectangle(new Point(0, 0), new Point(width, height)), i));
            }

            double maxWidth = Math.Sqrt(area);
            RectanglePacking <int> rectanglePacking = new RectanglePacking <int>(rectangles, maxWidth);

            rectanglePacking.Run();
            ShowDebugView(rectangles);
            double appoxAspectRatio = rectanglePacking.PackedWidth / rectanglePacking.PackedHeight;

            Assert.IsTrue(rectanglePacking.PackedWidth < maxWidth, "Packing is wider than the max width we specified");
            Assert.IsFalse(IsOverlapping(rectangles), "There are overlaps between the packed rectangles");

            OptimalRectanglePacking <int> optimalRectanglePacking = new OptimalRectanglePacking <int>(rectangles, desiredAspectRatio);

            optimalRectanglePacking.Run();
            double optimalAspectRatio = optimalRectanglePacking.PackedWidth / optimalRectanglePacking.PackedHeight;

            ShowDebugView(rectangles);

            Assert.IsTrue(Math.Abs(appoxAspectRatio - desiredAspectRatio) > Math.Abs(optimalAspectRatio - desiredAspectRatio), "aspect ratio calculated by OptimalRectanglePacking was not better than regular RectanglePacking");

            Assert.IsFalse(IsOverlapping(rectangles), "There are overlaps between the packed rectangles");
        }
        public void PowerLawRandomRectangles()
        {
            const int N = 100;
            Random rand = new Random(0);
            double desiredAspectRatio = 1;
            List<RectangleToPack<int>> rectangles = new List<RectangleToPack<int>>();
            double area = 0;
            const double Scale = 100;
            for (int i = 0; i < N; ++i)
            {
                double s = Scale * Math.Pow(2, i / 10.0);
                double width = s * rand.NextDouble();
                double height = s * rand.NextDouble();
                area += width * height;
                rectangles.Add(new RectangleToPack<int>(new Rectangle(new Point(0, 0), new Point(width, height)), i));
            }

            double maxWidth = Math.Sqrt(area);
            RectanglePacking<int> rectanglePacking = new RectanglePacking<int>(rectangles, maxWidth);
            rectanglePacking.Run();
            ShowDebugView(rectangles);
            double appoxAspectRatio = rectanglePacking.PackedWidth / rectanglePacking.PackedHeight;
            Assert.IsTrue(rectanglePacking.PackedWidth < maxWidth, "Packing is wider than the max width we specified");
            Assert.IsFalse(IsOverlapping(rectangles), "There are overlaps between the packed rectangles");

            OptimalRectanglePacking<int> optimalRectanglePacking = new OptimalRectanglePacking<int>(rectangles, desiredAspectRatio);
            optimalRectanglePacking.Run();
            double optimalAspectRatio = optimalRectanglePacking.PackedWidth / optimalRectanglePacking.PackedHeight;
            ShowDebugView(rectangles);

            Assert.IsTrue(Math.Abs(appoxAspectRatio - desiredAspectRatio) > Math.Abs(optimalAspectRatio - desiredAspectRatio), "aspect ratio calculated by OptimalRectanglePacking was not better than regular RectanglePacking");

            Assert.IsFalse(IsOverlapping(rectangles), "There are overlaps between the packed rectangles");
        }
        public void SimpleRectanglesDifferentHeights()
        {
            List<RectangleToPack<int>> rectangles = new List<RectangleToPack<int>>();
            rectangles.Add(new RectangleToPack<int>(new Rectangle(new Point(), new Point(1, 3)), 0));
            rectangles.Add(new RectangleToPack<int>(new Rectangle(new Point(), new Point(1, 2)), 1));
            rectangles.Add(new RectangleToPack<int>(new Rectangle(new Point(), new Point(1, 2)), 2));
            rectangles.Add(new RectangleToPack<int>(new Rectangle(new Point(), new Point(1, 1)), 3));
            rectangles.Add(new RectangleToPack<int>(new Rectangle(new Point(), new Point(1, 1)), 4));
            const double Scale = 100;
            foreach (var r in rectangles)
            {
                r.Rectangle = new Rectangle(new Point(), new Point(r.Rectangle.Width * Scale, r.Rectangle.Height * Scale));
            }

            // shuffle
            rectangles = rectangles.OrderBy(x => Guid.NewGuid()).ToList();

            RectanglePacking<int> rectanglePacking = new RectanglePacking<int>(rectangles, 3 * Scale);
            rectanglePacking.Run();
            Assert.AreEqual(3 * Scale, rectanglePacking.PackedWidth, "packing is wrong width");
            Assert.AreEqual(3 * Scale, rectanglePacking.PackedHeight, "packing is wrong height");
            Assert.IsFalse(IsOverlapping(rectangles), "There are overlaps between the packed rectangles");
            ShowDebugView(rectangles);
        }