1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
Rect {
var <>left=0, <>top=0, <>width=0, <>height=0;
*new { arg left=0, top=0, width=0, height=0;
^super.newCopyArgs(left, top, width, height)
}
*newSides { arg left=0, top=0, right=0, bottom=0;
^super.newCopyArgs(left, top, right-left, bottom-top)
}
*fromPoints { arg pt1, pt2;
^super.newCopyArgs(
pt1.x min: pt2.x,
pt1.y min: pt2.y,
absdif(pt1.x, pt2.x),
absdif(pt1.y, pt2.y)
)
}
*fromRect { arg rect;
^this.new(rect.left, rect.top, rect.width, rect.height)
}
*fromArray {|array|
^this.new(*array)
}
*aboutPoint { arg point, dx, dy;
^this.new(point.x-dx, point.y-dy, 2*dx, 2*dy)
}
set { arg argLeft=0, argTop=0, argWidth=0, argHeight=0;
left = argLeft;
top = argTop;
width = argWidth;
height = argHeight;
}
setExtent { arg argWidth=0, argHeight=0;
width = argWidth;
height = argHeight;
}
origin { ^Point.new(left, top) }
origin_ { | pt | left = pt.x; top = pt.y }
extent { ^Point.new(width, height) }
extent_ { | pt | width = pt.x; height = pt.y }
center { ^Point.new(left + (width * 0.5), top + (height * 0.5)) }
center_ { arg center; ^this.class.aboutPoint(center, width * 0.5, height * 0.5) }
bottom { ^top + height }
bottom_ { |b| top = top - (this.bottom - b) }
right { ^left + width }
right_ { |r| left = left - (this.right - r) }
leftTop { ^Point.new(this.left, this.top) }
rightTop { ^Point.new(this.right, this.top) }
leftBottom { ^Point.new(this.left, this.bottom) }
rightBottom { ^Point.new(this.right, this.bottom) }
size { ^Size(width,height) }
size_ { |sz| width = sz.width; height = sz.height }
moveBy { arg h, v;
^this.class.new(left + h, top + v, width, height)
}
moveTo { arg h, v;
^this.class.new(h, v, width, height)
}
moveToPoint { arg aPoint;
^this.class.new(aPoint.x, aPoint.y, width, height)
}
resizeBy { arg h, v;
^this.class.new(left, top, width + h, height + (v ? h))
}
resizeTo { arg h, v;
^this.class.new(left, top, h, v)
}
insetBy { arg h, v;
if(v.isNil){ v = h };
^this.class.new(left + h, top + v, width - h - h, height - v - v)
}
insetAll { arg a, b, c, d;
^this.class.new(left + a, top + b, width - a - c, height - b - d)
}
insetByRect { arg r;
^this.copy.insetAll(r.left, r.top, r.right, r.bottom)
}
centerSquare {
var pos, center;
if (width > height, {
pos = (width - height) * 0.5 + left;
^Rect(pos, top, height, height)
},{
pos = (height - width) * 0.5 + top;
^Rect(left, pos, width, width)
});
}
centerIn { arg inRect;
var pos, spacing;
spacing = (inRect.extent - this.extent) * 0.5;
^inRect.origin - this.origin + spacing;
}
contains{ arg anObject;
if ( anObject.isKindOf( Point ),
{ ^this.containsPoint( anObject ) });
if ( anObject.isKindOf( Rect ),
{ ^this.containsRect( anObject ) });
^false;
}
containsPoint { arg aPoint;
^(aPoint.x.inclusivelyBetween(left, left + width)
and: { aPoint.y.inclusivelyBetween(top, top + height) })
}
containsRect { arg aRect;
^(this.containsPoint(aRect.leftTop) and: {this.containsPoint(aRect.rightBottom) })
}
intersects { arg aRect;
if (aRect.right <= this.left, { ^false });
if (aRect.bottom <= this.top, { ^false });
if (aRect.left >= this.right, { ^false });
if (aRect.top >= this.bottom, { ^false });
^true
}
& { arg aRect; ^this sect: aRect }
| { arg aRect; ^this union: aRect }
union { arg aRect;
^this.class.newSides( left min: aRect.left, top min: aRect.top,
this.right max: aRect.right, this.bottom max: aRect.bottom)
}
sect { arg aRect;
^this.class.newSides( left max: aRect.left, top max: aRect.top,
this.right min: aRect.right, this.bottom min: aRect.bottom)
}
storeArgs { ^[left,top,width,height] }
printOn { arg stream;
stream << this.class.name << "(" <<* [left, top, width, height] << ")";
}
// the drawing routine here use Quickdraw.
// If you want CoreGraphics drawing, use methods in Pen.
draw { arg color, operation=2;
_Rect_Draw
^this.primitiveFailed
}
asRect { ^this }
bounds { ^Rect.new(left, top, width, height) }
== { arg that;
^this.compareObject(that, #[\left, \top, \width, \height])
}
hash {
^this.instVarHash(#[\left, \top, \width, \height])
}
// deprec
layout { arg argBounds;
this.set(argBounds.left, argBounds.top, argBounds.width, argBounds.height);
}
asArray { ^[this.left, this.top, this.width, this.height] }
performBinaryOpOnSomething { |aSelector, thing, adverb|
^thing.asRect.perform(aSelector, this, adverb)
}
+ {|that|
var thatRect;
thatRect = that.asRect;
^Rect(
this.left + thatRect.left,
this.top + thatRect.top,
this.width + thatRect.width,
this.height + thatRect.height
)
}
- {|that|
var thatRect;
thatRect = that.asRect;
^Rect(
this.left - thatRect.left,
this.top - thatRect.top,
this.width - thatRect.width,
this.height - thatRect.height
)
}
}
|