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