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