Click here to Skip to main content
15,892,059 members
Articles / Web Development / ASP.NET

Fast Optimizing Rectangle Packing Algorithm for Building CSS Sprites

Rate me:
Please Sign up or sign in to vote.
4.90/5 (46 votes)
14 Jun 2011CPOL21 min read 312.4K   7.9K   60  
Describes a fast algorithm to pack a series of rectangles of varying widths and heights into an enclosing rectangle of minimum size
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using Mapper;

/// <summary>
/// Represents the contents of a sprite image.
/// </summary>
public class Sprite : ISprite
{
    private List<IMappedImageInfo> _mappedImages = null;
    private int _width = 0;
    private int _height = 0;

    /// <summary>
    /// Holds the locations of all the individual images within the sprite image.
    /// </summary>
    public List<IMappedImageInfo> MappedImages { get { return _mappedImages; } }

    /// <summary>
    /// Width of the sprite image
    /// </summary>
    public int Width { get { return _width; } }

    /// <summary>
    /// Height of the sprite image
    /// </summary>
    public int Height { get { return _height; } }

    /// <summary>
    /// Area of the sprite image
    /// </summary>
    public int Area { get { return _width * _height; } }

    public Sprite()
    {
        _mappedImages = new List<IMappedImageInfo>();
        _width = 0;
        _height = 0;
    }

    /// <summary>
    /// Adds a Rectangle to the SpriteInfo, and updates the width and height of the SpriteInfo.
    /// </summary>
    /// <param name="imageLocation"></param>
    public void AddMappedImage(IMappedImageInfo imageLocation)
    {
        _mappedImages.Add(imageLocation);

        IImageInfo newImage = imageLocation.ImageInfo;

        int highestY = imageLocation.Y + newImage.Height;
        int rightMostX = imageLocation.X + newImage.Width;

        if (_height < highestY) { _height = highestY; }
        if (_width < rightMostX) { _width = rightMostX; }
    }

}

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)


Written By
Architect
Australia Australia
Twitter: @MattPerdeck
LinkedIn: au.linkedin.com/in/mattperdeck
Current project: JSNLog JavaScript Logging Package

Matt has over 9 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 is the author of the book ASP.NET Performance Secrets (www.amazon.com/ASP-NET-Site-Performance-Secrets-Perdeck/dp/1849690685) 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. He recently worked at Readify and the global professional services company PwC. He now works at SP Health, a global provider of weight loss web sites such at CSIRO's TotalWellBeingDiet.com and BiggestLoserClub.com.

Comments and Discussions