Example #1
0
        private static void GeneratePicture(string filename, MaxRectsBinPack result)
        {
            int stretch = 3;
            Bitmap bmp = new Bitmap(result.binWidth * stretch, result.binHeight * stretch);
            int count = 0;
            foreach (var rect in result.UsedBoxes)
            {
                Color col = ColorTranslator.FromHtml(Extensions.ColourValues[count]);
                using (Graphics gfx = Graphics.FromImage(bmp))
                using (SolidBrush brush = new SolidBrush(col))
                {
                    gfx.FillRectangle(brush, rect.x * stretch, rect.y * stretch, rect.width * stretch, rect.height * stretch);
                }

                count = count == Extensions.ColourValues.Length-1 ? 0 : count +1 ;
            }
            bmp.Save(filename);
        }
Example #2
0
 private static IEnumerable<MaxRectsBinPack> GetResults(List<Rect> boxList, int specifiedHeight = 0)
 {
     var comparerList = new List<IComparer<Rect>>() {new HeightComparer(), new AreaComparer(), new WidthComparer()};
     foreach (var comparer in comparerList)
     foreach (var method in Extensions.GetValues<FreeRectChoiceHeuristic>())
     {
         bool finished = false;
         long minArea = boxList.Aggregate<Rect, long>(0, (current, t) => current + t.width * t.height);
         int height = specifiedHeight > 0 ? specifiedHeight : (int)Math.Sqrt( minArea);
         int width = specifiedHeight > 0 ? (int)(minArea / height) : (int)Math.Sqrt(minArea);
         MaxRectsBinPack bin = null;
         while (!finished)
         {
             width++;
             if (specifiedHeight == 0)
                 height++;
             bin = new MaxRectsBinPack(width, height, method, comparer);
             finished = bin.Insert(boxList.Select(x => new Rect(x)).ToList());
         }
         yield return bin;
     }
 }
Example #3
0
 private static void WriteOutputFile(string filename, MaxRectsBinPack result)
 {
     using (var file = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))
     using (var writer = new StreamWriter(file))
     {
         result.UsedBoxes.ForEach(x => writer.WriteLine(x.ToString()));
     }
 }