Click here to Skip to main content
12,549,003 members (48,884 online)
Click here to Skip to main content


50 bookmarked

Fast Optimizing Rectangle Packing Algorithm for Building CSS Sprites

, 14 Jun 2011 CPOL
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;

namespace Mapper
    /// <summary>
    /// Statistics gathered by a canvas.
    /// </summary>
    public interface ICanvasStats
        /// <summary>
        /// Number of times an attempt was made to add an image to the canvas used by the mapper.
        /// </summary>
        int RectangleAddAttempts { get; set; }

        /// <summary>
        /// Number of cells generated by the canvas.
        /// </summary>
        int NbrCellsGenerated { get; set; }

        /// <summary>
        /// Each time a rectangle cannot be added because the canvas is not high enough,
        /// there is "free height deficit". The lowest of these free height deficits is recorded here.
        /// This value is important, because if you try to put the same rectangles in a canvas
        /// whose height has not been increased by at 
        /// least this Lowest Missing Height, all rectangles will be placed in the exact same
        /// locations and you'll get the same outcome.
        /// If there was never a failure to place a rectangle due to free height deficit, this will be Int32.MaxValue.
        /// </summary>
        int LowestFreeHeightDeficit { get; set; }

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.


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


About the Author

Matt Perdeck
Australia Australia
Twitter: @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 ( 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 and

You may also be interested in...

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