author | Claus Gittinger <cg@exept.de> |
Thu, 04 Jan 1996 02:24:09 +0100 | |
changeset 829 | fc386319f41c |
parent 637 | f71df465819c |
child 835 | 8bd6f4aa8130 |
permissions | -rw-r--r-- |
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 |
||
356 | 13 |
Geometric subclass:#Rectangle |
637 | 14 |
instanceVariableNames:'left top width height' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Graphics-Geometry' |
|
1 | 18 |
! |
19 |
||
77 | 20 |
!Rectangle class methodsFor:'documentation'! |
21 |
||
88 | 22 |
copyright |
23 |
" |
|
24 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
186 | 25 |
All Rights Reserved |
88 | 26 |
|
27 |
This software is furnished under a license and may be used |
|
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
35 |
||
77 | 36 |
documentation |
37 |
" |
|
38 |
Rectangles represent a rectangular area in 2D space. |
|
39 |
||
40 |
notice, my implementation does not use origin/corner as instance objects |
|
41 |
but left/top/width/height to save space and allocations. This means, that my |
|
42 |
Rectangles cannot be used to represent Rectangles in a higher than 2D |
|
43 |
space. (i.e. only valid if origin/corner are 2D Points) |
|
44 |
||
45 |
(aside from that, you will not see any difference from the outside) |
|
46 |
||
47 |
Instance variables: |
|
48 |
||
186 | 49 |
left <Number> the left coordinate (i.e origin x) |
50 |
top <Number> the top coordinate (i.e origin y) |
|
51 |
width <Number> the width of the rectangle |
|
52 |
height <Number> the height of the rectangle |
|
77 | 53 |
|
54 |
I am not certain, if implementing Rectangle different was a good idea - |
|
55 |
subclasses may expect things to be different ... |
|
56 |
Therefore, this may change. |
|
57 |
" |
|
58 |
! ! |
|
59 |
||
1 | 60 |
!Rectangle class methodsFor:'instance creation'! |
61 |
||
637 | 62 |
fromUser |
63 |
"let user specify a rectangle on the screen, return it" |
|
64 |
||
65 |
^ Screen current rectangleFromUser |
|
66 |
||
67 |
" |
|
68 |
Rectangle fromUser |
|
69 |
" |
|
70 |
! |
|
71 |
||
72 |
left:left right:right top:top bottom:bottom |
|
73 |
"create and return a new Rectangle giving left, top, right and bottom coordinates" |
|
74 |
||
75 |
^ (self basicNew) left:left right:right top:top bottom:bottom |
|
76 |
! |
|
77 |
||
78 |
left:left top:top right:right bottom:bottom |
|
79 |
"create and return a new Rectangle giving left, top, right and bottom coordinates" |
|
80 |
||
81 |
^ (self basicNew) left:left top:top right:right bottom:bottom |
|
82 |
! |
|
83 |
||
84 |
left:left top:top width:w height:h |
|
85 |
"create and return a new Rectangle giving left and top coordinates |
|
86 |
and width, height" |
|
87 |
||
88 |
%{ /* NOCONTEXT */ |
|
89 |
REGISTER OBJ newRect; |
|
90 |
OBJ t; |
|
91 |
int spc; |
|
92 |
||
93 |
if (_CanDoQuickNew(OHDR_SIZE + 4*sizeof(OBJ))) { |
|
94 |
if (self == Rectangle) { |
|
95 |
/* short cut - rectangles are created so often ... */ |
|
96 |
_qCheckedAlignedNew(newRect, OHDR_SIZE + 4*sizeof(OBJ)); |
|
97 |
||
98 |
_InstPtr(newRect)->o_class = Rectangle; |
|
99 |
_InstPtr(newRect)->i_instvars[0] = left; |
|
100 |
_InstPtr(newRect)->i_instvars[1] = top; |
|
101 |
_InstPtr(newRect)->i_instvars[2] = w; |
|
102 |
_InstPtr(newRect)->i_instvars[3] = h; |
|
103 |
||
104 |
if (! (__bothSmallInteger(left, top) && |
|
105 |
__bothSmallInteger(w, h))) { |
|
106 |
spc = __qSpace(newRect); |
|
107 |
__STORE_SPC(newRect, left, spc); |
|
108 |
__STORE_SPC(newRect, top, spc); |
|
109 |
__STORE_SPC(newRect, w, spc); |
|
110 |
__STORE_SPC(newRect, h, spc); |
|
111 |
} |
|
112 |
||
113 |
RETURN ( newRect ); |
|
114 |
} |
|
115 |
} |
|
116 |
%} |
|
117 |
. |
|
118 |
^ (self basicNew) left:left top:top width:w height:h |
|
119 |
! |
|
120 |
||
1 | 121 |
origin:origin corner:corner |
122 |
"create and return a new Rectangle giving top-left and bottom-right points" |
|
123 |
||
124 |
^ (self basicNew) origin:origin corner:corner |
|
125 |
! |
|
126 |
||
127 |
origin:origin extent:extent |
|
128 |
"create and return a new Rectangle giving top-left point and extent point" |
|
129 |
||
130 |
%{ /* NOCONTEXT */ |
|
59 | 131 |
REGISTER OBJ newRect; |
293 | 132 |
OBJ t; |
133 |
int spc; |
|
1 | 134 |
|
135 |
/* short cut - rectangles are created so often ... */ |
|
136 |
if (_CanDoQuickNew(OHDR_SIZE + 4*sizeof(OBJ))) { |
|
186 | 137 |
if (self == Rectangle) { |
138 |
if (__isPoint(origin) && __isPoint(extent)) { |
|
139 |
_qCheckedAlignedNew(newRect, OHDR_SIZE + 4*sizeof(OBJ)); |
|
140 |
_InstPtr(newRect)->o_class = Rectangle; |
|
325 | 141 |
spc = __qSpace(newRect); |
59 | 142 |
|
325 | 143 |
t = _PointInstPtr(origin)->p_x; |
293 | 144 |
_InstPtr(newRect)->i_instvars[0] = t; |
145 |
__STORE_SPC(newRect, t, spc); |
|
146 |
||
325 | 147 |
t = _PointInstPtr(origin)->p_y; |
293 | 148 |
_InstPtr(newRect)->i_instvars[1] = t; |
149 |
__STORE_SPC(newRect, t, spc); |
|
59 | 150 |
|
325 | 151 |
t = _PointInstPtr(extent)->p_x; |
293 | 152 |
_InstPtr(newRect)->i_instvars[2] = t; |
153 |
__STORE_SPC(newRect, t, spc); |
|
154 |
||
325 | 155 |
t = _PointInstPtr(extent)->p_y; |
293 | 156 |
_InstPtr(newRect)->i_instvars[3] = t; |
157 |
__STORE_SPC(newRect, t, spc); |
|
186 | 158 |
RETURN ( newRect ); |
159 |
} |
|
160 |
} |
|
1 | 161 |
} |
162 |
%} |
|
163 |
. |
|
164 |
^ (self basicNew) origin:origin extent:extent |
|
165 |
! ! |
|
166 |
||
167 |
!Rectangle methodsFor:'accessing'! |
|
168 |
||
637 | 169 |
area |
170 |
"return the area |
|
171 |
- for screen Rectangles this is the number of pixels" |
|
172 |
||
173 |
^ width * height |
|
174 |
! |
|
175 |
||
176 |
bottom |
|
177 |
"return the y coordinate of the bottom" |
|
178 |
||
179 |
^ (top + height) |
|
180 |
! |
|
181 |
||
182 |
bottom:aNumber |
|
183 |
"set the bottom edge" |
|
184 |
||
185 |
height := aNumber - top |
|
186 |
! |
|
187 |
||
188 |
bottomCenter |
|
189 |
"return the bottom center point" |
|
190 |
||
191 |
^ (left + (width // 2)) @ (top + height) |
|
192 |
! |
|
193 |
||
194 |
bottomLeft |
|
195 |
"return the bottom-left point" |
|
196 |
||
197 |
^ left @ (top + height) |
|
198 |
! |
|
199 |
||
200 |
bottomRight |
|
201 |
"return the bottom-right point" |
|
202 |
||
203 |
^ (left + width) @ (top + height) |
|
204 |
! |
|
205 |
||
206 |
center |
|
207 |
"return the point in the center of the receiver" |
|
208 |
||
209 |
^ (left + (width // 2)) @ (top + (height // 2)) |
|
210 |
! |
|
211 |
||
212 |
corner |
|
213 |
"return the corner" |
|
214 |
||
215 |
^ (left + width) @ (top + height) |
|
216 |
! |
|
217 |
||
218 |
corner:aPoint |
|
219 |
"set the bottom-right corner" |
|
220 |
||
221 |
width := aPoint x - left. |
|
222 |
height := aPoint y - top |
|
223 |
! |
|
224 |
||
225 |
extent |
|
226 |
"return the extent" |
|
227 |
||
228 |
^ width @ height |
|
229 |
! |
|
230 |
||
231 |
extent:aPoint |
|
232 |
"set the extent from the argument, aPoint with width taken from aPoint x |
|
233 |
and height taken from aPoint y" |
|
234 |
||
235 |
width := aPoint x. |
|
236 |
height := aPoint y |
|
237 |
! |
|
238 |
||
239 |
height |
|
240 |
"return the height of the rectangle" |
|
241 |
||
242 |
^ height |
|
243 |
! |
|
244 |
||
245 |
left |
|
246 |
"return the x-coordinate of the top-left origin" |
|
247 |
||
248 |
^ left |
|
249 |
! |
|
250 |
||
251 |
left:aNumber |
|
252 |
"set the left edge" |
|
253 |
||
254 |
width := width + (left - aNumber). |
|
255 |
left := aNumber |
|
256 |
! |
|
257 |
||
1 | 258 |
left:newLeft right:right top:newTop bottom:bottom |
77 | 259 |
"set the rectangle given left, top, right and bottom coordinates." |
1 | 260 |
|
261 |
left := newLeft. |
|
262 |
top := newTop. |
|
263 |
width := right - left. |
|
264 |
height := bottom - top |
|
265 |
! |
|
266 |
||
267 |
left:newLeft top:newTop right:right bottom:bottom |
|
268 |
"set the rectangle given left, top, right and bottom coordinates" |
|
269 |
||
270 |
left := newLeft. |
|
271 |
top := newTop. |
|
272 |
width := right - left. |
|
273 |
height := bottom - top |
|
274 |
! |
|
275 |
||
276 |
left:newLeft top:newTop width:newWidth height:newHeight |
|
277 |
"set the rectangle given left, top coordinates and width, height" |
|
278 |
||
279 |
left := newLeft. |
|
280 |
top := newTop. |
|
281 |
width := newWidth. |
|
282 |
height := newHeight |
|
283 |
! |
|
284 |
||
637 | 285 |
leftCenter |
286 |
"return the left center point" |
|
287 |
||
288 |
^ left @ (top + (height // 2)) |
|
289 |
! |
|
290 |
||
291 |
origin |
|
292 |
"return the origin" |
|
293 |
||
294 |
^ left @ top |
|
295 |
! |
|
296 |
||
1 | 297 |
origin:aPoint |
77 | 298 |
"set the top-left origin. The corner remains unchanged." |
299 |
||
300 |
|newTop newLeft| |
|
1 | 301 |
|
77 | 302 |
newTop := aPoint y. |
303 |
newLeft := aPoint x. |
|
304 |
height := height + (top - newTop). |
|
305 |
width := width + (left - newLeft). |
|
306 |
left := newLeft. |
|
307 |
top := newTop |
|
1 | 308 |
! |
309 |
||
637 | 310 |
origin:origin corner:corner |
311 |
"set both origin and corner" |
|
312 |
||
313 |
left := origin x. |
|
314 |
top := origin y. |
|
315 |
width := corner x - left. |
|
316 |
height := corner y - top |
|
317 |
! |
|
318 |
||
319 |
origin:origin extent:extent |
|
320 |
"set both origin and extent" |
|
321 |
||
322 |
left := origin x. |
|
323 |
top := origin y. |
|
324 |
width := extent x. |
|
325 |
height := extent y |
|
326 |
! |
|
1 | 327 |
|
637 | 328 |
right |
329 |
"return the x coordinate of the right" |
|
330 |
||
331 |
^ (left + width) |
|
332 |
! |
|
333 |
||
334 |
right:aNumber |
|
335 |
"set the right edge" |
|
336 |
||
337 |
width := aNumber - left |
|
338 |
! |
|
339 |
||
340 |
rightCenter |
|
341 |
"return the right center point" |
|
342 |
||
343 |
^ (left + width) @ (top + (height // 2)) |
|
344 |
! |
|
345 |
||
346 |
top |
|
347 |
"return the y-coordinate of the top-left" |
|
348 |
||
349 |
^ top |
|
1 | 350 |
! |
351 |
||
352 |
top:aNumber |
|
353 |
"set the top edge" |
|
354 |
||
355 |
height := height + (top - aNumber). |
|
356 |
top := aNumber |
|
357 |
! |
|
358 |
||
637 | 359 |
topCenter |
360 |
"return the top center point" |
|
1 | 361 |
|
637 | 362 |
^ (left + (width // 2)) @ top |
1 | 363 |
! |
364 |
||
637 | 365 |
topLeft |
366 |
"return the top-left point - the same as origin" |
|
77 | 367 |
|
637 | 368 |
^ left @ top |
77 | 369 |
! |
370 |
||
371 |
topLeft:aPoint |
|
372 |
"Set the top and left edges. |
|
373 |
The bottom right remains unchanged." |
|
374 |
||
375 |
|newTop| |
|
376 |
||
377 |
newTop := aPoint y. |
|
378 |
height := height + (top - newTop). |
|
379 |
top := newTop. |
|
380 |
width := aPoint x - left |
|
381 |
! |
|
382 |
||
637 | 383 |
topRight |
384 |
"return the top-right point" |
|
385 |
||
386 |
^ (left + width) @ top |
|
387 |
! |
|
388 |
||
77 | 389 |
topRight:aPoint |
390 |
"Set the top and right edges. |
|
391 |
The bottom left remains unchanged." |
|
392 |
||
393 |
|newTop| |
|
394 |
||
395 |
newTop := aPoint y. |
|
396 |
height := height + (top - newTop). |
|
397 |
top := newTop. |
|
398 |
width := aPoint x - left |
|
399 |
! |
|
400 |
||
401 |
width |
|
402 |
"return the width of the rectangle" |
|
403 |
||
404 |
^ width |
|
1 | 405 |
! ! |
406 |
||
407 |
!Rectangle methodsFor:'comparing'! |
|
408 |
||
409 |
= aRectangle |
|
410 |
"return true, if the argument aRectangle represents the same |
|
411 |
rectangle as the receiver" |
|
412 |
||
413 |
%{ /* NOCONTEXT */ |
|
59 | 414 |
/* |
415 |
* handle the common case quickly |
|
416 |
*/ |
|
329 | 417 |
if (__isNonNilObject(aRectangle) |
418 |
&& __qClass(aRectangle) == Rectangle) { |
|
325 | 419 |
if ((_InstPtr(self)->i_instvars[0] == _InstPtr(aRectangle)->i_instvars[0]) |
420 |
&& (_InstPtr(self)->i_instvars[1] == _InstPtr(aRectangle)->i_instvars[1]) |
|
421 |
&& (_InstPtr(self)->i_instvars[2] == _InstPtr(aRectangle)->i_instvars[2]) |
|
422 |
&& (_InstPtr(self)->i_instvars[3] == _InstPtr(aRectangle)->i_instvars[3])) { |
|
186 | 423 |
RETURN ( true ); |
424 |
} |
|
325 | 425 |
if (__bothSmallInteger(_InstPtr(self)->i_instvars[0], _InstPtr(aRectangle)->i_instvars[0]) |
426 |
&& __bothSmallInteger(_InstPtr(self)->i_instvars[1], _InstPtr(aRectangle)->i_instvars[1]) |
|
427 |
&& __bothSmallInteger(_InstPtr(self)->i_instvars[2], _InstPtr(aRectangle)->i_instvars[2]) |
|
428 |
&& __bothSmallInteger(_InstPtr(self)->i_instvars[3], _InstPtr(aRectangle)->i_instvars[3])) { |
|
429 |
RETURN ( false ); |
|
430 |
} |
|
1 | 431 |
} |
432 |
%} |
|
433 |
. |
|
259 | 434 |
(aRectangle species ~~ self species) ifTrue:[^ false]. |
1 | 435 |
|
436 |
left = aRectangle left ifFalse:[^ false]. |
|
437 |
top = aRectangle top ifFalse:[^ false]. |
|
438 |
width = aRectangle width ifFalse:[^ false]. |
|
439 |
height = aRectangle height ifFalse:[^ false]. |
|
440 |
^ true |
|
59 | 441 |
! |
442 |
||
443 |
hash |
|
444 |
"return an integer useful for hashing - |
|
445 |
redefined since = is redefined here" |
|
446 |
||
447 |
^ ((left hash bitShift:16) bitXor:(top hash bitShift:16)) |
|
448 |
+ ((width hash) bitXor:(height hash)) |
|
1 | 449 |
! ! |
450 |
||
356 | 451 |
!Rectangle methodsFor:'converting'! |
452 |
||
453 |
asFractionalLayout |
|
454 |
"return a layoutFrame in which fractions (top, left, bottom, right) |
|
455 |
are taken from corresponding edges of the receiver. |
|
456 |
You have to make certain that those are in 0..1." |
|
457 |
||
458 |
|l| |
|
459 |
||
460 |
l := LayoutFrame new. |
|
461 |
l |
|
462 |
leftFraction:(self left); |
|
463 |
rightFraction:(self right); |
|
464 |
topFraction:(self top); |
|
465 |
bottomFraction:(self bottom). |
|
466 |
^ l |
|
467 |
||
468 |
" |
|
469 |
(0.5@0.5 corner:0.75@0.75) asFractionalLayout |
|
470 |
(0.5@0.5 corner:0.75@0.75) asOffsetLayout |
|
471 |
(0.5@0.5 corner:0.75@0.75) asLayout |
|
472 |
" |
|
473 |
! |
|
474 |
||
475 |
asLayout |
|
476 |
"return a layoutFrame in which offsets (top, left, bottom, right) |
|
477 |
are taken from corresponding edges of the receiver. |
|
478 |
If all values are between 0..1, a fractionalLayout is created, |
|
479 |
otherwise, an offsetLayout" |
|
480 |
||
481 |
|l left right top bot| |
|
482 |
||
483 |
l := LayoutFrame new. |
|
484 |
left := (self left). |
|
485 |
right := (self right). |
|
486 |
top := (self top). |
|
487 |
bot := (self bottom). |
|
488 |
((left between:0 and:1) |
|
489 |
and:[(right between:0 and:1) |
|
490 |
and:[(top between:0 and:1) |
|
491 |
and:[(bot between:0 and:1)]]]) ifTrue:[ |
|
492 |
l |
|
493 |
leftFraction:left; |
|
494 |
rightFraction:right; |
|
495 |
topFraction:top; |
|
496 |
bottomFraction:bot. |
|
497 |
] ifFalse:[ |
|
498 |
l |
|
499 |
leftOffset:left; |
|
500 |
rightFraction:0 offset:right; |
|
501 |
topOffset:top; |
|
502 |
bottomFraction:0 offset:bot. |
|
503 |
]. |
|
504 |
^ l |
|
505 |
||
506 |
" |
|
507 |
(0.5@0.5 corner:0.75@0.75) asFractionalLayout |
|
508 |
(0.5@0.5 corner:0.75@0.75) asOffsetLayout |
|
509 |
(0.5@0.5 corner:0.75@0.75) asLayout |
|
510 |
(0@0 corner:1@1) asLayout |
|
511 |
(0@0 corner:1@1) asFractionalLayout |
|
512 |
(0@0 corner:1@1) asOffsetLayout |
|
513 |
" |
|
514 |
! |
|
515 |
||
637 | 516 |
asOffsetLayout |
517 |
"return a layoutFrame in which offsets (top, left, bottom, right) |
|
518 |
are taken from corresponding edges of the receiver. |
|
519 |
You have to make certain that those are in 0..1." |
|
520 |
||
521 |
|l| |
|
522 |
||
523 |
l := LayoutFrame new. |
|
524 |
l |
|
525 |
leftOffset:(self left); |
|
526 |
rightFraction:0 offset:(self right); |
|
527 |
topOffset:(self top); |
|
528 |
bottomFraction:0 offset:(self bottom). |
|
529 |
^ l |
|
530 |
||
531 |
" |
|
532 |
(0.5@0.5 corner:0.75@0.75) asFractionalLayout |
|
533 |
(0.5@0.5 corner:0.75@0.75) asOffsetLayout |
|
534 |
(0.5@0.5 corner:0.75@0.75) asLayout |
|
535 |
||
536 |
(10@10 corner:20@20) asFractionalLayout |
|
537 |
(10@10 corner:20@20) asOffsetLayout |
|
538 |
(10@10 corner:20@20) asLayout |
|
539 |
" |
|
540 |
||
541 |
! |
|
542 |
||
356 | 543 |
asPointArray |
544 |
"return an array containing my corners (clockwise) and |
|
545 |
the origin again as 5th element. Can be used to convert |
|
546 |
a rectangle into a polygon." |
|
547 |
||
548 |
|org| |
|
549 |
||
550 |
^ Array with:(org := self origin) |
|
551 |
with:self topRight |
|
552 |
with:self corner |
|
553 |
with:self bottomLeft |
|
554 |
with:org |
|
555 |
||
556 |
" |
|
557 |
(10@10 corner:100@100) asPointArray |
|
558 |
" |
|
559 |
! |
|
560 |
||
637 | 561 |
fromLiteralArrayEncoding:encoding |
562 |
"read my values from an encoding. |
|
563 |
The encoding is supposed to be of the form: (Rectangle orgX orgY cornX cornY)" |
|
356 | 564 |
|
637 | 565 |
left := encoding at:2. |
566 |
top := encoding at:3. |
|
567 |
width := (encoding at:4) - left. |
|
568 |
height := (encoding at:5) - top |
|
569 |
||
356 | 570 |
|
571 |
" |
|
637 | 572 |
Rectangle new fromLiteralArrayEncoding:#(Rectangle 100 200 300 500) |
356 | 573 |
" |
379 | 574 |
! |
575 |
||
421 | 576 |
literalArrayEncoding |
577 |
"encode myself as an array. |
|
578 |
The encoding is: (Rectangle orgX orgY cornX cornY)" |
|
579 |
||
580 |
^ Array |
|
581 |
with:#Rectangle |
|
582 |
with:left |
|
583 |
with:top |
|
584 |
with:width |
|
585 |
with:height |
|
586 |
||
587 |
||
588 |
" |
|
589 |
Rectangle new fromLiteralArrayEncoding:#(Rectangle 100 200 300 500) |
|
590 |
(100@200 corner:300@500) literalArrayEncoding |
|
591 |
" |
|
592 |
||
593 |
"Modified: 1.9.1995 / 02:16:54 / claus" |
|
594 |
! |
|
595 |
||
637 | 596 |
rectangleRelativeTo:aRectangle preferred:prefRect |
597 |
"compute a displayRectangle, treating the receiver like a |
|
598 |
layoutorigin. This allows point to be used interchangable with |
|
599 |
LayoutOrigins." |
|
379 | 600 |
|
637 | 601 |
^ (self asLayout) rectangleRelativeTo:aRectangle preferred:prefRect |
379 | 602 |
|
603 |
" |
|
637 | 604 |
(10@20 corner:20@30) rectangleRelativeTo:(0@0 corner:100@100) preferred:(0@0 corner:50@50) |
605 |
||
606 |
(0.5@0.5) rectangleRelativeTo:(0@0 corner:100@100) preferred:(0@0 corner:50@50) |
|
379 | 607 |
" |
356 | 608 |
! ! |
609 |
||
186 | 610 |
!Rectangle methodsFor:'destructive rectangle operations'! |
611 |
||
356 | 612 |
expandBy:delta |
613 |
"destructively expanded the receiver in all directions |
|
614 |
by amount, a Point, Rectangle or Number. |
|
615 |
Warning: this is a destructive operation, modifying the receiver |
|
616 |
NOT returning a copy. You have to be certain to be the exclusive |
|
617 |
owner of the receiver to avoid side effects. See also: #expandedBy:" |
|
618 |
||
619 |
|amountPoint deltaLeft deltaTop deltaWidth deltaHeight| |
|
620 |
||
621 |
delta isNumber ifTrue:[ |
|
622 |
deltaLeft := deltaTop := delta. |
|
623 |
deltaWidth := deltaHeight := delta * 2. |
|
624 |
] ifFalse:[ |
|
625 |
delta isRectangle ifTrue:[ |
|
626 |
deltaLeft := delta left. |
|
627 |
deltaTop := delta top. |
|
628 |
deltaWidth := deltaLeft + delta right. |
|
629 |
deltaHeight := deltaTop + delta bottom |
|
630 |
] ifFalse:[ |
|
631 |
amountPoint := delta asPoint. |
|
632 |
deltaLeft := amountPoint x. |
|
633 |
deltaTop := amountPoint y. |
|
634 |
deltaWidth := deltaLeft * 2. |
|
635 |
deltaHeight := deltaTop * 2. |
|
636 |
] |
|
637 |
]. |
|
638 |
||
639 |
left := (left - deltaLeft). |
|
640 |
top := (top - deltaTop). |
|
641 |
width := (width + deltaWidth). |
|
642 |
height := (height + deltaHeight). |
|
643 |
||
644 |
" |
|
645 |
|r| |
|
646 |
r := Rectangle origin:10@10 corner:100@100. |
|
647 |
r expandBy:5. |
|
648 |
||
649 |
r := Rectangle origin:10@10 corner:100@100. |
|
650 |
r expandBy:(5 @ 0). |
|
651 |
||
652 |
r := Rectangle origin:10@10 corner:100@100. |
|
653 |
r expandBy:(10 @ 10). |
|
654 |
||
655 |
r := Rectangle origin:10@10 corner:100@100. |
|
656 |
r expandBy:( 10@10 corner:20@20 ) |
|
657 |
" |
|
637 | 658 |
! |
659 |
||
660 |
moveBy:aPoint |
|
661 |
"destructively translate the rectangle by some distance. |
|
662 |
sorry for the name inconsistency - but GNU-ST named it that way" |
|
663 |
||
664 |
left := left + aPoint x. |
|
665 |
top := top + aPoint y |
|
666 |
! |
|
667 |
||
668 |
moveTo:aPoint |
|
669 |
"destructively translate the rectangle to have its origin at aPoint." |
|
670 |
||
671 |
|diff| |
|
672 |
||
673 |
diff := aPoint - self origin. |
|
674 |
self origin:aPoint corner:self corner + diff |
|
675 |
! |
|
676 |
||
677 |
scaleBy:scale |
|
678 |
"scale the receiver rectangle by scale (a Number or Point). |
|
679 |
This is destructive (modifies the receiver, not a copy) and |
|
680 |
should only be used if you know, that you are the exclusive owner |
|
681 |
of the receiver. (use scaledBy if in doubt)" |
|
682 |
||
683 |
|scalePoint sx sy| |
|
684 |
||
685 |
(scale isMemberOf:Point) ifTrue:[ "type hint to stc" |
|
686 |
sx := scale x. |
|
687 |
sy := scale y |
|
688 |
] ifFalse:[ |
|
689 |
scalePoint := scale asPoint. |
|
690 |
sx := scalePoint x. |
|
691 |
sy := scalePoint y |
|
692 |
]. |
|
693 |
width := width * sx. |
|
694 |
height := height * sy. |
|
695 |
left := left * sx. |
|
696 |
top := top * sy |
|
697 |
||
698 |
" |
|
699 |
(Rectangle origin:10@10 corner:50@50) scaleBy:2 |
|
700 |
" |
|
701 |
||
702 |
"its destructive:" |
|
703 |
" |
|
704 |
|r1 r2| |
|
705 |
||
706 |
r1 := Rectangle origin:10@10 corner:50@50. |
|
707 |
r2 := r1 scaleBy:2. |
|
708 |
r1 |
|
709 |
" |
|
710 |
! |
|
711 |
||
712 |
translateBy:amount |
|
713 |
"translate (i.e. move) the receiver rectangle |
|
714 |
by amount, a Point or Number. |
|
715 |
This is destructive (modifies the receiver, not a copy) and |
|
716 |
should only be used if you know, that you are the exclusive owner |
|
717 |
of the receiver. (use translatedBy if in doubt)" |
|
718 |
||
719 |
|amountPoint| |
|
720 |
||
721 |
(amount isMemberOf:Point) ifTrue:[ "type hint to stc" |
|
722 |
left := left + amount x. |
|
723 |
top := top + amount y |
|
724 |
] ifFalse:[ |
|
725 |
amountPoint := amount asPoint. |
|
726 |
left := left + amountPoint x. |
|
727 |
top := top + amountPoint y |
|
728 |
] |
|
729 |
||
730 |
" |
|
731 |
(Rectangle origin:10@10 corner:50@50) translateBy:10 |
|
732 |
" |
|
733 |
||
734 |
"its destructive:" |
|
735 |
" |
|
736 |
|r1 r2| |
|
737 |
||
738 |
r1 := Rectangle origin:10@10 corner:50@50. |
|
739 |
r2 := r1 translateBy:10. |
|
740 |
r1 |
|
741 |
" |
|
742 |
! ! |
|
743 |
||
744 |
!Rectangle methodsFor:'displaying'! |
|
745 |
||
746 |
displayFilledOn:aGC |
|
747 |
aGC fillRectangleX:left y:top width:width height:height |
|
748 |
||
749 |
" |
|
750 |
|v| |
|
751 |
||
752 |
v := View new open. |
|
753 |
[v shown] whileFalse:[Processor yield]. |
|
754 |
||
755 |
(Rectangle origin:10@10 corner:50@50) displayFilledOn:v |
|
756 |
" |
|
757 |
! |
|
758 |
||
759 |
displayStrokedOn:aGC |
|
760 |
aGC displayRectangleX:left y:top width:width height:height |
|
761 |
||
762 |
" |
|
763 |
|v| |
|
764 |
||
765 |
v := View new open. |
|
766 |
[v shown] whileFalse:[Processor yield]. |
|
767 |
||
768 |
(Rectangle origin:10@10 corner:50@50) displayStrokedOn:v |
|
769 |
" |
|
770 |
! ! |
|
771 |
||
772 |
!Rectangle methodsFor:'printing & storing'! |
|
773 |
||
774 |
printOn:aStream |
|
775 |
"print the receiver on aStream" |
|
776 |
||
777 |
aStream nextPutAll:(self class name). |
|
778 |
aStream nextPutAll:' origin:'. |
|
779 |
(self origin) printOn:aStream. |
|
780 |
aStream nextPutAll:' corner:'. |
|
781 |
(self corner) printOn:aStream |
|
782 |
! |
|
783 |
||
784 |
storeOn:aStream |
|
785 |
"store the receiver on aStream; i.e. print an expression which will |
|
786 |
reconstruct the receiver" |
|
787 |
||
788 |
aStream nextPut:$(. |
|
789 |
aStream nextPutAll:(self class name). |
|
790 |
aStream nextPutAll:' origin:'. |
|
791 |
(self origin) storeOn:aStream. |
|
792 |
aStream nextPutAll:' corner:'. |
|
793 |
(self corner) storeOn:aStream. |
|
794 |
aStream nextPut:$) |
|
795 |
! ! |
|
796 |
||
797 |
!Rectangle methodsFor:'queries'! |
|
798 |
||
799 |
isRectangle |
|
800 |
"return true, if the receiver is some kind of rectangle" |
|
801 |
||
802 |
^ true |
|
186 | 803 |
! ! |
804 |
||
1 | 805 |
!Rectangle methodsFor:'rectangle operations'! |
806 |
||
807 |
+ aPoint |
|
356 | 808 |
"return a new rectangle with same extent as receiver but |
1 | 809 |
origin translated by the argument, aPoint" |
810 |
||
217 | 811 |
|amountPoint| |
812 |
||
813 |
amountPoint := aPoint asPoint. |
|
814 |
^ Rectangle left:(left + amountPoint x) |
|
815 |
top:(top + amountPoint y) |
|
816 |
width:width |
|
817 |
height:height |
|
1 | 818 |
! |
819 |
||
326 | 820 |
align:offset with:someCoordinate |
821 |
"return a new rectangle which is translated (i.e. moved) |
|
822 |
such that the point offset in mySelf is placed on someCoordinate." |
|
823 |
||
824 |
^ Rectangle origin:(someCoordinate - offset + self origin) |
|
825 |
extent:(self extent) |
|
826 |
" |
|
827 |
|r| |
|
828 |
||
829 |
r := Rectangle origin:10@10 corner:50@50. |
|
830 |
r align:(r center) with:100@100. |
|
831 |
" |
|
832 |
! |
|
833 |
||
1 | 834 |
amountToTranslateWithin: aRectangle |
835 |
"for GNU-ST compatibility" |
|
836 |
||
837 |
^(aRectangle origin) - self origin |
|
838 |
! |
|
839 |
||
840 |
areasOutside: aRectangle |
|
841 |
"---------------------------------------------------------------- |
|
842 |
| added for GNU-ST compatibility |
|
843 |
| |
|
844 |
| author: Doug McCallum <uunet!!ico.isc.com!!dougm> |
|
845 |
| |
|
846 |
|areasOutside: aRectangle |
|
847 |
| most complicated of the Rectangle primitives |
|
848 |
| The basic methodology is to first determine that there is an |
|
849 |
| intersection by finding the overlapping rectangle. From the |
|
850 |
| overlapping rectangle, first determine if it runs along an edge. |
|
851 |
| If it doesn't, extend the rectangle up to the top edge and add |
|
852 |
| the new rectangle to the collection and start the rest of the |
|
853 |
| process. If the left edge does not touch the left edge of self, |
|
854 |
| extend it to the edge saving the new rectangle. Then do the |
|
855 |
| same to the right edge. Then check top and bottom edges. Most |
|
856 |
| of the time only 2 or 3 rectangles get formed, occasionally 4. |
|
857 |
| It should be possible to never get more than 3 but requires more |
|
858 |
| work. |
|
859 |
----------------------------------------------------------------" |
|
860 |
||
861 |
| collect iRect tmp | |
|
360 | 862 |
iRect := self intersect: aRectangle. |
1 | 863 |
(iRect = nil) ifTrue: [^nil]. "case of no intersection" |
186 | 864 |
"the collect collection gathers Rectangles" |
360 | 865 |
collect := OrderedCollection new: 4. |
186 | 866 |
"is it floating or on the edge?" |
1 | 867 |
(((((iRect top) ~= self top) |
186 | 868 |
and: [ (iRect bottom) ~= self bottom ]) |
869 |
and: [ (iRect left) ~= self left ]) |
|
870 |
and: [ (iRect right) ~= self right ] ) |
|
871 |
ifTrue: "entirely in the center." |
|
360 | 872 |
[tmp := Rectangle origin: (Point x: iRect left y: self top) |
873 |
corner: iRect bottomRight. |
|
186 | 874 |
collect add: tmp. |
360 | 875 |
iRect := iRect merge: tmp]. |
1 | 876 |
((iRect left) ~= self left) |
186 | 877 |
ifTrue: "doesn't touch left edge so make it touch" |
360 | 878 |
[tmp := Rectangle origin: (Point x: self left y: iRect top) |
879 |
corner: iRect bottomLeft. |
|
186 | 880 |
collect add: tmp. |
881 |
"merge new (tmp) with overlap to keep track" |
|
360 | 882 |
iRect := iRect merge: tmp]. |
1 | 883 |
((iRect right) ~= self right) |
186 | 884 |
ifTrue: "doesn't touch right edge so extend it" |
360 | 885 |
[tmp := Rectangle origin: iRect topRight |
886 |
corner: (Point x: self right y: iRect bottom). |
|
186 | 887 |
collect add: tmp. |
360 | 888 |
iRect := iRect merge: tmp]. |
77 | 889 |
(((iRect left) ~= self left) or: [(iRect top) ~= self top]) |
186 | 890 |
ifTrue: "whole top part can be taken now" |
360 | 891 |
[tmp := Rectangle origin: self origin corner: iRect topRight. |
186 | 892 |
collect add: tmp]. |
77 | 893 |
(((iRect right) ~= self right) or: [(iRect bottom) ~= self bottom]) |
186 | 894 |
ifTrue: "whole bottom open and can be taken" |
360 | 895 |
[tmp := Rectangle origin: iRect bottomLeft corner: self corner. |
186 | 896 |
collect add: tmp]. |
1 | 897 |
^collect |
77 | 898 |
! |
1 | 899 |
|
637 | 900 |
expandedBy:delta |
901 |
"return a new rectangle which is expanded in all directions |
|
902 |
by amount, a Point, Rectangle or Number" |
|
903 |
||
904 |
^ self copy expandBy:delta |
|
905 |
||
906 |
" |
|
907 |
|r| |
|
908 |
r := Rectangle origin:10@10 corner:100@100. |
|
909 |
r expandedBy:5. |
|
910 |
r expandedBy:(5 @ 0). |
|
911 |
r expandedBy:(10 @ 10). |
|
912 |
r expandedBy:( 10@10 corner:20@20 ) |
|
913 |
" |
|
914 |
! |
|
915 |
||
916 |
insetBy: delta |
|
917 |
"return a new rectangle which is inset in all directions |
|
918 |
by delta, a Point, Rectangle or Number" |
|
919 |
||
920 |
|amountPoint deltaLeft deltaTop deltaWidth deltaHeight| |
|
921 |
||
922 |
delta isNumber ifTrue:[ |
|
923 |
deltaLeft := deltaTop := delta. |
|
924 |
deltaWidth := deltaHeight := delta * 2. |
|
925 |
] ifFalse:[ |
|
926 |
delta isRectangle ifTrue:[ |
|
927 |
deltaLeft := delta left. |
|
928 |
deltaTop := delta top. |
|
929 |
deltaWidth := deltaLeft + delta right. |
|
930 |
deltaHeight := deltaTop + delta bottom |
|
931 |
] ifFalse:[ |
|
932 |
amountPoint := delta asPoint. |
|
933 |
deltaLeft := amountPoint x. |
|
934 |
deltaTop := amountPoint y. |
|
935 |
deltaWidth := deltaLeft * 2. |
|
936 |
deltaHeight := deltaTop * 2. |
|
937 |
] |
|
938 |
]. |
|
939 |
||
940 |
^ Rectangle left:(left + deltaLeft) |
|
941 |
top:(top + deltaTop) |
|
942 |
width:(width - deltaWidth) |
|
943 |
height:(height - deltaHeight) |
|
944 |
" |
|
945 |
|r| |
|
946 |
r := Rectangle origin:10@10 corner:100@100. |
|
947 |
r insetBy:5. |
|
948 |
r insetBy:(5 @ 0). |
|
949 |
r insetBy:(10 @ 10). |
|
950 |
r insetBy:( 10@10 corner:20@20 ) |
|
951 |
" |
|
952 |
! |
|
953 |
||
954 |
insetOriginBy:originDelta cornerBy:cornerDelta |
|
955 |
"return a new rectangle which is inset by originDelta |
|
956 |
and cornerDelta; both may be instances of Point or Number" |
|
957 |
||
958 |
|amountPoint deltaLeft deltaTop deltaWidth deltaHeight| |
|
959 |
||
960 |
^ Rectangle |
|
961 |
origin:(left @ top) + originDelta asPoint |
|
962 |
corner:(self corner - cornerDelta asPoint) |
|
963 |
" |
|
964 |
|r| |
|
965 |
r := Rectangle origin:10@10 corner:100@100. |
|
966 |
r insetOriginBy:5 cornerBy:10. |
|
967 |
r insetOriginBy:10@5 cornerBy:10. |
|
968 |
r insetOriginBy:10 cornerBy:10@5. |
|
969 |
r insetOriginBy:10@10 cornerBy:20@20. |
|
970 |
" |
|
971 |
! |
|
972 |
||
973 |
intersect:aRectangle |
|
974 |
"return a new rectangle covering the intersection of the receiver |
|
975 |
and the argument, aRectangle. |
|
976 |
the rectangles must intersect for a valid return" |
|
977 |
||
978 |
^ Rectangle left:(left max:(aRectangle left)) |
|
979 |
right:((left + width) min:(aRectangle right)) |
|
980 |
top:(top max:(aRectangle top)) |
|
981 |
bottom:((top + height) min:(aRectangle bottom)) |
|
982 |
||
983 |
" |
|
984 |
|r1 r2| |
|
985 |
||
986 |
r1 := Rectangle origin:10@10 corner:100@100. |
|
987 |
r2 := Rectangle origin:50@50 corner:150@75. |
|
988 |
r1 intersect:r2 |
|
989 |
" |
|
990 |
! |
|
991 |
||
992 |
merge:aRectangle |
|
993 |
"return a new rectangle covering both the receiver |
|
994 |
and the argument, aRectangle" |
|
995 |
||
996 |
^ Rectangle left:(left min:(aRectangle left)) |
|
997 |
right:((left + width) max:(aRectangle right)) |
|
998 |
top:(top min:(aRectangle top)) |
|
999 |
bottom:((top + height) max:(aRectangle bottom)) |
|
1000 |
! |
|
1001 |
||
77 | 1002 |
nonIntersections:aRectangle |
1003 |
"this is the same as areasOutside: - for ST/V compatibility only" |
|
1004 |
||
1005 |
^ self areasOutside:aRectangle |
|
637 | 1006 |
! |
1007 |
||
1008 |
rounded |
|
1009 |
"return a copy of the receiver with rounded coordinates" |
|
1 | 1010 |
|
637 | 1011 |
^ Rectangle left:(left rounded) |
1012 |
top:(top rounded) |
|
1013 |
width:(width rounded) |
|
1014 |
height:(height rounded) |
|
1015 |
! |
|
356 | 1016 |
|
637 | 1017 |
scaledBy:scale |
1018 |
"return a new rectangle which is the receiver |
|
1019 |
scaled by scale" |
|
1020 |
||
1021 |
|scalePoint sx sy| |
|
356 | 1022 |
|
637 | 1023 |
scalePoint := scale asPoint. |
1024 |
sx := scalePoint x. |
|
1025 |
sy := scalePoint y. |
|
1026 |
^ Rectangle left:left * sx |
|
1027 |
top:top * sy |
|
1028 |
width:(width * sx) |
|
1029 |
height:(height * sy) |
|
356 | 1030 |
" |
637 | 1031 |
(Rectangle origin:10@10 corner:50@50) scaledBy:2 |
1032 |
" |
|
356 | 1033 |
|
637 | 1034 |
"its NOT destructive:" |
1035 |
" |
|
1036 |
|r1 r2| |
|
356 | 1037 |
|
637 | 1038 |
r1 := Rectangle origin:10@10 corner:50@50. |
1039 |
r2 := r1 scaledBy:2. |
|
1040 |
r1 |
|
356 | 1041 |
" |
1042 |
! |
|
1043 |
||
637 | 1044 |
translatedBy:amount |
1045 |
"return a new rectangle which is translated (i.e. moved) |
|
1046 |
by amount, aPoint or Number" |
|
1047 |
||
1048 |
|amountPoint| |
|
356 | 1049 |
|
637 | 1050 |
amountPoint := amount asPoint. |
1051 |
^ Rectangle left:(left + amountPoint x) |
|
1052 |
top:(top + amountPoint y) |
|
1053 |
width:width |
|
1054 |
height:height |
|
356 | 1055 |
" |
637 | 1056 |
(Rectangle origin:10@10 corner:50@50) translatedBy:10 |
1057 |
" |
|
356 | 1058 |
|
637 | 1059 |
"its NOT destructive:" |
1060 |
" |
|
1061 |
|r1 r2| |
|
356 | 1062 |
|
637 | 1063 |
r1 := Rectangle origin:10@10 corner:50@50. |
1064 |
r2 := r1 translatedBy:10. |
|
1065 |
r1 |
|
356 | 1066 |
" |
1067 |
! ! |
|
1068 |
||
637 | 1069 |
!Rectangle methodsFor:'testing'! |
1 | 1070 |
|
637 | 1071 |
contains:aRectangle |
1072 |
"return true, if the argument, aRectangle is equal to or |
|
1073 |
is contained fully within the receiver" |
|
1 | 1074 |
|
637 | 1075 |
(left <= aRectangle left) ifTrue:[ |
1076 |
((left + width) >= aRectangle right) ifTrue:[ |
|
1077 |
(top <= aRectangle top) ifTrue:[ |
|
1078 |
((top + height) >= aRectangle bottom) ifTrue:[ |
|
1079 |
^ true |
|
1080 |
] |
|
1081 |
] |
|
1082 |
] |
|
1083 |
]. |
|
1084 |
^ false |
|
1085 |
! |
|
1086 |
||
1087 |
containsPoint:aPoint |
|
1088 |
"return true, if the argument, aPoint is contained in the receiver" |
|
1089 |
||
1090 |
|px py| |
|
1091 |
||
1092 |
px := aPoint x. |
|
1093 |
(px < left) ifTrue:[^ false]. |
|
1094 |
(px > (left + width)) ifTrue:[^ false]. |
|
1095 |
py := aPoint y. |
|
1096 |
(py < top) ifTrue:[^ false]. |
|
1097 |
(py > (top + height)) ifTrue:[^ false]. |
|
1098 |
^ true |
|
1 | 1099 |
! |
1100 |
||
637 | 1101 |
containsPointX:x y:y |
1102 |
"return true, if the point defined by x@y is contained in the receiver. |
|
1103 |
This is the same as containsPoint:, but can be used if the coordinates |
|
1104 |
are already available as separate numbers to avoid useless creation of a |
|
1105 |
temporary point." |
|
1106 |
||
1107 |
(x < left) ifTrue:[^ false]. |
|
1108 |
(x > (left + width)) ifTrue:[^ false]. |
|
1109 |
(y < top) ifTrue:[^ false]. |
|
1110 |
(y > (top + height)) ifTrue:[^ false]. |
|
1111 |
^ true |
|
1112 |
! |
|
1113 |
||
1114 |
intersects:aRectangle |
|
1115 |
"return true, if the intersection between the argument, aRectangle |
|
1116 |
and the receiver is not empty" |
|
1117 |
||
1118 |
|b r| |
|
1 | 1119 |
|
637 | 1120 |
(aRectangle right) < left ifTrue:[^ false]. |
1121 |
(aRectangle bottom) < top ifTrue:[^ false]. |
|
1122 |
r := left + width. |
|
1123 |
(aRectangle left) > r ifTrue:[^ false]. |
|
1124 |
b := top + height. |
|
1125 |
(aRectangle top) > b ifTrue:[^ false]. |
|
1126 |
^ true |
|
1127 |
! |
|
1128 |
||
1129 |
isContainedIn:aRectangle |
|
1130 |
"return true, if the receiver is fully contained within |
|
1131 |
the argument, aRectangle" |
|
1132 |
||
1133 |
(aRectangle left <= left) ifTrue:[ |
|
1134 |
(aRectangle right >= (left + width)) ifTrue:[ |
|
1135 |
(aRectangle top <= top) ifTrue:[ |
|
1136 |
(aRectangle bottom >= (top + height)) ifTrue:[ |
|
1137 |
^ true |
|
1138 |
] |
|
1139 |
] |
|
1140 |
] |
|
1141 |
]. |
|
1142 |
^ false |
|
1 | 1143 |
! ! |
637 | 1144 |
|
1145 |
!Rectangle class methodsFor:'documentation'! |
|
1146 |
||
1147 |
version |
|
829
fc386319f41c
removed external function defs (soon no longer allowed)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1148 |
^ '$Header: /cvs/stx/stx/libbasic/Rectangle.st,v 1.29 1996-01-04 01:23:33 cg Exp $' |
637 | 1149 |
! ! |