2 var <>left=0, <>top=0, <>width=0, <>height=0;
4 *new { arg left=0, top=0, width=0, height=0;
5 ^super.newCopyArgs(left, top, width, height)
7 *newSides { arg left=0, top=0, right=0, bottom=0;
8 ^super.newCopyArgs(left, top, right-left, bottom-top)
10 *fromPoints { arg pt1, pt2;
19 ^this.new(rect.left, rect.top, rect.width, rect.height)
24 *aboutPoint { arg point, dx, dy;
25 ^this.new(point.x-dx, point.y-dy, 2*dx, 2*dy)
28 set { arg argLeft=0, argTop=0, argWidth=0, argHeight=0;
34 setExtent { arg argWidth=0, argHeight=0;
39 origin { ^Point.new(left, top) }
40 origin_ { | pt | left = pt.x; top = pt.y }
41 extent { ^Point.new(width, height) }
42 extent_ { | pt | width = pt.x; height = pt.y }
43 center { ^Point.new(left + (width * 0.5), top + (height * 0.5)) }
44 center_ { arg center; ^this.class.aboutPoint(center, width * 0.5, height * 0.5) }
46 bottom { ^top + height }
47 bottom_ { |b| top = top - (this.bottom - b) }
48 right { ^left + width }
49 right_ { |r| left = left - (this.right - r) }
51 leftTop { ^Point.new(this.left, this.top) }
52 rightTop { ^Point.new(this.right, this.top) }
53 leftBottom { ^Point.new(this.left, this.bottom) }
54 rightBottom { ^Point.new(this.right, this.bottom) }
56 size { ^Size(width,height) }
57 size_ { |sz| width = sz.width; height = sz.height }
60 ^this.class.new(left + h, top + v, width, height)
63 ^this.class.new(h, v, width, height)
65 moveToPoint { arg aPoint;
66 ^this.class.new(aPoint.x, aPoint.y, width, height)
69 ^this.class.new(left, top, width + h, height + (v ? h))
72 ^this.class.new(left, top, h, v)
76 ^this.class.new(left + h, top + v, width - h - h, height - v - v)
78 insetAll { arg a, b, c, d;
79 ^this.class.new(left + a, top + b, width - a - c, height - b - d)
82 ^this.copy.insetAll(r.left, r.top, r.right, r.bottom)
87 pos = (width - height) * 0.5 + left;
88 ^Rect(pos, top, height, height)
90 pos = (height - width) * 0.5 + top;
91 ^Rect(left, pos, width, width)
94 centerIn { arg inRect;
96 spacing = (inRect.extent - this.extent) * 0.5;
97 ^inRect.origin - this.origin + spacing;
100 contains{ arg anObject;
101 if ( anObject.isKindOf( Point ),
102 { ^this.containsPoint( anObject ) });
103 if ( anObject.isKindOf( Rect ),
104 { ^this.containsRect( anObject ) });
108 containsPoint { arg aPoint;
109 ^(aPoint.x.inclusivelyBetween(left, left + width)
110 and: { aPoint.y.inclusivelyBetween(top, top + height) })
112 containsRect { arg aRect;
113 ^(this.containsPoint(aRect.leftTop) and: {this.containsPoint(aRect.rightBottom) })
115 intersects { arg aRect;
116 if (aRect.right <= this.left, { ^false });
117 if (aRect.bottom <= this.top, { ^false });
118 if (aRect.left >= this.right, { ^false });
119 if (aRect.top >= this.bottom, { ^false });
123 & { arg aRect; ^this sect: aRect }
124 | { arg aRect; ^this union: aRect }
127 ^this.class.newSides( left min: aRect.left, top min: aRect.top,
128 this.right max: aRect.right, this.bottom max: aRect.bottom)
131 ^this.class.newSides( left max: aRect.left, top max: aRect.top,
132 this.right min: aRect.right, this.bottom min: aRect.bottom)
134 storeArgs { ^[left,top,width,height] }
135 printOn { arg stream;
136 stream << this.class.name << "(" <<* [left, top, width, height] << ")";
139 // the drawing routine here use Quickdraw.
140 // If you want CoreGraphics drawing, use methods in Pen.
141 draw { arg color, operation=2;
143 ^this.primitiveFailed
147 bounds { ^Rect.new(left, top, width, height) }
149 ^that respondsTo: #[\left, \top, \width, \height]
150 and: { left == that.left
151 and: { top == that.top
152 and: { width == that.width
153 and: { height == that.height }}}}
155 hash { ^left.hash bitXor: top.hash bitXor: width.hash bitXor: height.hash }
158 layout { arg argBounds;
159 this.set(argBounds.left, argBounds.top, argBounds.width, argBounds.height);
162 asArray { ^[this.left, this.top, this.width, this.height] }
164 performBinaryOpOnSomething { |aSelector, thing, adverb|
165 ^thing.asRect.perform(aSelector, this, adverb)
169 thatRect = that.asRect;
172 this.left + thatRect.left,
173 this.top + thatRect.top,
174 this.width + thatRect.width,
175 this.height + thatRect.height
180 thatRect = that.asRect;
183 this.left - thatRect.left,
184 this.top - thatRect.top,
185 this.width - thatRect.width,
186 this.height - thatRect.height