Table of Contents

Class MaxRectsBin

Namespace
Microsoft.Xna.Framework.Content.Pipeline.Graphics
Assembly
MonoGame.Framework.Content.Pipeline.dll

A bin that can pack rectangles using the MaxRects algorithm with several heuristics.

public class MaxRectsBin
Inheritance
MaxRectsBin
Inherited Members

Constructors

MaxRectsBin(int, int, GrowRule, float, bool)

Create a MaxRectsBin instance.

public MaxRectsBin(int width = 64, int height = 64, GrowRule grow = GrowRule.Both, float growIncrement = -2, bool allowRotation = false)

Parameters

width int

Initial available width. Defaults to 64.

height int

Initial available height. Defaults to 64.

grow GrowRule

Whether to allow growing the available space when a rectangle doesn't fit.

growIncrement float

Determines how the bin is grown. A positive value means a fixed size increase, a negative value means a factor increase. E.g. -2 will grow the bin by a factor of 2 when it needs to grow.

allowRotation bool

Whether to allow rotating the rectangles.

Properties

AllowRotations

Indicates if rectangle can be rotated.

public bool AllowRotations { get; }

Property Value

bool

BinHeight

Height of the bin.

public int BinHeight { get; }

Property Value

int

BinWidth

Width of the bin.

public int BinWidth { get; }

Property Value

int

Grow

Rule to choose in which dimension to grow.

public GrowRule Grow { get; set; }

Property Value

GrowRule

GrowIncrement

Determines how the bin is grown. A positive value means a fixed size increase, a negative value means a factor increase. E.g. -2 will grow the bin by a factor of 2 when it needs to grow.

public float GrowIncrement { get; set; }

Property Value

float

GrowLimit

The number of times to grow the bin for a single rectangle when it doesn't fit. Defaults to 3.

public int GrowLimit { get; set; }

Property Value

int

IncludePadding

Determines if padding is included in returned rectangles. Default is false.

public bool IncludePadding { get; set; }

Property Value

bool

PaddingHeight

Padding to add to both sides in the vertical dimension. Default is 0.

public int PaddingHeight { get; set; }

Property Value

int

PaddingWidth

Padding to add to both sides in the horizontal dimension. Default is 0.

public int PaddingWidth { get; set; }

Property Value

int

UsedHeight

The actually used height of the bin.

public int UsedHeight { get; }

Property Value

int

UsedWidth

The actually used width of the bin.

public int UsedWidth { get; }

Property Value

int

Methods

GetOccupancy(bool)

Computes the ratio of used surface area.

public float GetOccupancy(bool crop = false)

Parameters

crop bool

If true this uses UsedWidth and UsedHeight instead of BinWidth and BinHeight for available area.

Returns

float

Insert(Point[], MaxRectsHeuristic)

Place an array of rectangles in the bin.

public Rectangle[] Insert(Point[] sizes, MaxRectsHeuristic heuristic = MaxRectsHeuristic.Bssf)

Parameters

sizes Point[]

Sizes of the rectangles.

heuristic MaxRectsHeuristic

Heuristic to use.

Returns

Rectangle[]

Bounds of all rectangles after placement.

Exceptions

ArgumentNullException

If sizes is null.

Insert(Point[], Rectangle[], int, MaxRectsHeuristic)

Place an array of rectangles in the bin.

public void Insert(Point[] sizes, Rectangle[] bounds, int indexOffset = 0, MaxRectsHeuristic heuristic = MaxRectsHeuristic.Bssf)

Parameters

sizes Point[]

Sizes of the rectangles.

bounds Rectangle[]

Array to fill with rectangle bounds.

indexOffset int

Index into bounds to start filling.

heuristic MaxRectsHeuristic

Heuristic to use.

Exceptions

ArgumentNullException

If sizes or bounds is null.

ArgumentException

If indexOffset is less than 0.

ArgumentOutOfRangeException

If bounds is not large enough to hold all bounds. I.e. bounds.Length - indexOffset < sizes.Length

Insert(int, int, MaxRectsHeuristic)

Insert a rectangle into the bin.

public Rectangle Insert(int width, int height, MaxRectsHeuristic heuristic)

Parameters

width int

Width of the rectangle.

height int

Height of the rectangle.

heuristic MaxRectsHeuristic

Heuristic to use.

Returns

Rectangle

The bounds of the rectangle after placement.

Exceptions

Exception

If the rectangle did not fit in the bin.