Click here to Skip to main content
11,496,146 members (643 online)
Click here to Skip to main content
Add your own
alternative version

Fast Optimizing Rectangle Packing Algorithm for Building CSS Sprites

, 14 Jun 2011 CPOL 92.8K 3.8K 46
Describes a fast algorithm to pack a series of rectangles of varying widths and heights into an enclosing rectangle of minimum size
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Mapper;

/// <summary>
/// Summary description for SingleTestStats
/// </summary>
public class SingleTestStats : ISingleTestStats
{
    public enum StatisticType
    {
        // Use underscores, so when using these names as chart titles, the underscores can be replaced by spaces.
        Nbr_Images,
        Width_Standard_Deviation,
        Heigth_Standard_Deviation,
        Area_Standard_Deviation,
        Aspect_Ratio_Standard_Deviation
    }

    public Type MapperType { get; set; }
    public int TotalAreaAllImages { get; set; }
    public ISprite FinalSprite { get; set; }

    public int CandidateSpriteFails { get; set; }
    public int CandidateSpritesGenerated { get; set; }
    public int CanvasRectangleAddAttempts { get; set; }
    public int CanvasNbrCellsGenerated { get; set; }

    public float Efficiency { get; set; }
    public long ElapsedTicks { get; set; }

    public double[] ImageStats { get; set; }

    /// <summary>
    /// Returns the name of the type of a mapper in a friendly form
    /// </summary>
    public static string FriendlyMapperTypeName(Type mapperType)
    {
        string unfriendlyMapperTypeName = mapperType.ToString();
        string friendlyMapperTypeName = unfriendlyMapperTypeName.Replace("Mapper.", "").Replace("`1[SpriteInfo]", "");
        return friendlyMapperTypeName;
    }

    /// <summary>
    /// Returns the names of the fields holding observed test results
    /// </summary>
    /// <returns></returns>
    public static string[] ResultNames()
    {
        return new string[] { "Efficiency", "ElapsedTicks" };
    }

    /// <summary>
    /// Returns a header line for a CSV file, containing all field names in a SingleTestStats object.
    /// </summary>
    public static string CsvHeader()
    {
        string result =
            "MapperType, " +
            "TotalAreaAllImages, " +
            "SpriteWidth, " +
            "SpriteHeight, " +
            "SpriteArea, " +
            "CandidateSpriteFails, " +
            "CandidateSpritesGenerated, " +
            "CanvasRectangleAddAttempts, " +
            "CanvasNbrCellsGenerated, " +
            "Efficiency, " +
            "ElapsedTicks";

        foreach (string statisticTypeName in Enum.GetNames(typeof(StatisticType)))
        {
            result += ", " + statisticTypeName;
        }

        return result;
    }

    /// <summary>
    /// Returns a data line for use in a CSV file based on the info in this object.
    /// </summary>
    public string CsvDataLine()
    {
        string result = 
            string.Format(
                "{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}",
                FriendlyMapperTypeName(MapperType), TotalAreaAllImages, FinalSprite.Width, FinalSprite.Height,
                FinalSprite.Area,
                CandidateSpriteFails,
                CandidateSpritesGenerated,
                CanvasRectangleAddAttempts, 
                CanvasNbrCellsGenerated, 
                Efficiency, ElapsedTicks);

        foreach (int statisticTypeIdx in Enum.GetValues(typeof(StatisticType)))
        {
            result += ", " + ImageStats[statisticTypeIdx].ToString();
        }

        return result;
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Matt Perdeck
Web Developer
Australia Australia
Twitter: @MattPerdeck
Blog: mattperdeck.com
Current project: JSNLog JavaScript Logging Package

Matt has over 6 years .NET and SQL Server development experience. Before getting into .Net, he worked on a number of systems, ranging from the largest ATM network in The Netherlands to embedded software in advanced Wide Area Networks and the largest ticketing web site in Australia. He has lived and worked in Australia, The Netherlands, Slovakia and Thailand.

He recently wrote a book, ASP.NET Performance Secrets (www.packtpub.com/asp-net-site-performance-secrets/book) in which he shows in clear and practical terms how to quickly find the biggest bottlenecks holding back the performance of your web site, and how to then remove those bottlenecks. The book deals with all environments affecting a web site - the web server, the database server and the browser.

Matt currently lives in Sydney, Australia. After 2 years at Readify, he now works at the global professional services company PwC. His current contract ends at 29 June 2014.
Follow on   Twitter   Google+

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 14 Jun 2011
Article Copyright 2011 by Matt Perdeck
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid