{ import: Objects } Rectangle : Object ( origin corner ) Rectangle origin: o corner: c [ self := self new. origin := o. corner := c. ] Rectangle origin: o extent: e [ ^self origin: o corner: o + e ] Rectangle origin [ ^origin ] Rectangle corner [ ^corner ] Rectangle extent [ ^corner - origin ] Rectangle zero [ ^Rectangle origin: PointZero corner: PointZero ] Rectangle + aPair [ ^self origin: origin + aPair corner: corner + aPair ] Rectangle union: aRectangle [ ^self origin: (origin min: aRectangle origin) corner: (corner max: aRectangle corner) ] Rectangle intersect: aRectangle [ "Answer the Rectangle in which the receiver overlaps with aRectangle." | aPoint left right top bottom | aPoint := aRectangle origin. left := (aPoint x > origin x ifTrue: [aPoint] ifFalse: [origin]) x. bottom := (aPoint y > origin y ifTrue: [aPoint] ifFalse: [origin]) y. aPoint := aRectangle corner. right := (aPoint x < corner x ifTrue: [aPoint] ifFalse: [corner]) x. top := (aPoint y < corner y ifTrue: [aPoint] ifFalse: [corner]) y. ^(left <= right and: [bottom <= top]) ifTrue: [self origin: (left , bottom) corner: (right , top)] ifFalse: [Rectangle zero] ] Rectangle printOn: aStream [ aStream nextPut: $(; print: origin; nextPutAll: ' corner: '; print: corner; nextPut: $) ] "----------------" Point : Object ( x y ) Point asPoint [] Point x [ ^x ] Point y [ ^y ] Point withX: xx withY: yy [ self := self new. x := xx. y := yy. ] PointZero := [ Point withX: 0 withY: 0 ] PointOne := [ Point withX: 1 withY: 1 ] Point extent [] Point + aPoint [ ^self withX: x + aPoint x withY: y + aPoint y ] Point - aPoint [ ^self withX: x - aPoint x withY: y - aPoint y ] Point * aNumber [ ^self withX: x * aNumber withY: y * aNumber ] Point / aNumber [ ^self withX: x / aNumber withY: y / aNumber ] Point max: aPoint [ ^self withX: (x max: aPoint x) withY: (y max: aPoint y) ] Point min: aPoint [ ^self withX: (x min: aPoint x) withY: (y min: aPoint y) ] Point printOn: aStream [ aStream nextPut: $(; print: self x; nextPut: $,; print: self y; nextPut: $) ] Number , aNumber [ ^Point withX: self withY: aNumber ] Point < aPoint [ ^x < aPoint x and: [y < aPoint y] ] Point <= aPoint [ ^x <= aPoint x and: [y <= aPoint y] ] Point = aPoint [ ^x = aPoint x and: [y = aPoint y] ] Point > aPoint [ ^x > aPoint x and: [y > aPoint y] ] Point drawOn: aContext [ aContext fillRectangle: 0,0 extent: self ]