Nuclex.Support/Source/Packing/RectanglePacker.cs
Markus Ewald d09bb30cec Added a small rectangle packing library for optimally arranging smaller rectangles within one larger rectangle
git-svn-id: file:///srv/devel/repo-conversion/nusu@19 d2e56fa2-650e-0410-a79f-9358c0239efd
2007-05-16 20:28:23 +00:00

89 lines
3.5 KiB
C#

#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2007 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
namespace Nuclex.Support.Packing {
/// <summary>Base class for rectangle packing algorithms</summary>
/// <remarks>
/// <para>
/// By uniting all rectangle packers under this common base class, you can
/// easily switch between different algorithms to find the most efficient or
/// performant one for a given job.
/// </para>
/// <para>
/// An almost exhaustive list of rectangle packers can be found here:
/// http://www.csc.liv.ac.uk/~epa/surveyhtml.html
/// </para>
/// </remarks>
public abstract class RectanglePacker {
/// <summary>Initializes a new rectangle packer</summary>
/// <param name="maxPackingAreaWidth">Maximum width of the packing area</param>
/// <param name="maxPackingAreaHeight">Maximum height of the packing area</param>
protected RectanglePacker(int maxPackingAreaWidth, int maxPackingAreaHeight) {
this.maxPackingAreaWidth = maxPackingAreaWidth;
this.maxPackingAreaHeight = maxPackingAreaHeight;
}
/// <summary>Allocates space for a rectangle in the packing area</summary>
/// <param name="rectangleWidth">Width of the rectangle to allocate</param>
/// <param name="rectangleHeight">Height of the rectangle to allocate</param>
/// <returns>The location at which the rectangle has been placed</returns>
public virtual Point Allocate(int rectangleWidth, int rectangleHeight) {
Point point;
if(!TryAllocate(rectangleWidth, rectangleHeight, out point))
throw new Exception("Rectangle does not fit in packing area");
return point;
}
/// <summary>Tries to allocate space for a rectangle in the packing area</summary>
/// <param name="rectangleWidth">Width of the rectangle to allocate</param>
/// <param name="rectangleHeight">Height of the rectangle to allocate</param>
/// <param name="placement">Output parameter receiving the rectangle's placement</param>
/// <returns>True if space for the rectangle could be allocated</returns>
public abstract bool TryAllocate(
int rectangleWidth, int rectangleHeight, out Point placement
);
/// <summary>Maximum width the packing area is allowed to have</summary>
protected int MaxPackingAreaWidth {
get { return this.maxPackingAreaWidth; }
}
/// <summary>Maximum height the packing area is allowed to have</summary>
protected int MaxPackingAreaHeight {
get { return this.maxPackingAreaHeight; }
}
/// <summary>Maximum allowed width of the packing area</summary>
private int maxPackingAreaWidth;
/// <summary>Maximum allowed height of the packing area</summary>
private int maxPackingAreaHeight;
}
} // namespace Nuclex.Support.Packing