User Tools

Site Tools


maxrects-lua

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
maxrects-lua [2021/02/13 07:57]
muragami [MaxRects.new(width,height,//canflip//)]
maxrects-lua [2021/02/13 10:21] (current)
muragami [:insertCollection(collect,sort)]
Line 76: Line 76:
   * **Array indexes 1, 2, 3, and 4** are X, Y, Width, and Height respectively. e.g. If you wanted to calculate the area of a rectangle: rect[3] * rect[4] is Width * Height.   * **Array indexes 1, 2, 3, and 4** are X, Y, Width, and Height respectively. e.g. If you wanted to calculate the area of a rectangle: rect[3] * rect[4] is Width * Height.
   * **The field 'data' is user assigned to any Lua value** and is there to contain information about what the rectangle represents. It is also valid for .data to be nil.   * **The field 'data' is user assigned to any Lua value** and is there to contain information about what the rectangle represents. It is also valid for .data to be nil.
 +  * **The field 'flipped' is set to true** if the rectangle was flipped to fit, assuming the algorithm allows that.
  
 +This format was chosen for ease of use, see these examples:
  
-===== Functions =====+<code lua> 
 +-- create a rectangle 
 +MyRect = { 0, 0, 32, 32 } -- as simple as that { X, Y, Width, Height } 
 +-- area of a rectangle 
 +Area = MyRect[3] * MyRect[4] -- Width x Height 
 +-- copy a rectangle 
 +MyOtherRect = MaxRects.copy(MyRect) -- shallow copy of just the 4 indexes and two fields 
 +</code> 
 + 
 +===== Functions (module) =====
  
 All the exposed functions of this module, with in-depth explanations. All the exposed functions of this module, with in-depth explanations.
Line 90: Line 101:
  
 Creates a new instance of a packing algorithm. This can be bypassed if you ever just need on instance, calling MaxRects:init(width,height,canflip) allows you to call later MaxRects:insert(), etc. The .new() is protected from changes you might make using MaxRects: calls and will always return a blank instance. Creates a new instance of a packing algorithm. This can be bypassed if you ever just need on instance, calling MaxRects:init(width,height,canflip) allows you to call later MaxRects:insert(), etc. The .new() is protected from changes you might make using MaxRects: calls and will always return a blank instance.
 +
 +==== MaxRects.copy(rect) ====
 +
 +  * **rect**: The rectangle to duplicate.
 +
 +  * **return**: A table containing the new copied rectangle.
 +
 +Creates a shallow copy of a rectangle. Does not duplicate .data for instance, but copies the reference. Explicitly only copies: [1], [2], [3], [4], .data, and .flip.
 +
 +===== Functions (instance) =====
 +
 +==== :reset(width,height,canflip) ====
 +
 +  * **//width//**: //optional// The width of the full packing rectangle.
 +  * **//height//**: //optional// The height of the full packing rectangle.
 +  * **//canflip//**: //optional// Boolean, default false. If true the algorithm can flip rectangles for better fit.
 +
 +Resets an instance of the algorithm to a blank state with the supplied parameters. If supplied with no parameters, resets the instance with the same parameters it was created with.
 +
 +==== :insert(width,height,data) ====
 +
 +  * **width**: The width of the rectangle to insert.
 +  * **height**: The height of the rectangle to insert.
 +  * **//data//**: //optional// Data to attach to the field .data of the rectangle.
 +
 +  * **return**: Boolean. True if the rectangle was inserted, False if it didn't fit.
 +
 +Inserts a rectangle into the collection, using the algorithm to assign a location. If the rectangle can't fit, returns false.
 +
 +==== :insertRect(rect) ====
 +
 +  * **rect**: The rectangle to insert. X/Y([0]/[1]) are ignored, only Width([3]) and Height([4]) count.
 +
 +  * **return**: Boolean. True if the rectangle was inserted, False if it didn't fit.
 +
 +Inserts a rectangle into the collection, using the algorithm to assign a location. If the rectangle can't fit, returns false. Copies .data into the stored node in the collection.
 +
 +==== :insertCollection(collect,sort) ====
 +
 +  * **collect**: A table array of rectangles to insert into the collection (see [[#:insertRect(rect)]])
 +  * **//sort//**: //optional// A method to sort the rectangles before insert.
 +
 +  * **return**: Boolean, Number. True if all were inserted, False if only some fit. The number is the amount added.
 +
 +Inserts rectangles into the collection, from an array table. If you define sort to one of: 'SortArea', 'SortShortSide', or 'SortLongSide' it will sort them from biggest to smallest using that method before inserting. If you pass anything else as sort, it'll throw an error. Note: If you pass a sort, then **table.sort() is applied to collect, so it's order might/will be changed by this function.**
 +==== :occupancy() ====
 +
 +  * **return**: Number from 0 to 1. This is a measure of how full the packing rectangle is. A percentage would be done like so: :occupancy() * 100.
 +
 +Reports how much of the packing rectangle has been filled. The algorithm does not store used space, so this is an O(N) call to iterate over all the contained rectangles. Though that might change in the future.
 +
 +==== :iterate(func,other) ====
 +
 +  * **func**: The function to call over each rectangle in the collection, as so: func(rect).
 +  * **//other//**: //optional// If supplied, passes that variable to the function called like so: func(other,rect).
 +
 +Iterates over all the rectangles in a collection using func(rect). If you supply other, that becomes func(other,rect). Using this option allows you do iterate with a class/table, like so:
 +
 +<code lua>
 +Packer:iterate(MyClass.func,MyClass)
 +
 +function MyClass:func(rect)
 + print("Got a rectangle!")
 +end
 +</code>
 +
 +==== :setAlgorithm(algo) ====
 +
 +  * **algo**: The algorithm to use for this instance. Defaults to 'BestShortSideFit'.
 +
 +Applies a given algorithm to the packer. The options are: 'BestShortSideFit', 'BestLongSideFit', 'BestAreaFit', and 'ContactPointRule'. Passing any other value will throw an error.
 +
 +==== :algorithmName() ====
 +
 +  * **return**: The algorithm in use for this instance. Defaults to 'BestShortSideFit'.
 +
 +Returns the algorithm in use by the packer. The options are: 'BestShortSideFit', 'BestLongSideFit', 'BestAreaFit', and 'ContactPointRule'.
 +
 +===== Simple LÖVE Texture Packer =====
 +
 +TODO!
maxrects-lua.1613231863.txt.gz · Last modified: 2021/02/13 07:57 by muragami