maxrects-packer / Rectangle
Class: Rectangle
Defined in: geom/Rectangle.ts:116
A rect the packer has placed: an IRectangleInput plus the x/y the packer wrote onto it.
Implements
Constructors
Constructor
new Rectangle(
width?,height?,x?,y?,rot?,allowRotation?):Rectangle
Defined in: geom/Rectangle.ts:133
Creates an instance of Rectangle.
Parameters
width?
number = 0
width of the rectangle (default is 0)
height?
number = 0
height of the rectangle (default is 0)
x?
number = 0
x position of the rectangle (default is 0)
y?
number = 0
y position of the rectangle (default is 0)
rot?
boolean = false
rotation flag (default is false)
allowRotation?
boolean | undefined
allow rotation flag (default is undefined)
Returns
Rectangle
Properties
_allowRotation
protected_allowRotation:boolean|undefined=undefined
Defined in: geom/Rectangle.ts:446
_data
protected_data:any
Defined in: geom/Rectangle.ts:465
_defaultData
protected_defaultData:any
Defined in: geom/Rectangle.ts:474
The {} the constructor installed as _data, held so toJSON() can recognise that very object.
A flag cleared by the setter cannot do this job: a subclass may assign the protected _data field directly, which leaves the flag set while the payload is already the subclass's. An object the caller wrote is shape-identical to the placeholder, so identity is the only thing that tells them apart.
_dirty
protected_dirty:number=0
Defined in: geom/Rectangle.ts:493
_height
protected_height:number
Defined in: geom/Rectangle.ts:390
_rot
protected_rot:boolean=false
Defined in: geom/Rectangle.ts:420
_width
protected_width:number
Defined in: geom/Rectangle.ts:380
_x
protected_x:number
Defined in: geom/Rectangle.ts:400
_y
protected_y:number
Defined in: geom/Rectangle.ts:410
oversized
oversized:
boolean=false
Defined in: geom/Rectangle.ts:121
Oversized tag on a rect the packer could not put in a bin: one bigger than the packer itself, or one no bin could grow to hold (square caps a bin below maxHeight, say).
Accessors
allowRotation
Get Signature
get allowRotation():
boolean|undefined
Defined in: geom/Rectangle.ts:451
If the rectangle allow rotation
Returns
boolean | undefined
Set Signature
set allowRotation(
value):void
Defined in: geom/Rectangle.ts:458
Set the allowRotation tag of the rectangle.
Parameters
value
boolean | undefined
Returns
void
data
Get Signature
get data():
any
Defined in: geom/Rectangle.ts:476
Opaque caller payload, including explicit null. Objects may also supply allowRotation.
Returns
any
Set Signature
set data(
value):void
Defined in: geom/Rectangle.ts:479
Parameters
value
any
Returns
void
dirty
Get Signature
get dirty():
boolean
Defined in: geom/Rectangle.ts:494
Returns
boolean
height
Get Signature
get height():
number
Defined in: geom/Rectangle.ts:391
Returns
number
Set Signature
set height(
value):void
Defined in: geom/Rectangle.ts:394
Parameters
value
number
Returns
void
Implementation of
rot
Get Signature
get rot():
boolean
Defined in: geom/Rectangle.ts:425
If the rectangle is rotated
Returns
boolean
Set Signature
set rot(
value):void
Defined in: geom/Rectangle.ts:434
Set the rotate tag of the rectangle.
note: after rot is set, width/height of this rectangle is swaped.
Parameters
value
boolean
Returns
void
width
Get Signature
get width():
number
Defined in: geom/Rectangle.ts:381
Returns
number
Set Signature
set width(
value):void
Defined in: geom/Rectangle.ts:384
Parameters
value
number
Returns
void
Implementation of
x
Get Signature
get x():
number
Defined in: geom/Rectangle.ts:401
Returns
number
Set Signature
set x(
value):void
Defined in: geom/Rectangle.ts:404
Parameters
value
number
Returns
void
Implementation of
y
Get Signature
get y():
number
Defined in: geom/Rectangle.ts:411
Returns
number
Set Signature
set y(
value):void
Defined in: geom/Rectangle.ts:414
Parameters
value
number
Returns
void
Implementation of
Methods
area()
area():
number
Defined in: geom/Rectangle.ts:264
Get the area (w * h) of the rectangle
Returns
number
The area of the rectangle
collide()
collide(
rect):boolean
Defined in: geom/Rectangle.ts:356
Test if the given rectangle collide with this rectangle.
Parameters
rect
rectangle to test collision with
Returns
boolean
true if rectangles collide
contain()
contain(
rect):boolean
Defined in: geom/Rectangle.ts:371
Test if this rectangle contains the given rectangle.
Parameters
rect
rectangle to test containment
Returns
boolean
true if this rectangle contains the given rectangle
setDirty()
setDirty(
value?):void
Defined in: geom/Rectangle.ts:497
Parameters
value?
boolean = true
Returns
void
toJSON()
toJSON():
IRectangleJSON
Defined in: geom/Rectangle.ts:289
The public shape of this rect, for JSON.stringify() (issue #53).
Without this, JSON.stringify(rect) walked the own properties and emitted the backing fields — {"oversized":false,"_rot":false,"_dirty":1,"_width":10,"_height":20,"_x":0,"_y":0,"_data":{}} — so a Rectangle serialized differently from the equivalent plain { width, height } object the packer also accepts. data is the caller's payload and belongs in the output; the internal dirty counter does not.
The shape is the placed rect: width, height, x, y and rot, in that order. oversized is added only when set, data only when it is not the constructor's own untouched {} placeholder — a {} the caller assigned is a value, and so is a null — and allowRotation only when the rect carries an explicit override, since a plain object may state it as a field. Any other enumerable own field is kept as well, so a subclass or a rect carrying custom state still serializes whole; the class's own backing fields are the only thing dropped, by name, so a caller's _assetId survives. Enumerable is the boundary JSON.stringify() itself draws on a plain object, so a field the caller hid stays hidden, and a getter the caller hid is never read — reading it would run code the caller's own serialization never runs.
Returns
the rect's own fields, with the backing fields replaced by the public ones
Clone()
staticClone<T>(rect):T
Defined in: geom/Rectangle.ts:207
Copy a rect object without going through its setters: the prototype and every own property are kept — non-enumerable ones included — so a Rectangle stays a Rectangle, a custom rect class keeps its identity and its extra fields, and a plain {width, height} object stays plain. The copy is shallow — an object held in rect.data, or in any custom field, is shared.
A rect class that keeps its state out of reach of a copy — behind an ECMAScript #private field — cannot be copied this way and has to hand out a clone() method instead, which is used whenever it exists. That method decides how the copy is built, the bin's state fills in the rest, and the rule between them is about what the copy already holds: what the copy defines for itself wins where it is not a plain value — an accessor is how it keeps its own state (#private fields included), a function is behaviour bound to the copy, and a non-configurable property cannot be redefined at all — while plain values, and every key the copy does not define, come from the source.
State travels, behaviour does not. No function is taken over from the source: one cannot be classified from the outside, since a this-based method and a function closing over the source look the same, and a wrong guess either lets a method call on a rect in the cloned bin write to the original or drops a method the copy needs. Prototype methods come with the instance a clone() returns; an own method or accessor the copy needs is that clone()'s to define. So a clone() which rebuilds only the dimensions still inherits the payload, the placement, the per-item rotation permission and any plain field added since construction, but not the source's own functions, and no source accessor travels either — it reads and writes whatever it closed over.
When no clone() exists the copy is a prototype-only shell, so every own descriptor is taken over as-is, functions included: there the copy is the source's shape by construction. A rect that keeps its state in an own accessor closing over the source, or in an own function that does, is then still shared with the original and has to provide clone() as well. A rect whose state is simply out of reach (a #private field) fails on the first field the copy cannot read, and that is reported here rather than from inside a bin placing it.
Type Parameters
T
T extends IRectangleInput
Parameters
rect
T
the rect to copy
Returns
T
a new object carrying the same own values as the source, built on whatever rect.clone() returns when the rect class has one
Collide()
staticCollide(first,second):any
Defined in: geom/Rectangle.ts:158
Test if two given rectangle collide each other
Parameters
first
first rectangle
second
second rectangle
Returns
any
true if rectangles collide
Contain()
staticContain(first,second):any
Defined in: geom/Rectangle.ts:169
Test if the first rectangle contains the second one
Parameters
first
first rectangle
second
second rectangle
Returns
any
true if first rectangle contains the second