1
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
186
|
3 |
All Rights Reserved
|
1
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
|
|
13 |
Object subclass:#Rectangle
|
|
14 |
instanceVariableNames:'left top width height'
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Graphics-Primitives'
|
|
18 |
!
|
|
19 |
|
|
20 |
Rectangle comment:'
|
5
|
21 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
186
|
22 |
All Rights Reserved
|
93
|
23 |
|
208
|
24 |
$Header: /cvs/stx/stx/libbasic/Rectangle.st,v 1.12 1994-11-21 16:39:12 claus Exp $
|
1
|
25 |
'!
|
|
26 |
|
77
|
27 |
!Rectangle class methodsFor:'documentation'!
|
|
28 |
|
88
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
186
|
32 |
All Rights Reserved
|
88
|
33 |
|
|
34 |
This software is furnished under a license and may be used
|
|
35 |
only in accordance with the terms of that license and with the
|
|
36 |
inclusion of the above copyright notice. This software may not
|
|
37 |
be provided or otherwise made available to, or used by, any
|
|
38 |
other person. No title to or ownership of the software is
|
|
39 |
hereby transferred.
|
|
40 |
"
|
|
41 |
!
|
|
42 |
|
|
43 |
version
|
|
44 |
"
|
208
|
45 |
$Header: /cvs/stx/stx/libbasic/Rectangle.st,v 1.12 1994-11-21 16:39:12 claus Exp $
|
88
|
46 |
"
|
|
47 |
!
|
|
48 |
|
77
|
49 |
documentation
|
|
50 |
"
|
|
51 |
Rectangles represent a rectangular area in 2D space.
|
|
52 |
|
|
53 |
notice, my implementation does not use origin/corner as instance objects
|
|
54 |
but left/top/width/height to save space and allocations. This means, that my
|
|
55 |
Rectangles cannot be used to represent Rectangles in a higher than 2D
|
|
56 |
space. (i.e. only valid if origin/corner are 2D Points)
|
|
57 |
|
|
58 |
(aside from that, you will not see any difference from the outside)
|
|
59 |
|
|
60 |
Instance variables:
|
|
61 |
|
186
|
62 |
left <Number> the left coordinate (i.e origin x)
|
|
63 |
top <Number> the top coordinate (i.e origin y)
|
|
64 |
width <Number> the width of the rectangle
|
|
65 |
height <Number> the height of the rectangle
|
77
|
66 |
|
|
67 |
I am not certain, if implementing Rectangle different was a good idea -
|
|
68 |
subclasses may expect things to be different ...
|
|
69 |
Therefore, this may change.
|
|
70 |
"
|
|
71 |
! !
|
|
72 |
|
1
|
73 |
!Rectangle class methodsFor:'instance creation'!
|
|
74 |
|
|
75 |
origin:origin corner:corner
|
|
76 |
"create and return a new Rectangle giving top-left and bottom-right points"
|
|
77 |
|
|
78 |
^ (self basicNew) origin:origin corner:corner
|
|
79 |
!
|
|
80 |
|
|
81 |
origin:origin extent:extent
|
|
82 |
"create and return a new Rectangle giving top-left point and extent point"
|
|
83 |
|
|
84 |
%{ /* NOCONTEXT */
|
59
|
85 |
REGISTER OBJ newRect;
|
|
86 |
REGISTER struct point *p;
|
1
|
87 |
extern OBJ Point;
|
|
88 |
|
|
89 |
/* short cut - rectangles are created so often ... */
|
|
90 |
if (_CanDoQuickNew(OHDR_SIZE + 4*sizeof(OBJ))) {
|
186
|
91 |
if (self == Rectangle) {
|
|
92 |
if (__isPoint(origin) && __isPoint(extent)) {
|
|
93 |
_qCheckedAlignedNew(newRect, OHDR_SIZE + 4*sizeof(OBJ));
|
|
94 |
_InstPtr(newRect)->o_class = Rectangle;
|
59
|
95 |
|
186
|
96 |
p = _PointInstPtr(origin);
|
|
97 |
_InstPtr(newRect)->i_instvars[0] = p->p_x;
|
|
98 |
_InstPtr(newRect)->i_instvars[1] = p->p_y;
|
|
99 |
__STORE(newRect, p->p_x);
|
|
100 |
__STORE(newRect, p->p_y);
|
59
|
101 |
|
186
|
102 |
p = _PointInstPtr(extent);
|
|
103 |
_InstPtr(newRect)->i_instvars[2] = p->p_x;
|
|
104 |
_InstPtr(newRect)->i_instvars[3] = p->p_y;
|
|
105 |
__STORE(newRect, p->p_x);
|
|
106 |
__STORE(newRect, p->p_y);
|
|
107 |
RETURN ( newRect );
|
|
108 |
}
|
|
109 |
}
|
1
|
110 |
}
|
|
111 |
%}
|
|
112 |
.
|
|
113 |
^ (self basicNew) origin:origin extent:extent
|
|
114 |
!
|
|
115 |
|
|
116 |
left:left top:top width:w height:h
|
|
117 |
"create and return a new Rectangle giving left and top coordinates
|
|
118 |
and width, height"
|
|
119 |
|
|
120 |
%{ /* NOCONTEXT */
|
59
|
121 |
REGISTER OBJ newRect;
|
1
|
122 |
|
|
123 |
if (_CanDoQuickNew(OHDR_SIZE + 4*sizeof(OBJ))) {
|
186
|
124 |
/* short cut - rectangles are created so often ... */
|
|
125 |
if (self == Rectangle) {
|
|
126 |
_qCheckedAlignedNew(newRect, OHDR_SIZE + 4*sizeof(OBJ));
|
|
127 |
_InstPtr(newRect)->o_class = Rectangle;
|
|
128 |
_InstPtr(newRect)->i_instvars[0] = left;
|
|
129 |
_InstPtr(newRect)->i_instvars[1] = top;
|
|
130 |
_InstPtr(newRect)->i_instvars[2] = w;
|
|
131 |
_InstPtr(newRect)->i_instvars[3] = h;
|
59
|
132 |
|
186
|
133 |
__STORE(newRect, _InstPtr(newRect)->i_instvars[0]);
|
|
134 |
__STORE(newRect, _InstPtr(newRect)->i_instvars[1]);
|
|
135 |
__STORE(newRect, _InstPtr(newRect)->i_instvars[2]);
|
|
136 |
__STORE(newRect, _InstPtr(newRect)->i_instvars[3]);
|
|
137 |
RETURN ( newRect );
|
|
138 |
}
|
1
|
139 |
}
|
|
140 |
%}
|
|
141 |
.
|
|
142 |
^ (self basicNew) left:left top:top width:w height:h
|
|
143 |
!
|
|
144 |
|
|
145 |
left:left right:right top:top bottom:bottom
|
|
146 |
"create and return a new Rectangle giving left, top, right and bottom coordinates"
|
|
147 |
|
|
148 |
^ (self basicNew) left:left right:right top:top bottom:bottom
|
|
149 |
!
|
|
150 |
|
|
151 |
left:left top:top right:right bottom:bottom
|
|
152 |
"create and return a new Rectangle giving left, top, right and bottom coordinates"
|
|
153 |
|
|
154 |
^ (self basicNew) left:left top:top right:right bottom:bottom
|
32
|
155 |
!
|
|
156 |
|
|
157 |
fromUser
|
|
158 |
"let user specify a rectangle on the screen, return it"
|
|
159 |
|
|
160 |
^ Display rectangleFromUser
|
|
161 |
|
|
162 |
"Rectangle fromUser"
|
1
|
163 |
! !
|
|
164 |
|
|
165 |
!Rectangle methodsFor:'accessing'!
|
|
166 |
|
|
167 |
left:newLeft right:right top:newTop bottom:bottom
|
77
|
168 |
"set the rectangle given left, top, right and bottom coordinates."
|
1
|
169 |
|
|
170 |
left := newLeft.
|
|
171 |
top := newTop.
|
|
172 |
width := right - left.
|
|
173 |
height := bottom - top
|
|
174 |
!
|
|
175 |
|
|
176 |
left:newLeft top:newTop right:right bottom:bottom
|
|
177 |
"set the rectangle given left, top, right and bottom coordinates"
|
|
178 |
|
|
179 |
left := newLeft.
|
|
180 |
top := newTop.
|
|
181 |
width := right - left.
|
|
182 |
height := bottom - top
|
|
183 |
!
|
|
184 |
|
|
185 |
left:newLeft top:newTop width:newWidth height:newHeight
|
|
186 |
"set the rectangle given left, top coordinates and width, height"
|
|
187 |
|
|
188 |
left := newLeft.
|
|
189 |
top := newTop.
|
|
190 |
width := newWidth.
|
|
191 |
height := newHeight
|
|
192 |
!
|
|
193 |
|
|
194 |
origin:aPoint
|
77
|
195 |
"set the top-left origin. The corner remains unchanged."
|
|
196 |
|
|
197 |
|newTop newLeft|
|
1
|
198 |
|
77
|
199 |
newTop := aPoint y.
|
|
200 |
newLeft := aPoint x.
|
|
201 |
height := height + (top - newTop).
|
|
202 |
width := width + (left - newLeft).
|
|
203 |
left := newLeft.
|
|
204 |
top := newTop
|
1
|
205 |
!
|
|
206 |
|
|
207 |
corner:aPoint
|
|
208 |
"set the bottom-right corner"
|
|
209 |
|
|
210 |
width := aPoint x - left.
|
|
211 |
height := aPoint y - top
|
|
212 |
!
|
|
213 |
|
|
214 |
top:aNumber
|
|
215 |
"set the top edge"
|
|
216 |
|
|
217 |
height := height + (top - aNumber).
|
|
218 |
top := aNumber
|
|
219 |
!
|
|
220 |
|
|
221 |
bottom:aNumber
|
|
222 |
"set the bottom edge"
|
|
223 |
|
|
224 |
height := aNumber - top
|
|
225 |
!
|
|
226 |
|
77
|
227 |
left:aNumber
|
|
228 |
"set the left edge"
|
|
229 |
|
|
230 |
width := width + (left - aNumber).
|
|
231 |
left := aNumber
|
|
232 |
!
|
|
233 |
|
|
234 |
right:aNumber
|
|
235 |
"set the right edge"
|
|
236 |
|
|
237 |
width := aNumber - left
|
|
238 |
!
|
|
239 |
|
|
240 |
topLeft:aPoint
|
|
241 |
"Set the top and left edges.
|
|
242 |
The bottom right remains unchanged."
|
|
243 |
|
|
244 |
|newTop|
|
|
245 |
|
|
246 |
newTop := aPoint y.
|
|
247 |
height := height + (top - newTop).
|
|
248 |
top := newTop.
|
|
249 |
width := aPoint x - left
|
|
250 |
!
|
|
251 |
|
|
252 |
topRight:aPoint
|
|
253 |
"Set the top and right edges.
|
|
254 |
The bottom left remains unchanged."
|
|
255 |
|
|
256 |
|newTop|
|
|
257 |
|
|
258 |
newTop := aPoint y.
|
|
259 |
height := height + (top - newTop).
|
|
260 |
top := newTop.
|
|
261 |
width := aPoint x - left
|
|
262 |
!
|
|
263 |
|
1
|
264 |
origin:origin corner:corner
|
|
265 |
"set both origin and corner"
|
|
266 |
|
|
267 |
left := origin x.
|
|
268 |
top := origin y.
|
|
269 |
width := corner x - left.
|
|
270 |
height := corner y - top
|
|
271 |
!
|
|
272 |
|
|
273 |
extent:aPoint
|
|
274 |
"set the extent from the argument, aPoint with width taken from aPoint x
|
|
275 |
and height taken from aPoint y"
|
|
276 |
|
|
277 |
width := aPoint x.
|
|
278 |
height := aPoint y
|
|
279 |
!
|
|
280 |
|
|
281 |
origin:origin extent:extent
|
|
282 |
"set both origin and extent"
|
|
283 |
|
|
284 |
left := origin x.
|
|
285 |
top := origin y.
|
|
286 |
width := extent x.
|
|
287 |
height := extent y
|
|
288 |
!
|
|
289 |
|
|
290 |
origin
|
|
291 |
"return the origin"
|
|
292 |
|
77
|
293 |
^ left @ top
|
1
|
294 |
!
|
|
295 |
|
|
296 |
corner
|
|
297 |
"return the corner"
|
|
298 |
|
77
|
299 |
^ (left + width) @ (top + height)
|
|
300 |
!
|
|
301 |
|
|
302 |
width
|
|
303 |
"return the width of the rectangle"
|
|
304 |
|
|
305 |
^ width
|
|
306 |
!
|
|
307 |
|
|
308 |
height
|
|
309 |
"return the height of the rectangle"
|
|
310 |
|
|
311 |
^ height
|
1
|
312 |
!
|
|
313 |
|
|
314 |
extent
|
|
315 |
"return the extent"
|
|
316 |
|
77
|
317 |
^ width @ height
|
|
318 |
!
|
|
319 |
|
|
320 |
left
|
|
321 |
"return the x-coordinate of the top-left origin"
|
|
322 |
|
|
323 |
^ left
|
|
324 |
!
|
|
325 |
|
|
326 |
leftCenter
|
|
327 |
"return the left center point"
|
|
328 |
|
|
329 |
^ left @ (top + (height // 2))
|
|
330 |
!
|
|
331 |
|
|
332 |
top
|
|
333 |
"return the y-coordinate of the top-left"
|
|
334 |
|
|
335 |
^ top
|
1
|
336 |
!
|
|
337 |
|
|
338 |
topLeft
|
77
|
339 |
"return the top-left point - the same as origin"
|
1
|
340 |
|
77
|
341 |
^ left @ top
|
1
|
342 |
!
|
|
343 |
|
|
344 |
topRight
|
|
345 |
"return the top-right point"
|
|
346 |
|
77
|
347 |
^ (left + width) @ top
|
1
|
348 |
!
|
|
349 |
|
77
|
350 |
topCenter
|
|
351 |
"return the top center point"
|
1
|
352 |
|
77
|
353 |
^ (left + (width // 2)) @ top
|
1
|
354 |
!
|
|
355 |
|
|
356 |
bottom
|
|
357 |
"return the y coordinate of the bottom"
|
|
358 |
|
|
359 |
^ (top + height)
|
|
360 |
!
|
|
361 |
|
77
|
362 |
bottomLeft
|
|
363 |
"return the bottom-left point"
|
|
364 |
|
|
365 |
^ left @ (top + height)
|
|
366 |
!
|
|
367 |
|
|
368 |
bottomRight
|
|
369 |
"return the bottom-right point"
|
|
370 |
|
|
371 |
^ (left + width) @ (top + height)
|
|
372 |
!
|
|
373 |
|
|
374 |
bottomCenter
|
|
375 |
"return the bottom center point"
|
|
376 |
|
|
377 |
^ (left + (width // 2)) @ (top + height)
|
|
378 |
!
|
|
379 |
|
1
|
380 |
right
|
|
381 |
"return the x coordinate of the right"
|
|
382 |
|
|
383 |
^ (left + width)
|
|
384 |
!
|
|
385 |
|
77
|
386 |
rightCenter
|
|
387 |
"return the right center point"
|
|
388 |
|
|
389 |
^ (left + width) @ (top + (height // 2))
|
|
390 |
!
|
|
391 |
|
1
|
392 |
center
|
|
393 |
"return the point in the center of the receiver"
|
|
394 |
|
77
|
395 |
^ (left + (width // 2)) @ (top + (height // 2))
|
1
|
396 |
!
|
|
397 |
|
|
398 |
area
|
77
|
399 |
"return the area
|
|
400 |
- for screen Rectangles this is the number of pixels"
|
1
|
401 |
|
|
402 |
^ width * height
|
|
403 |
! !
|
|
404 |
|
|
405 |
!Rectangle methodsFor:'comparing'!
|
|
406 |
|
|
407 |
= aRectangle
|
|
408 |
"return true, if the argument aRectangle represents the same
|
|
409 |
rectangle as the receiver"
|
|
410 |
|
|
411 |
%{ /* NOCONTEXT */
|
59
|
412 |
REGISTER struct instance *mySelf, *other;
|
1
|
413 |
|
59
|
414 |
/*
|
|
415 |
* handle the common case quickly
|
|
416 |
*/
|
|
417 |
if (_isNonNilObject(aRectangle)
|
|
418 |
&& _qClass(aRectangle) == Rectangle) {
|
186
|
419 |
mySelf = _InstPtr(self);
|
|
420 |
other = _InstPtr(aRectangle);
|
|
421 |
if ((mySelf->i_instvars[0] == other->i_instvars[0])
|
|
422 |
&& (mySelf->i_instvars[1] == other->i_instvars[1])
|
|
423 |
&& (mySelf->i_instvars[2] == other->i_instvars[2])
|
|
424 |
&& (mySelf->i_instvars[3] == other->i_instvars[3])) {
|
|
425 |
RETURN ( true );
|
|
426 |
}
|
1
|
427 |
}
|
|
428 |
%}
|
|
429 |
.
|
59
|
430 |
|
1
|
431 |
(aRectangle isKindOf:Rectangle) ifFalse:[^ false].
|
|
432 |
|
|
433 |
left = aRectangle left ifFalse:[^ false].
|
|
434 |
top = aRectangle top ifFalse:[^ false].
|
|
435 |
width = aRectangle width ifFalse:[^ false].
|
|
436 |
height = aRectangle height ifFalse:[^ false].
|
|
437 |
^ true
|
59
|
438 |
!
|
|
439 |
|
|
440 |
hash
|
|
441 |
"return an integer useful for hashing -
|
|
442 |
redefined since = is redefined here"
|
|
443 |
|
|
444 |
^ ((left hash bitShift:16) bitXor:(top hash bitShift:16))
|
|
445 |
+ ((width hash) bitXor:(height hash))
|
1
|
446 |
! !
|
|
447 |
|
77
|
448 |
!Rectangle methodsFor:'queries'!
|
|
449 |
|
|
450 |
isRectangle
|
|
451 |
"return true, if the receiver is some kind of rectangle"
|
|
452 |
|
|
453 |
^ true
|
|
454 |
! !
|
|
455 |
|
1
|
456 |
!Rectangle methodsFor:'testing'!
|
|
457 |
|
|
458 |
containsPoint:aPoint
|
|
459 |
"return true, if the argument, aPoint is contained in the receiver"
|
|
460 |
|
|
461 |
|px py|
|
|
462 |
|
|
463 |
px := aPoint x.
|
|
464 |
(px < left) ifTrue:[^ false].
|
|
465 |
(px > (left + width)) ifTrue:[^ false].
|
|
466 |
py := aPoint y.
|
|
467 |
(py < top) ifTrue:[^ false].
|
|
468 |
(py > (top + height)) ifTrue:[^ false].
|
|
469 |
^ true
|
|
470 |
!
|
|
471 |
|
208
|
472 |
containsPointX:x y:y
|
|
473 |
"return true, if the point defined by x@y is contained in the receiver.
|
|
474 |
This is the same as containsPoint:, but can be used if the coordinates
|
|
475 |
are already available as separate numbers to avoid useless creation of a
|
|
476 |
temporary point."
|
|
477 |
|
|
478 |
(x < left) ifTrue:[^ false].
|
|
479 |
(x > (left + width)) ifTrue:[^ false].
|
|
480 |
(y < top) ifTrue:[^ false].
|
|
481 |
(y > (top + height)) ifTrue:[^ false].
|
|
482 |
^ true
|
|
483 |
!
|
|
484 |
|
1
|
485 |
intersects:aRectangle
|
|
486 |
"return true, if the intersection between the argument, aRectangle
|
|
487 |
and the receiver is not empty"
|
|
488 |
|
|
489 |
|b r|
|
|
490 |
|
|
491 |
(aRectangle right) < left ifTrue:[^ false].
|
|
492 |
(aRectangle bottom) < top ifTrue:[^ false].
|
|
493 |
r := left + width.
|
|
494 |
(aRectangle left) > r ifTrue:[^ false].
|
|
495 |
b := top + height.
|
|
496 |
(aRectangle top) > b ifTrue:[^ false].
|
|
497 |
^ true
|
|
498 |
!
|
|
499 |
|
|
500 |
contains:aRectangle
|
|
501 |
"return true, if the argument, aRectangle is equal to or
|
|
502 |
is contained fully within the receiver"
|
|
503 |
|
|
504 |
(left <= aRectangle left) ifTrue:[
|
|
505 |
((left + width) >= aRectangle right) ifTrue:[
|
186
|
506 |
(top <= aRectangle top) ifTrue:[
|
|
507 |
((top + height) >= aRectangle bottom) ifTrue:[
|
|
508 |
^ true
|
|
509 |
]
|
|
510 |
]
|
1
|
511 |
]
|
|
512 |
].
|
|
513 |
^ false
|
|
514 |
!
|
|
515 |
|
|
516 |
isContainedIn:aRectangle
|
|
517 |
"return true, if the receiver is fully contained within
|
|
518 |
the argument, aRectangle"
|
|
519 |
|
|
520 |
(aRectangle left <= left) ifTrue:[
|
|
521 |
(aRectangle right >= (left + width)) ifTrue:[
|
186
|
522 |
(aRectangle top <= top) ifTrue:[
|
|
523 |
(aRectangle bottom >= (top + height)) ifTrue:[
|
|
524 |
^ true
|
|
525 |
]
|
|
526 |
]
|
1
|
527 |
]
|
|
528 |
].
|
|
529 |
^ false
|
|
530 |
! !
|
|
531 |
|
186
|
532 |
!Rectangle methodsFor:'destructive rectangle operations'!
|
|
533 |
|
|
534 |
translateBy:amount
|
|
535 |
"translate (i.e. move) the receiver rectangle
|
|
536 |
by amount, a Point or Number.
|
|
537 |
This is destructive (modifies the receiver, not a copy) and
|
|
538 |
should only be used if you know, that you are the exclusive owner
|
|
539 |
of the receiver."
|
|
540 |
|
|
541 |
|amountPoint|
|
|
542 |
|
|
543 |
(amount isMemberOf:Point) ifTrue:[ "type hint to stc"
|
|
544 |
left := left + amount x.
|
|
545 |
top := top + amount y
|
|
546 |
] ifFalse:[
|
|
547 |
amountPoint := amount asPoint.
|
|
548 |
left := left + amountPoint x.
|
|
549 |
top := top + amountPoint y
|
|
550 |
]
|
|
551 |
|
|
552 |
"
|
|
553 |
(Rectangle origin:10@10 corner:50@50) translateBy:10
|
|
554 |
"
|
|
555 |
|
|
556 |
"its destructive:"
|
|
557 |
"
|
|
558 |
|r1 r2|
|
|
559 |
|
|
560 |
r1 := Rectangle origin:10@10 corner:50@50.
|
|
561 |
r2 := r1 translateBy:10.
|
|
562 |
r1
|
|
563 |
"
|
|
564 |
!
|
|
565 |
|
|
566 |
scaleBy:scale
|
|
567 |
"scale the receiver rectangle by scale (a Number or Point).
|
|
568 |
This is destructive (modifies the receiver, not a copy) and
|
|
569 |
should only be used if you know, that you are the exclusive owner
|
|
570 |
of the receiver."
|
|
571 |
|
|
572 |
|scalePoint|
|
|
573 |
|
|
574 |
(scale isMemberOf:Point) ifTrue:[ "type hint to stc"
|
|
575 |
width := width * scale x.
|
|
576 |
height := height * scale y.
|
|
577 |
left := left * scale x.
|
|
578 |
top := top * scale y
|
|
579 |
] ifFalse:[
|
|
580 |
scalePoint := scale asPoint.
|
|
581 |
width := width * scalePoint x.
|
|
582 |
height := height * scalePoint y.
|
|
583 |
left := left * scalePoint x.
|
|
584 |
top := top * scalePoint y
|
|
585 |
]
|
|
586 |
"
|
|
587 |
(Rectangle origin:10@10 corner:50@50) scaleBy:2
|
|
588 |
"
|
|
589 |
|
|
590 |
"its destructive:"
|
|
591 |
"
|
|
592 |
|r1 r2|
|
|
593 |
|
|
594 |
r1 := Rectangle origin:10@10 corner:50@50.
|
|
595 |
r2 := r1 scaleBy:2.
|
|
596 |
r1
|
|
597 |
"
|
|
598 |
!
|
|
599 |
|
|
600 |
moveTo:aPoint
|
|
601 |
"destructively translate the rectangle to have its origin at aPoint."
|
|
602 |
|
|
603 |
|diff|
|
|
604 |
|
|
605 |
diff := aPoint - self origin.
|
|
606 |
self origin:aPoint corner:self corner + diff
|
|
607 |
!
|
|
608 |
|
|
609 |
moveBy:aPoint
|
|
610 |
"destructively translate the rectangle by some distance.
|
|
611 |
sorry for the name inconsistency - but GNU-ST named it that way"
|
|
612 |
|
|
613 |
left := left + aPoint x.
|
|
614 |
top := top + aPoint y
|
|
615 |
! !
|
|
616 |
|
1
|
617 |
!Rectangle methodsFor:'rectangle operations'!
|
|
618 |
|
|
619 |
intersect:aRectangle
|
|
620 |
"return a new rectangle covering the intersection of the receiver
|
|
621 |
and the argument, aRectangle.
|
|
622 |
the rectangles must intersect for a valid return"
|
|
623 |
|
|
624 |
^ Rectangle left:(left max:(aRectangle left))
|
186
|
625 |
right:((left + width) min:(aRectangle right))
|
|
626 |
top:(top max:(aRectangle top))
|
|
627 |
bottom:((top + height) min:(aRectangle bottom))
|
208
|
628 |
|
|
629 |
"
|
|
630 |
|r1 r2|
|
|
631 |
|
|
632 |
r1 := Rectangle origin:10@10 corner:100@100.
|
|
633 |
r2 := Rectangle origin:50@50 corner:150@75.
|
|
634 |
r1 intersect:r2
|
|
635 |
"
|
1
|
636 |
!
|
|
637 |
|
|
638 |
merge:aRectangle
|
|
639 |
"return a new rectangle covering both the receiver
|
|
640 |
and the argument, aRectangle"
|
|
641 |
|
|
642 |
^ Rectangle left:(left min:(aRectangle left))
|
186
|
643 |
right:((left + width) max:(aRectangle right))
|
|
644 |
top:(top min:(aRectangle top))
|
|
645 |
bottom:((top + height) max:(aRectangle bottom))
|
1
|
646 |
!
|
|
647 |
|
|
648 |
+ aPoint
|
|
649 |
"return a Rectangle with same extent as receiver but
|
|
650 |
origin translated by the argument, aPoint"
|
|
651 |
|
|
652 |
^ Rectangle origin:(self origin + aPoint) extent:(self extent)
|
|
653 |
!
|
|
654 |
|
|
655 |
rounded
|
|
656 |
^ Rectangle left:(left rounded) top:(top rounded)
|
186
|
657 |
width:(width rounded) height:(height rounded)
|
1
|
658 |
!
|
|
659 |
|
77
|
660 |
expandBy:delta
|
1
|
661 |
"return a new rectangle which is expanded in all directions
|
77
|
662 |
by amount, a Point, Rectangle or Number"
|
|
663 |
|
|
664 |
|amountPoint deltaLeft deltaTop deltaWidth deltaHeight|
|
1
|
665 |
|
77
|
666 |
delta isNumber ifTrue:[
|
186
|
667 |
deltaLeft := deltaTop := delta.
|
|
668 |
deltaWidth := deltaHeight := delta * 2.
|
77
|
669 |
] ifFalse:[
|
186
|
670 |
delta isRectangle ifTrue:[
|
|
671 |
deltaLeft := delta left.
|
|
672 |
deltaTop := delta top.
|
|
673 |
deltaWidth := deltaLeft + delta right.
|
|
674 |
deltaHeight := deltaTop + delta bottom
|
|
675 |
] ifFalse:[
|
|
676 |
amountPoint := delta asPoint.
|
|
677 |
deltaLeft := amountPoint x.
|
|
678 |
deltaTop := amountPoint y.
|
|
679 |
deltaWidth := deltaLeft * 2.
|
|
680 |
deltaHeight := deltaTop * 2.
|
|
681 |
]
|
77
|
682 |
].
|
1
|
683 |
|
77
|
684 |
^ Rectangle left:(left - deltaLeft)
|
186
|
685 |
top:(top - deltaTop)
|
|
686 |
width:(width + deltaWidth)
|
|
687 |
height:(height + deltaHeight)
|
77
|
688 |
|
|
689 |
"
|
|
690 |
|r|
|
|
691 |
r := Rectangle origin:10@10 corner:100@100.
|
|
692 |
r expandBy:5.
|
|
693 |
r expandBy:(5 @ 0).
|
|
694 |
r expandBy:(10 @ 10).
|
|
695 |
r expandBy:( 10@10 corner:20@20 )
|
|
696 |
"
|
1
|
697 |
!
|
|
698 |
|
|
699 |
insetBy: delta
|
|
700 |
"return a new rectangle which is inset in all directions
|
77
|
701 |
by delta, a Point, Rectangle or Number"
|
1
|
702 |
|
77
|
703 |
|amountPoint deltaLeft deltaTop deltaWidth deltaHeight|
|
1
|
704 |
|
77
|
705 |
delta isNumber ifTrue:[
|
186
|
706 |
deltaLeft := deltaTop := delta.
|
|
707 |
deltaWidth := deltaHeight := delta * 2.
|
77
|
708 |
] ifFalse:[
|
186
|
709 |
delta isRectangle ifTrue:[
|
|
710 |
deltaLeft := delta left.
|
|
711 |
deltaTop := delta top.
|
|
712 |
deltaWidth := deltaLeft + delta right.
|
|
713 |
deltaHeight := deltaTop + delta bottom
|
|
714 |
] ifFalse:[
|
|
715 |
amountPoint := delta asPoint.
|
|
716 |
deltaLeft := amountPoint x.
|
|
717 |
deltaTop := amountPoint y.
|
|
718 |
deltaWidth := deltaLeft * 2.
|
|
719 |
deltaHeight := deltaTop * 2.
|
|
720 |
]
|
77
|
721 |
].
|
|
722 |
|
|
723 |
^ Rectangle left:(left + deltaLeft)
|
186
|
724 |
top:(top + deltaTop)
|
|
725 |
width:(width - deltaWidth)
|
|
726 |
height:(height - deltaHeight)
|
77
|
727 |
"
|
|
728 |
|r|
|
|
729 |
r := Rectangle origin:10@10 corner:100@100.
|
|
730 |
r insetBy:5.
|
|
731 |
r insetBy:(5 @ 0).
|
|
732 |
r insetBy:(10 @ 10).
|
|
733 |
r insetBy:( 10@10 corner:20@20 )
|
|
734 |
"
|
1
|
735 |
!
|
|
736 |
|
3
|
737 |
translatedBy:amount
|
1
|
738 |
"return a new rectangle which is translated (i.e. moved)
|
|
739 |
by amount, aPoint or Number"
|
|
740 |
|
|
741 |
|amountPoint|
|
|
742 |
|
|
743 |
amountPoint := amount asPoint.
|
59
|
744 |
^ Rectangle left:(left + amountPoint x)
|
186
|
745 |
top:(top + amountPoint y)
|
|
746 |
width:width
|
|
747 |
height:height
|
|
748 |
"
|
|
749 |
(Rectangle origin:10@10 corner:50@50) translatedBy:10
|
|
750 |
"
|
1
|
751 |
|
186
|
752 |
"its NOT destructive:"
|
|
753 |
"
|
|
754 |
|r1 r2|
|
1
|
755 |
|
186
|
756 |
r1 := Rectangle origin:10@10 corner:50@50.
|
|
757 |
r2 := r1 translatedBy:10.
|
|
758 |
r1
|
|
759 |
"
|
1
|
760 |
!
|
|
761 |
|
|
762 |
scaledBy:scale
|
|
763 |
"return a new rectangle which is the receiver
|
|
764 |
scaled by scale"
|
|
765 |
|
|
766 |
|scalePoint|
|
|
767 |
|
|
768 |
scalePoint := scale asPoint.
|
|
769 |
^ Rectangle left:left top:top
|
186
|
770 |
width:(width * scalePoint x)
|
|
771 |
height:(height * scalePoint y)
|
|
772 |
"
|
|
773 |
(Rectangle origin:10@10 corner:50@50) scaledBy:2
|
|
774 |
"
|
|
775 |
|
|
776 |
"its NOT destructive:"
|
|
777 |
"
|
|
778 |
|r1 r2|
|
|
779 |
|
|
780 |
r1 := Rectangle origin:10@10 corner:50@50.
|
|
781 |
r2 := r1 scaledBy:2.
|
|
782 |
r1
|
|
783 |
"
|
1
|
784 |
!
|
|
785 |
|
|
786 |
amountToTranslateWithin: aRectangle
|
|
787 |
"for GNU-ST compatibility"
|
|
788 |
|
|
789 |
^(aRectangle origin) - self origin
|
|
790 |
!
|
|
791 |
|
|
792 |
areasOutside: aRectangle
|
|
793 |
"----------------------------------------------------------------
|
|
794 |
| added for GNU-ST compatibility
|
|
795 |
|
|
|
796 |
| author: Doug McCallum <uunet!!ico.isc.com!!dougm>
|
|
797 |
|
|
|
798 |
|areasOutside: aRectangle
|
|
799 |
| most complicated of the Rectangle primitives
|
|
800 |
| The basic methodology is to first determine that there is an
|
|
801 |
| intersection by finding the overlapping rectangle. From the
|
|
802 |
| overlapping rectangle, first determine if it runs along an edge.
|
|
803 |
| If it doesn't, extend the rectangle up to the top edge and add
|
|
804 |
| the new rectangle to the collection and start the rest of the
|
|
805 |
| process. If the left edge does not touch the left edge of self,
|
|
806 |
| extend it to the edge saving the new rectangle. Then do the
|
|
807 |
| same to the right edge. Then check top and bottom edges. Most
|
|
808 |
| of the time only 2 or 3 rectangles get formed, occasionally 4.
|
|
809 |
| It should be possible to never get more than 3 but requires more
|
|
810 |
| work.
|
|
811 |
----------------------------------------------------------------"
|
|
812 |
|
|
813 |
| collect iRect tmp |
|
|
814 |
iRect _ self intersect: aRectangle.
|
|
815 |
(iRect = nil) ifTrue: [^nil]. "case of no intersection"
|
186
|
816 |
"the collect collection gathers Rectangles"
|
1
|
817 |
collect _ OrderedCollection new: 4.
|
186
|
818 |
"is it floating or on the edge?"
|
1
|
819 |
(((((iRect top) ~= self top)
|
186
|
820 |
and: [ (iRect bottom) ~= self bottom ])
|
|
821 |
and: [ (iRect left) ~= self left ])
|
|
822 |
and: [ (iRect right) ~= self right ] )
|
|
823 |
ifTrue: "entirely in the center."
|
|
824 |
[tmp _ Rectangle origin: (Point x: iRect left y: self top)
|
|
825 |
corner: iRect bottomRight.
|
|
826 |
collect add: tmp.
|
|
827 |
iRect _ iRect merge: tmp].
|
1
|
828 |
((iRect left) ~= self left)
|
186
|
829 |
ifTrue: "doesn't touch left edge so make it touch"
|
|
830 |
[tmp _ Rectangle origin: (Point x: self left y: iRect top)
|
|
831 |
corner: iRect bottomLeft.
|
|
832 |
collect add: tmp.
|
|
833 |
"merge new (tmp) with overlap to keep track"
|
|
834 |
iRect _ iRect merge: tmp].
|
1
|
835 |
((iRect right) ~= self right)
|
186
|
836 |
ifTrue: "doesn't touch right edge so extend it"
|
|
837 |
[tmp _ Rectangle origin: iRect topRight
|
|
838 |
corner: (Point x: self right y: iRect bottom).
|
|
839 |
collect add: tmp.
|
|
840 |
iRect _ iRect merge: tmp].
|
77
|
841 |
(((iRect left) ~= self left) or: [(iRect top) ~= self top])
|
186
|
842 |
ifTrue: "whole top part can be taken now"
|
|
843 |
[tmp _ Rectangle origin: self origin corner: iRect topRight.
|
|
844 |
collect add: tmp].
|
77
|
845 |
(((iRect right) ~= self right) or: [(iRect bottom) ~= self bottom])
|
186
|
846 |
ifTrue: "whole bottom open and can be taken"
|
|
847 |
[tmp _ Rectangle origin: iRect bottomLeft corner: self corner.
|
|
848 |
collect add: tmp].
|
1
|
849 |
^collect
|
77
|
850 |
!
|
1
|
851 |
|
77
|
852 |
nonIntersections:aRectangle
|
|
853 |
"this is the same as areasOutside: - for ST/V compatibility only"
|
|
854 |
|
|
855 |
^ self areasOutside:aRectangle
|
1
|
856 |
! !
|
|
857 |
|
93
|
858 |
!Rectangle methodsFor:'printing & storing'!
|
1
|
859 |
|
|
860 |
printOn:aStream
|
|
861 |
"print the receiver on aStream"
|
|
862 |
|
59
|
863 |
aStream nextPutAll:(self class name).
|
|
864 |
aStream nextPutAll:' origin:'.
|
1
|
865 |
(self origin) printOn:aStream.
|
|
866 |
aStream nextPutAll:' corner:'.
|
|
867 |
(self corner) printOn:aStream
|
|
868 |
!
|
|
869 |
|
|
870 |
storeOn:aStream
|
|
871 |
"store the receiver on aStream; i.e. print an expression which will
|
|
872 |
reconstruct the receiver"
|
|
873 |
|
59
|
874 |
aStream nextPut:$(.
|
1
|
875 |
aStream nextPutAll:(self class name).
|
59
|
876 |
aStream nextPutAll:' origin:'.
|
|
877 |
(self origin) storeOn:aStream.
|
1
|
878 |
aStream nextPutAll:' corner:'.
|
59
|
879 |
(self corner) storeOn:aStream.
|
|
880 |
aStream nextPut:$)
|
1
|
881 |
! !
|