Example #1
0
        public void GoldenSectionTest()
        {
            const double Precision = 1e-2;
            double       xopt      = OptimalRectanglePacking <int> .GoldenSectionSearch(x => x *x, -1, -0.2, 1, Precision);

            Assert.AreEqual(0, xopt, Precision, "GoldenSectionSearch didn't find correct root for f(x)=x^2");
        }
 public void RectanglePackingTwoSquares()
 {
     List<RectangleToPack<int>> rectangles = new List<RectangleToPack<int>>();
     for (int i = 0; i < 2; ++i)
     {
         rectangles.Add(new RectangleToPack<int>(new Rectangle(new Point(0, 0), new Point(1, 1)), i));
     }
     var rectanglePacking = new OptimalRectanglePacking<int>(rectangles, PackingConstants.GoldenRatio);
     rectanglePacking.Run();
     Assert.AreEqual(2, rectanglePacking.PackedWidth, "packing is wrong width");
     Assert.AreEqual(1, rectanglePacking.PackedHeight, "packing is wrong height");
     Assert.IsFalse(IsOverlapping(rectangles), "There are overlaps between the packed rectangles");
 }
Example #3
0
        public void RectanglePackingTwoSquares()
        {
            List <RectangleToPack <int> > rectangles = new List <RectangleToPack <int> >();

            for (int i = 0; i < 2; ++i)
            {
                rectangles.Add(new RectangleToPack <int>(new Rectangle(new Point(0, 0), new Point(1, 1)), i));
            }
            var rectanglePacking = new OptimalRectanglePacking <int>(rectangles, PackingConstants.GoldenRatio);

            rectanglePacking.Run();
            Assert.AreEqual(2, rectanglePacking.PackedWidth, "packing is wrong width");
            Assert.AreEqual(1, rectanglePacking.PackedHeight, "packing is wrong height");
            Assert.IsFalse(IsOverlapping(rectangles), "There are overlaps between the packed rectangles");
        }
Example #4
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");
        }