author | Claus Gittinger <cg@exept.de> |
Wed, 12 Feb 1997 13:23:49 +0100 | |
changeset 2381 | 10af98758a49 |
parent 2373 | e5bc707209cd |
child 2426 | 3b04313af90e |
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 |
1149
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
14 |
instanceVariableNames:'left top width height' |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
15 |
classVariableNames:'' |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
16 |
poolDictionaries:'' |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
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 |
||
1290 | 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. |
|
1290 | 57 |
|
58 |
[author:] |
|
59 |
Claus Gittinger |
|
60 |
||
1314 | 61 |
[see also:] |
1355 | 62 |
Point Polygon Circle EllipticalArc Spline |
1314 | 63 |
LayoutOrigin LayoutFrame AlignmentOrigin Layout |
1390 | 64 |
View GraphicsContext StrokingWrapper FillingWrapper |
77 | 65 |
" |
66 |
! ! |
|
67 |
||
1 | 68 |
!Rectangle class methodsFor:'instance creation'! |
69 |
||
637 | 70 |
fromUser |
71 |
"let user specify a rectangle on the screen, return it" |
|
72 |
||
73 |
^ Screen current rectangleFromUser |
|
74 |
||
75 |
" |
|
76 |
Rectangle fromUser |
|
77 |
" |
|
78 |
! |
|
79 |
||
80 |
left:left right:right top:top bottom:bottom |
|
81 |
"create and return a new Rectangle giving left, top, right and bottom coordinates" |
|
82 |
||
83 |
^ (self basicNew) left:left right:right top:top bottom:bottom |
|
84 |
! |
|
85 |
||
1356 | 86 |
left:left top:top extent:extent |
87 |
"create and return a new Rectangle giving left, top, and |
|
88 |
an extent point." |
|
89 |
||
90 |
^ (self basicNew) left:left top:top extent:extent |
|
91 |
||
92 |
"Created: 8.5.1996 / 20:56:29 / cg" |
|
93 |
! |
|
94 |
||
637 | 95 |
left:left top:top right:right bottom:bottom |
96 |
"create and return a new Rectangle giving left, top, right and bottom coordinates" |
|
97 |
||
98 |
^ (self basicNew) left:left top:top right:right bottom:bottom |
|
99 |
! |
|
100 |
||
101 |
left:left top:top width:w height:h |
|
102 |
"create and return a new Rectangle giving left and top coordinates |
|
103 |
and width, height" |
|
104 |
||
105 |
%{ /* NOCONTEXT */ |
|
106 |
REGISTER OBJ newRect; |
|
107 |
OBJ t; |
|
108 |
int spc; |
|
109 |
||
835 | 110 |
if (__CanDoQuickNew(OHDR_SIZE + 4*sizeof(OBJ))) { |
637 | 111 |
if (self == Rectangle) { |
835 | 112 |
/* |
113 |
* short cut - rectangles are created so often ... |
|
114 |
*/ |
|
115 |
__qCheckedAlignedNew(newRect, OHDR_SIZE + 4*sizeof(OBJ)); |
|
637 | 116 |
|
1133 | 117 |
__InstPtr(newRect)->o_class = Rectangle; |
118 |
__InstPtr(newRect)->i_instvars[0] = left; |
|
119 |
__InstPtr(newRect)->i_instvars[1] = top; |
|
120 |
__InstPtr(newRect)->i_instvars[2] = w; |
|
121 |
__InstPtr(newRect)->i_instvars[3] = h; |
|
637 | 122 |
|
123 |
if (! (__bothSmallInteger(left, top) && |
|
124 |
__bothSmallInteger(w, h))) { |
|
125 |
spc = __qSpace(newRect); |
|
126 |
__STORE_SPC(newRect, left, spc); |
|
127 |
__STORE_SPC(newRect, top, spc); |
|
128 |
__STORE_SPC(newRect, w, spc); |
|
129 |
__STORE_SPC(newRect, h, spc); |
|
130 |
} |
|
131 |
||
132 |
RETURN ( newRect ); |
|
133 |
} |
|
134 |
} |
|
1507 | 135 |
%}. |
637 | 136 |
^ (self basicNew) left:left top:top width:w height:h |
137 |
! |
|
138 |
||
1 | 139 |
origin:origin corner:corner |
140 |
"create and return a new Rectangle giving top-left and bottom-right points" |
|
141 |
||
142 |
^ (self basicNew) origin:origin corner:corner |
|
143 |
! |
|
144 |
||
145 |
origin:origin extent:extent |
|
146 |
"create and return a new Rectangle giving top-left point and extent point" |
|
147 |
||
148 |
%{ /* NOCONTEXT */ |
|
59 | 149 |
REGISTER OBJ newRect; |
293 | 150 |
OBJ t; |
151 |
int spc; |
|
1 | 152 |
|
835 | 153 |
if (__CanDoQuickNew(OHDR_SIZE + 4*sizeof(OBJ))) { |
154 |
/* |
|
155 |
* short cut - rectangles are created so often ... |
|
156 |
*/ |
|
186 | 157 |
if (self == Rectangle) { |
158 |
if (__isPoint(origin) && __isPoint(extent)) { |
|
835 | 159 |
__qCheckedAlignedNew(newRect, OHDR_SIZE + 4*sizeof(OBJ)); |
1133 | 160 |
__InstPtr(newRect)->o_class = Rectangle; |
325 | 161 |
spc = __qSpace(newRect); |
59 | 162 |
|
1133 | 163 |
t = __PointInstPtr(origin)->p_x; |
164 |
__InstPtr(newRect)->i_instvars[0] = t; |
|
293 | 165 |
__STORE_SPC(newRect, t, spc); |
166 |
||
1133 | 167 |
t = __PointInstPtr(origin)->p_y; |
168 |
__InstPtr(newRect)->i_instvars[1] = t; |
|
293 | 169 |
__STORE_SPC(newRect, t, spc); |
59 | 170 |
|
1133 | 171 |
t = __PointInstPtr(extent)->p_x; |
172 |
__InstPtr(newRect)->i_instvars[2] = t; |
|
293 | 173 |
__STORE_SPC(newRect, t, spc); |
174 |
||
1133 | 175 |
t = __PointInstPtr(extent)->p_y; |
176 |
__InstPtr(newRect)->i_instvars[3] = t; |
|
293 | 177 |
__STORE_SPC(newRect, t, spc); |
186 | 178 |
RETURN ( newRect ); |
179 |
} |
|
180 |
} |
|
1 | 181 |
} |
1507 | 182 |
%}. |
1 | 183 |
^ (self basicNew) origin:origin extent:extent |
1356 | 184 |
! |
185 |
||
186 |
origin:origin width:w height:h |
|
187 |
"create and return a new Rectangle giving top-left and extent |
|
188 |
as individual width/height" |
|
189 |
||
190 |
^ (self basicNew) origin:origin width:w height:h |
|
191 |
||
192 |
"Created: 8.5.1996 / 20:55:53 / cg" |
|
2372
9a98e1acd063
added #vertex:vertex: - ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2274
diff
changeset
|
193 |
! |
9a98e1acd063
added #vertex:vertex: - ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2274
diff
changeset
|
194 |
|
9a98e1acd063
added #vertex:vertex: - ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2274
diff
changeset
|
195 |
vertex:vertex1Point vertex:vertex2Point |
9a98e1acd063
added #vertex:vertex: - ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2274
diff
changeset
|
196 |
"create and return a new instance of the receiver, |
9a98e1acd063
added #vertex:vertex: - ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2274
diff
changeset
|
197 |
given two diagonally opposite vertices." |
9a98e1acd063
added #vertex:vertex: - ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2274
diff
changeset
|
198 |
|
9a98e1acd063
added #vertex:vertex: - ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2274
diff
changeset
|
199 |
^ self |
9a98e1acd063
added #vertex:vertex: - ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2274
diff
changeset
|
200 |
origin: (vertex1Point min: vertex2Point) |
9a98e1acd063
added #vertex:vertex: - ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2274
diff
changeset
|
201 |
corner: (vertex1Point max: vertex2Point) |
9a98e1acd063
added #vertex:vertex: - ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2274
diff
changeset
|
202 |
|
9a98e1acd063
added #vertex:vertex: - ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2274
diff
changeset
|
203 |
"Created: 10.2.1997 / 12:14:32 / cg" |
1 | 204 |
! ! |
205 |
||
206 |
!Rectangle methodsFor:'accessing'! |
|
207 |
||
637 | 208 |
area |
209 |
"return the area |
|
210 |
- for screen Rectangles this is the number of pixels" |
|
211 |
||
212 |
^ width * height |
|
213 |
! |
|
214 |
||
215 |
bottom |
|
216 |
"return the y coordinate of the bottom" |
|
217 |
||
218 |
^ (top + height) |
|
219 |
! |
|
220 |
||
221 |
bottom:aNumber |
|
222 |
"set the bottom edge" |
|
223 |
||
224 |
height := aNumber - top |
|
225 |
! |
|
226 |
||
227 |
bottomCenter |
|
228 |
"return the bottom center point" |
|
229 |
||
1453
eb73ecdb25ab
center methods should not use //
Claus Gittinger <cg@exept.de>
parents:
1443
diff
changeset
|
230 |
^ (left + (width / 2)) @ (top + height) |
eb73ecdb25ab
center methods should not use //
Claus Gittinger <cg@exept.de>
parents:
1443
diff
changeset
|
231 |
|
eb73ecdb25ab
center methods should not use //
Claus Gittinger <cg@exept.de>
parents:
1443
diff
changeset
|
232 |
"Modified: 4.6.1996 / 16:04:08 / cg" |
637 | 233 |
! |
234 |
||
235 |
bottomLeft |
|
236 |
"return the bottom-left point" |
|
237 |
||
238 |
^ left @ (top + height) |
|
239 |
! |
|
240 |
||
241 |
bottomRight |
|
242 |
"return the bottom-right point" |
|
243 |
||
244 |
^ (left + width) @ (top + height) |
|
245 |
! |
|
246 |
||
247 |
center |
|
248 |
"return the point in the center of the receiver" |
|
249 |
||
1453
eb73ecdb25ab
center methods should not use //
Claus Gittinger <cg@exept.de>
parents:
1443
diff
changeset
|
250 |
^ (left + (width / 2)) @ (top + (height / 2)) |
eb73ecdb25ab
center methods should not use //
Claus Gittinger <cg@exept.de>
parents:
1443
diff
changeset
|
251 |
|
eb73ecdb25ab
center methods should not use //
Claus Gittinger <cg@exept.de>
parents:
1443
diff
changeset
|
252 |
"Modified: 4.6.1996 / 16:03:53 / cg" |
637 | 253 |
! |
254 |
||
255 |
corner |
|
256 |
"return the corner" |
|
257 |
||
258 |
^ (left + width) @ (top + height) |
|
259 |
! |
|
260 |
||
261 |
corner:aPoint |
|
262 |
"set the bottom-right corner" |
|
263 |
||
264 |
width := aPoint x - left. |
|
265 |
height := aPoint y - top |
|
266 |
! |
|
267 |
||
268 |
extent |
|
269 |
"return the extent" |
|
270 |
||
271 |
^ width @ height |
|
272 |
! |
|
273 |
||
274 |
extent:aPoint |
|
275 |
"set the extent from the argument, aPoint with width taken from aPoint x |
|
276 |
and height taken from aPoint y" |
|
277 |
||
278 |
width := aPoint x. |
|
279 |
height := aPoint y |
|
280 |
! |
|
281 |
||
282 |
height |
|
283 |
"return the height of the rectangle" |
|
284 |
||
285 |
^ height |
|
286 |
! |
|
287 |
||
288 |
left |
|
289 |
"return the x-coordinate of the top-left origin" |
|
290 |
||
291 |
^ left |
|
292 |
! |
|
293 |
||
294 |
left:aNumber |
|
295 |
"set the left edge" |
|
296 |
||
297 |
width := width + (left - aNumber). |
|
298 |
left := aNumber |
|
299 |
! |
|
300 |
||
1 | 301 |
left:newLeft right:right top:newTop bottom:bottom |
77 | 302 |
"set the rectangle given left, top, right and bottom coordinates." |
1 | 303 |
|
304 |
left := newLeft. |
|
305 |
top := newTop. |
|
306 |
width := right - left. |
|
307 |
height := bottom - top |
|
308 |
! |
|
309 |
||
1356 | 310 |
left:newLeft top:newTop extent:extent |
311 |
"set the rectangle given left, top, coordinates and an extent" |
|
312 |
||
313 |
left := newLeft. |
|
314 |
top := newTop. |
|
315 |
width := extent x. |
|
316 |
height := extent y |
|
317 |
||
318 |
"Created: 8.5.1996 / 20:55:10 / cg" |
|
319 |
! |
|
320 |
||
1 | 321 |
left:newLeft top:newTop right:right bottom:bottom |
322 |
"set the rectangle given left, top, right and bottom coordinates" |
|
323 |
||
324 |
left := newLeft. |
|
325 |
top := newTop. |
|
326 |
width := right - left. |
|
327 |
height := bottom - top |
|
328 |
! |
|
329 |
||
330 |
left:newLeft top:newTop width:newWidth height:newHeight |
|
331 |
"set the rectangle given left, top coordinates and width, height" |
|
332 |
||
333 |
left := newLeft. |
|
334 |
top := newTop. |
|
335 |
width := newWidth. |
|
336 |
height := newHeight |
|
337 |
! |
|
338 |
||
637 | 339 |
leftCenter |
340 |
"return the left center point" |
|
341 |
||
1453
eb73ecdb25ab
center methods should not use //
Claus Gittinger <cg@exept.de>
parents:
1443
diff
changeset
|
342 |
^ left @ (top + (height / 2)) |
eb73ecdb25ab
center methods should not use //
Claus Gittinger <cg@exept.de>
parents:
1443
diff
changeset
|
343 |
|
eb73ecdb25ab
center methods should not use //
Claus Gittinger <cg@exept.de>
parents:
1443
diff
changeset
|
344 |
"Modified: 4.6.1996 / 16:04:01 / cg" |
637 | 345 |
! |
346 |
||
347 |
origin |
|
348 |
"return the origin" |
|
349 |
||
350 |
^ left @ top |
|
351 |
! |
|
352 |
||
1 | 353 |
origin:aPoint |
77 | 354 |
"set the top-left origin. The corner remains unchanged." |
355 |
||
356 |
|newTop newLeft| |
|
1 | 357 |
|
77 | 358 |
newTop := aPoint y. |
359 |
newLeft := aPoint x. |
|
360 |
height := height + (top - newTop). |
|
361 |
width := width + (left - newLeft). |
|
362 |
left := newLeft. |
|
363 |
top := newTop |
|
1 | 364 |
! |
365 |
||
637 | 366 |
origin:origin corner:corner |
367 |
"set both origin and corner" |
|
368 |
||
369 |
left := origin x. |
|
370 |
top := origin y. |
|
371 |
width := corner x - left. |
|
372 |
height := corner y - top |
|
373 |
! |
|
374 |
||
375 |
origin:origin extent:extent |
|
376 |
"set both origin and extent" |
|
377 |
||
378 |
left := origin x. |
|
379 |
top := origin y. |
|
380 |
width := extent x. |
|
381 |
height := extent y |
|
382 |
! |
|
1 | 383 |
|
1356 | 384 |
origin:origin width:w height:h |
385 |
"set both origin and extent; |
|
386 |
the extent is given as individual width and height" |
|
387 |
||
388 |
left := origin x. |
|
389 |
top := origin y. |
|
390 |
width := w. |
|
391 |
height := h |
|
392 |
||
393 |
"Created: 8.5.1996 / 20:53:53 / cg" |
|
394 |
! |
|
395 |
||
637 | 396 |
right |
397 |
"return the x coordinate of the right" |
|
398 |
||
399 |
^ (left + width) |
|
400 |
! |
|
401 |
||
402 |
right:aNumber |
|
403 |
"set the right edge" |
|
404 |
||
405 |
width := aNumber - left |
|
406 |
! |
|
407 |
||
408 |
rightCenter |
|
409 |
"return the right center point" |
|
410 |
||
1453
eb73ecdb25ab
center methods should not use //
Claus Gittinger <cg@exept.de>
parents:
1443
diff
changeset
|
411 |
^ (left + width) @ (top + (height / 2)) |
eb73ecdb25ab
center methods should not use //
Claus Gittinger <cg@exept.de>
parents:
1443
diff
changeset
|
412 |
|
eb73ecdb25ab
center methods should not use //
Claus Gittinger <cg@exept.de>
parents:
1443
diff
changeset
|
413 |
"Modified: 4.6.1996 / 16:09:05 / cg" |
637 | 414 |
! |
415 |
||
416 |
top |
|
417 |
"return the y-coordinate of the top-left" |
|
418 |
||
419 |
^ top |
|
1 | 420 |
! |
421 |
||
422 |
top:aNumber |
|
423 |
"set the top edge" |
|
424 |
||
425 |
height := height + (top - aNumber). |
|
426 |
top := aNumber |
|
427 |
! |
|
428 |
||
637 | 429 |
topCenter |
430 |
"return the top center point" |
|
1 | 431 |
|
1453
eb73ecdb25ab
center methods should not use //
Claus Gittinger <cg@exept.de>
parents:
1443
diff
changeset
|
432 |
^ (left + (width / 2)) @ top |
eb73ecdb25ab
center methods should not use //
Claus Gittinger <cg@exept.de>
parents:
1443
diff
changeset
|
433 |
|
eb73ecdb25ab
center methods should not use //
Claus Gittinger <cg@exept.de>
parents:
1443
diff
changeset
|
434 |
"Modified: 4.6.1996 / 16:09:09 / cg" |
1 | 435 |
! |
436 |
||
637 | 437 |
topLeft |
438 |
"return the top-left point - the same as origin" |
|
77 | 439 |
|
637 | 440 |
^ left @ top |
77 | 441 |
! |
442 |
||
443 |
topLeft:aPoint |
|
444 |
"Set the top and left edges. |
|
445 |
The bottom right remains unchanged." |
|
446 |
||
447 |
|newTop| |
|
448 |
||
449 |
newTop := aPoint y. |
|
450 |
height := height + (top - newTop). |
|
451 |
top := newTop. |
|
452 |
width := aPoint x - left |
|
453 |
! |
|
454 |
||
637 | 455 |
topRight |
456 |
"return the top-right point" |
|
457 |
||
458 |
^ (left + width) @ top |
|
459 |
! |
|
460 |
||
77 | 461 |
topRight:aPoint |
462 |
"Set the top and right edges. |
|
463 |
The bottom left remains unchanged." |
|
464 |
||
465 |
|newTop| |
|
466 |
||
467 |
newTop := aPoint y. |
|
468 |
height := height + (top - newTop). |
|
469 |
top := newTop. |
|
470 |
width := aPoint x - left |
|
471 |
! |
|
472 |
||
473 |
width |
|
474 |
"return the width of the rectangle" |
|
475 |
||
476 |
^ width |
|
1 | 477 |
! ! |
478 |
||
479 |
!Rectangle methodsFor:'comparing'! |
|
480 |
||
481 |
= aRectangle |
|
482 |
"return true, if the argument aRectangle represents the same |
|
483 |
rectangle as the receiver" |
|
484 |
||
485 |
%{ /* NOCONTEXT */ |
|
59 | 486 |
/* |
1151 | 487 |
* because rectangles are often compared in the graphics code, |
59 | 488 |
* handle the common case quickly |
489 |
*/ |
|
329 | 490 |
if (__isNonNilObject(aRectangle) |
491 |
&& __qClass(aRectangle) == Rectangle) { |
|
1133 | 492 |
if ((__InstPtr(self)->i_instvars[0] == __InstPtr(aRectangle)->i_instvars[0]) |
493 |
&& (__InstPtr(self)->i_instvars[1] == __InstPtr(aRectangle)->i_instvars[1]) |
|
494 |
&& (__InstPtr(self)->i_instvars[2] == __InstPtr(aRectangle)->i_instvars[2]) |
|
495 |
&& (__InstPtr(self)->i_instvars[3] == __InstPtr(aRectangle)->i_instvars[3])) { |
|
186 | 496 |
RETURN ( true ); |
497 |
} |
|
1133 | 498 |
if (__bothSmallInteger(__InstPtr(self)->i_instvars[0], __InstPtr(aRectangle)->i_instvars[0]) |
499 |
&& __bothSmallInteger(__InstPtr(self)->i_instvars[1], __InstPtr(aRectangle)->i_instvars[1]) |
|
500 |
&& __bothSmallInteger(__InstPtr(self)->i_instvars[2], __InstPtr(aRectangle)->i_instvars[2]) |
|
501 |
&& __bothSmallInteger(__InstPtr(self)->i_instvars[3], __InstPtr(aRectangle)->i_instvars[3])) { |
|
325 | 502 |
RETURN ( false ); |
503 |
} |
|
1 | 504 |
} |
1151 | 505 |
%}. |
259 | 506 |
(aRectangle species ~~ self species) ifTrue:[^ false]. |
1 | 507 |
|
508 |
left = aRectangle left ifFalse:[^ false]. |
|
509 |
top = aRectangle top ifFalse:[^ false]. |
|
510 |
width = aRectangle width ifFalse:[^ false]. |
|
511 |
height = aRectangle height ifFalse:[^ false]. |
|
512 |
^ true |
|
59 | 513 |
! |
514 |
||
515 |
hash |
|
516 |
"return an integer useful for hashing - |
|
517 |
redefined since = is redefined here" |
|
518 |
||
519 |
^ ((left hash bitShift:16) bitXor:(top hash bitShift:16)) |
|
520 |
+ ((width hash) bitXor:(height hash)) |
|
1 | 521 |
! ! |
522 |
||
356 | 523 |
!Rectangle methodsFor:'converting'! |
524 |
||
525 |
asFractionalLayout |
|
526 |
"return a layoutFrame in which fractions (top, left, bottom, right) |
|
527 |
are taken from corresponding edges of the receiver. |
|
528 |
You have to make certain that those are in 0..1." |
|
529 |
||
530 |
|l| |
|
531 |
||
532 |
l := LayoutFrame new. |
|
533 |
l |
|
534 |
leftFraction:(self left); |
|
535 |
rightFraction:(self right); |
|
536 |
topFraction:(self top); |
|
537 |
bottomFraction:(self bottom). |
|
538 |
^ l |
|
539 |
||
540 |
" |
|
541 |
(0.5@0.5 corner:0.75@0.75) asFractionalLayout |
|
542 |
(0.5@0.5 corner:0.75@0.75) asOffsetLayout |
|
543 |
(0.5@0.5 corner:0.75@0.75) asLayout |
|
544 |
" |
|
545 |
! |
|
546 |
||
547 |
asLayout |
|
548 |
"return a layoutFrame in which offsets (top, left, bottom, right) |
|
549 |
are taken from corresponding edges of the receiver. |
|
1151 | 550 |
If all values are between 0.0 .. 1.0, a fractionalLayout is created, |
356 | 551 |
otherwise, an offsetLayout" |
552 |
||
1454 | 553 |
|newLayout l r t b| |
356 | 554 |
|
1151 | 555 |
newLayout := LayoutFrame new. |
1454 | 556 |
l := left. |
557 |
r := (self right). |
|
558 |
t := top. |
|
559 |
b := (self bottom). |
|
560 |
((l between:0.0 and:1.0) |
|
561 |
and:[(r between:0.0 and:1.0) |
|
562 |
and:[(t between:0.0 and:1.0) |
|
563 |
and:[(b between:0.0 and:1.0)]]]) ifTrue:[ |
|
564 |
newLayout |
|
565 |
leftFraction:l; |
|
566 |
rightFraction:r; |
|
567 |
topFraction:t; |
|
568 |
bottomFraction:b. |
|
356 | 569 |
] ifFalse:[ |
1454 | 570 |
newLayout |
571 |
leftOffset:l; |
|
572 |
rightFraction:0 offset:r; |
|
573 |
topOffset:t; |
|
574 |
bottomFraction:0 offset:b. |
|
356 | 575 |
]. |
1151 | 576 |
^ newLayout |
356 | 577 |
|
578 |
" |
|
579 |
(0.5@0.5 corner:0.75@0.75) asFractionalLayout |
|
580 |
(0.5@0.5 corner:0.75@0.75) asOffsetLayout |
|
581 |
(0.5@0.5 corner:0.75@0.75) asLayout |
|
582 |
(0@0 corner:1@1) asLayout |
|
583 |
(0@0 corner:1@1) asFractionalLayout |
|
584 |
(0@0 corner:1@1) asOffsetLayout |
|
585 |
" |
|
1454 | 586 |
|
587 |
"Modified: 5.6.1996 / 00:45:46 / cg" |
|
356 | 588 |
! |
589 |
||
637 | 590 |
asOffsetLayout |
591 |
"return a layoutFrame in which offsets (top, left, bottom, right) |
|
592 |
are taken from corresponding edges of the receiver. |
|
593 |
You have to make certain that those are in 0..1." |
|
594 |
||
1151 | 595 |
|newLayout| |
637 | 596 |
|
1151 | 597 |
newLayout := LayoutFrame new. |
598 |
newLayout |
|
637 | 599 |
leftOffset:(self left); |
600 |
rightFraction:0 offset:(self right); |
|
601 |
topOffset:(self top); |
|
602 |
bottomFraction:0 offset:(self bottom). |
|
1151 | 603 |
^ newLayout |
637 | 604 |
|
605 |
" |
|
606 |
(0.5@0.5 corner:0.75@0.75) asFractionalLayout |
|
607 |
(0.5@0.5 corner:0.75@0.75) asOffsetLayout |
|
608 |
(0.5@0.5 corner:0.75@0.75) asLayout |
|
609 |
||
610 |
(10@10 corner:20@20) asFractionalLayout |
|
611 |
(10@10 corner:20@20) asOffsetLayout |
|
612 |
(10@10 corner:20@20) asLayout |
|
613 |
" |
|
614 |
||
615 |
! |
|
616 |
||
356 | 617 |
asPointArray |
618 |
"return an array containing my corners (clockwise) and |
|
619 |
the origin again as 5th element. Can be used to convert |
|
620 |
a rectangle into a polygon." |
|
621 |
||
622 |
|org| |
|
623 |
||
624 |
^ Array with:(org := self origin) |
|
625 |
with:self topRight |
|
626 |
with:self corner |
|
627 |
with:self bottomLeft |
|
628 |
with:org |
|
629 |
||
630 |
" |
|
631 |
(10@10 corner:100@100) asPointArray |
|
632 |
" |
|
633 |
! |
|
634 |
||
1354 | 635 |
asPolygon |
636 |
"return a polygon from the receiver" |
|
637 |
||
638 |
^ Polygon vertices:self asPointArray |
|
639 |
||
640 |
" |
|
641 |
(10@10 corner:100@100) asPolygon |
|
642 |
" |
|
643 |
||
644 |
"Modified: 8.5.1996 / 20:14:44 / cg" |
|
645 |
! |
|
646 |
||
637 | 647 |
fromLiteralArrayEncoding:encoding |
648 |
"read my values from an encoding. |
|
649 |
The encoding is supposed to be of the form: (Rectangle orgX orgY cornX cornY)" |
|
356 | 650 |
|
637 | 651 |
left := encoding at:2. |
652 |
top := encoding at:3. |
|
653 |
width := (encoding at:4) - left. |
|
654 |
height := (encoding at:5) - top |
|
655 |
||
356 | 656 |
|
657 |
" |
|
637 | 658 |
Rectangle new fromLiteralArrayEncoding:#(Rectangle 100 200 300 500) |
356 | 659 |
" |
379 | 660 |
! |
661 |
||
421 | 662 |
literalArrayEncoding |
1245 | 663 |
"encode myself as an array, from which a copy of the receiver |
664 |
can be reconstructed with #decodeAsLiteralArray. |
|
421 | 665 |
The encoding is: (Rectangle orgX orgY cornX cornY)" |
666 |
||
667 |
^ Array |
|
1245 | 668 |
with:#Rectangle |
669 |
with:left |
|
670 |
with:top |
|
671 |
with:width |
|
672 |
with:height |
|
421 | 673 |
|
674 |
||
675 |
" |
|
676 |
Rectangle new fromLiteralArrayEncoding:#(Rectangle 100 200 300 500) |
|
677 |
(100@200 corner:300@500) literalArrayEncoding |
|
678 |
" |
|
679 |
||
680 |
"Modified: 1.9.1995 / 02:16:54 / claus" |
|
1245 | 681 |
"Modified: 22.4.1996 / 13:00:36 / cg" |
421 | 682 |
! |
683 |
||
637 | 684 |
rectangleRelativeTo:aRectangle preferred:prefRect |
685 |
"compute a displayRectangle, treating the receiver like a |
|
1454 | 686 |
layoutFrame. |
687 |
This allows rectangles to be used interchangable with Layouts." |
|
379 | 688 |
|
637 | 689 |
^ (self asLayout) rectangleRelativeTo:aRectangle preferred:prefRect |
379 | 690 |
|
691 |
" |
|
637 | 692 |
(10@20 corner:20@30) rectangleRelativeTo:(0@0 corner:100@100) preferred:(0@0 corner:50@50) |
693 |
||
694 |
(0.5@0.5) rectangleRelativeTo:(0@0 corner:100@100) preferred:(0@0 corner:50@50) |
|
379 | 695 |
" |
1454 | 696 |
|
697 |
"Modified: 5.6.1996 / 17:24:11 / cg" |
|
356 | 698 |
! ! |
699 |
||
186 | 700 |
!Rectangle methodsFor:'destructive rectangle operations'! |
701 |
||
356 | 702 |
expandBy:delta |
703 |
"destructively expanded the receiver in all directions |
|
704 |
by amount, a Point, Rectangle or Number. |
|
705 |
Warning: this is a destructive operation, modifying the receiver |
|
706 |
NOT returning a copy. You have to be certain to be the exclusive |
|
707 |
owner of the receiver to avoid side effects. See also: #expandedBy:" |
|
708 |
||
709 |
|amountPoint deltaLeft deltaTop deltaWidth deltaHeight| |
|
710 |
||
711 |
delta isNumber ifTrue:[ |
|
712 |
deltaLeft := deltaTop := delta. |
|
713 |
deltaWidth := deltaHeight := delta * 2. |
|
714 |
] ifFalse:[ |
|
715 |
delta isRectangle ifTrue:[ |
|
716 |
deltaLeft := delta left. |
|
717 |
deltaTop := delta top. |
|
718 |
deltaWidth := deltaLeft + delta right. |
|
719 |
deltaHeight := deltaTop + delta bottom |
|
720 |
] ifFalse:[ |
|
721 |
amountPoint := delta asPoint. |
|
722 |
deltaLeft := amountPoint x. |
|
723 |
deltaTop := amountPoint y. |
|
724 |
deltaWidth := deltaLeft * 2. |
|
725 |
deltaHeight := deltaTop * 2. |
|
726 |
] |
|
727 |
]. |
|
728 |
||
729 |
left := (left - deltaLeft). |
|
730 |
top := (top - deltaTop). |
|
731 |
width := (width + deltaWidth). |
|
732 |
height := (height + deltaHeight). |
|
733 |
||
734 |
" |
|
735 |
|r| |
|
736 |
r := Rectangle origin:10@10 corner:100@100. |
|
737 |
r expandBy:5. |
|
738 |
||
739 |
r := Rectangle origin:10@10 corner:100@100. |
|
740 |
r expandBy:(5 @ 0). |
|
741 |
||
742 |
r := Rectangle origin:10@10 corner:100@100. |
|
743 |
r expandBy:(10 @ 10). |
|
744 |
||
745 |
r := Rectangle origin:10@10 corner:100@100. |
|
746 |
r expandBy:( 10@10 corner:20@20 ) |
|
747 |
" |
|
637 | 748 |
! |
749 |
||
750 |
moveBy:aPoint |
|
751 |
"destructively translate the rectangle by some distance. |
|
752 |
sorry for the name inconsistency - but GNU-ST named it that way" |
|
753 |
||
754 |
left := left + aPoint x. |
|
755 |
top := top + aPoint y |
|
756 |
! |
|
757 |
||
758 |
moveTo:aPoint |
|
759 |
"destructively translate the rectangle to have its origin at aPoint." |
|
760 |
||
761 |
|diff| |
|
762 |
||
763 |
diff := aPoint - self origin. |
|
764 |
self origin:aPoint corner:self corner + diff |
|
765 |
! |
|
766 |
||
767 |
scaleBy:scale |
|
768 |
"scale the receiver rectangle by scale (a Number or Point). |
|
769 |
This is destructive (modifies the receiver, not a copy) and |
|
770 |
should only be used if you know, that you are the exclusive owner |
|
771 |
of the receiver. (use scaledBy if in doubt)" |
|
772 |
||
773 |
|scalePoint sx sy| |
|
774 |
||
775 |
(scale isMemberOf:Point) ifTrue:[ "type hint to stc" |
|
776 |
sx := scale x. |
|
777 |
sy := scale y |
|
778 |
] ifFalse:[ |
|
779 |
scalePoint := scale asPoint. |
|
780 |
sx := scalePoint x. |
|
781 |
sy := scalePoint y |
|
782 |
]. |
|
783 |
width := width * sx. |
|
784 |
height := height * sy. |
|
785 |
left := left * sx. |
|
786 |
top := top * sy |
|
787 |
||
788 |
" |
|
789 |
(Rectangle origin:10@10 corner:50@50) scaleBy:2 |
|
790 |
" |
|
791 |
||
792 |
"its destructive:" |
|
793 |
" |
|
794 |
|r1 r2| |
|
795 |
||
796 |
r1 := Rectangle origin:10@10 corner:50@50. |
|
797 |
r2 := r1 scaleBy:2. |
|
798 |
r1 |
|
799 |
" |
|
800 |
! |
|
801 |
||
802 |
translateBy:amount |
|
803 |
"translate (i.e. move) the receiver rectangle |
|
804 |
by amount, a Point or Number. |
|
805 |
This is destructive (modifies the receiver, not a copy) and |
|
806 |
should only be used if you know, that you are the exclusive owner |
|
807 |
of the receiver. (use translatedBy if in doubt)" |
|
808 |
||
809 |
|amountPoint| |
|
810 |
||
811 |
(amount isMemberOf:Point) ifTrue:[ "type hint to stc" |
|
812 |
left := left + amount x. |
|
813 |
top := top + amount y |
|
814 |
] ifFalse:[ |
|
815 |
amountPoint := amount asPoint. |
|
816 |
left := left + amountPoint x. |
|
817 |
top := top + amountPoint y |
|
818 |
] |
|
819 |
||
820 |
" |
|
821 |
(Rectangle origin:10@10 corner:50@50) translateBy:10 |
|
822 |
" |
|
823 |
||
824 |
"its destructive:" |
|
825 |
" |
|
826 |
|r1 r2| |
|
827 |
||
828 |
r1 := Rectangle origin:10@10 corner:50@50. |
|
829 |
r2 := r1 translateBy:10. |
|
830 |
r1 |
|
831 |
" |
|
832 |
! ! |
|
833 |
||
834 |
!Rectangle methodsFor:'displaying'! |
|
835 |
||
836 |
displayFilledOn:aGC |
|
1314 | 837 |
"display a filled rectangle as represented by the receiver in |
838 |
the graphicsContext, aGC" |
|
839 |
||
637 | 840 |
aGC fillRectangleX:left y:top width:width height:height |
841 |
||
842 |
" |
|
843 |
|v| |
|
844 |
||
1349
275388fd5271
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
1314
diff
changeset
|
845 |
v := View new openAndWait. |
637 | 846 |
|
847 |
(Rectangle origin:10@10 corner:50@50) displayFilledOn:v |
|
848 |
" |
|
1314 | 849 |
|
1349
275388fd5271
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
1314
diff
changeset
|
850 |
"Modified: 8.5.1996 / 14:40:42 / cg" |
637 | 851 |
! |
852 |
||
853 |
displayStrokedOn:aGC |
|
1314 | 854 |
"display an unfilled rectangle as represented by the receiver in |
855 |
the graphicsContext, aGC" |
|
856 |
||
637 | 857 |
aGC displayRectangleX:left y:top width:width height:height |
858 |
||
859 |
" |
|
860 |
|v| |
|
861 |
||
1349
275388fd5271
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
1314
diff
changeset
|
862 |
v := View new openAndWait. |
637 | 863 |
|
864 |
(Rectangle origin:10@10 corner:50@50) displayStrokedOn:v |
|
865 |
" |
|
1314 | 866 |
|
1349
275388fd5271
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
1314
diff
changeset
|
867 |
"Modified: 8.5.1996 / 14:40:53 / cg" |
637 | 868 |
! ! |
869 |
||
870 |
!Rectangle methodsFor:'printing & storing'! |
|
871 |
||
872 |
printOn:aStream |
|
873 |
"print the receiver on aStream" |
|
874 |
||
875 |
aStream nextPutAll:(self class name). |
|
876 |
aStream nextPutAll:' origin:'. |
|
877 |
(self origin) printOn:aStream. |
|
1443 | 878 |
aStream nextPutAll:' extent:'. |
879 |
(self extent) printOn:aStream |
|
880 |
||
881 |
"Modified: 29.5.1996 / 00:17:39 / cg" |
|
637 | 882 |
! |
883 |
||
884 |
storeOn:aStream |
|
885 |
"store the receiver on aStream; i.e. print an expression which will |
|
886 |
reconstruct the receiver" |
|
887 |
||
888 |
aStream nextPut:$(. |
|
889 |
aStream nextPutAll:(self class name). |
|
890 |
aStream nextPutAll:' origin:'. |
|
891 |
(self origin) storeOn:aStream. |
|
1443 | 892 |
aStream nextPutAll:' extent:'. |
893 |
(self extent) storeOn:aStream. |
|
637 | 894 |
aStream nextPut:$) |
1443 | 895 |
|
896 |
"Modified: 29.5.1996 / 00:17:42 / cg" |
|
637 | 897 |
! ! |
898 |
||
899 |
!Rectangle methodsFor:'queries'! |
|
900 |
||
1349
275388fd5271
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
1314
diff
changeset
|
901 |
bounds |
275388fd5271
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
1314
diff
changeset
|
902 |
"return the smallest enclosing rectangle" |
275388fd5271
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
1314
diff
changeset
|
903 |
|
275388fd5271
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
1314
diff
changeset
|
904 |
^ self |
275388fd5271
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
1314
diff
changeset
|
905 |
|
275388fd5271
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
1314
diff
changeset
|
906 |
"Created: 8.5.1996 / 13:56:24 / cg" |
275388fd5271
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
1314
diff
changeset
|
907 |
"Modified: 8.5.1996 / 14:06:38 / cg" |
275388fd5271
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
1314
diff
changeset
|
908 |
! |
275388fd5271
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
1314
diff
changeset
|
909 |
|
275388fd5271
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
1314
diff
changeset
|
910 |
canBeFilled |
275388fd5271
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
1314
diff
changeset
|
911 |
"return true, if the receiver can be drawn as a filled geometric. |
275388fd5271
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
1314
diff
changeset
|
912 |
Always true here." |
275388fd5271
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
1314
diff
changeset
|
913 |
|
275388fd5271
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
1314
diff
changeset
|
914 |
^ true |
275388fd5271
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
1314
diff
changeset
|
915 |
|
275388fd5271
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
1314
diff
changeset
|
916 |
"Created: 8.5.1996 / 08:16:47 / cg" |
275388fd5271
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
1314
diff
changeset
|
917 |
! |
275388fd5271
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
1314
diff
changeset
|
918 |
|
2381
10af98758a49
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
2373
diff
changeset
|
919 |
computeBounds |
10af98758a49
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
2373
diff
changeset
|
920 |
"return the smallest enclosing rectangle" |
10af98758a49
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
2373
diff
changeset
|
921 |
|
10af98758a49
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
2373
diff
changeset
|
922 |
^ self |
10af98758a49
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
2373
diff
changeset
|
923 |
|
10af98758a49
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
2373
diff
changeset
|
924 |
"Modified: 8.5.1996 / 14:06:38 / cg" |
10af98758a49
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
2373
diff
changeset
|
925 |
"Created: 12.2.1997 / 11:44:45 / cg" |
10af98758a49
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
2373
diff
changeset
|
926 |
! |
10af98758a49
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
2373
diff
changeset
|
927 |
|
637 | 928 |
isRectangle |
929 |
"return true, if the receiver is some kind of rectangle" |
|
930 |
||
931 |
^ true |
|
186 | 932 |
! ! |
933 |
||
1 | 934 |
!Rectangle methodsFor:'rectangle operations'! |
935 |
||
936 |
+ aPoint |
|
356 | 937 |
"return a new rectangle with same extent as receiver but |
1 | 938 |
origin translated by the argument, aPoint" |
939 |
||
217 | 940 |
|amountPoint| |
941 |
||
2274 | 942 |
(aPoint isMemberOf:SmallInteger) ifTrue:[ |
943 |
"/ this is an stc optimization |
|
944 |
^ Rectangle |
|
2373 | 945 |
left:(left + aPoint) |
946 |
top:(top + aPoint) |
|
2274 | 947 |
width:width |
948 |
height:height |
|
949 |
]. |
|
950 |
||
217 | 951 |
amountPoint := aPoint asPoint. |
952 |
^ Rectangle left:(left + amountPoint x) |
|
2274 | 953 |
top:(top + amountPoint y) |
954 |
width:width |
|
955 |
height:height |
|
956 |
||
957 |
"Modified: 25.1.1997 / 17:29:53 / cg" |
|
958 |
! |
|
959 |
||
960 |
- aPoint |
|
961 |
"return a new rectangle with same extent as receiver but |
|
962 |
origin translated by the argument, aPoint" |
|
963 |
||
964 |
|amountPoint| |
|
965 |
||
966 |
(aPoint isMemberOf:SmallInteger) ifTrue:[ |
|
967 |
"/ this is an stc optimization |
|
968 |
^ Rectangle |
|
2373 | 969 |
left:(left - aPoint) |
970 |
top:(top - aPoint) |
|
2274 | 971 |
width:width |
972 |
height:height |
|
973 |
]. |
|
974 |
||
975 |
amountPoint := aPoint asPoint. |
|
976 |
^ Rectangle left:(left - amountPoint x) |
|
977 |
top:(top - amountPoint y) |
|
978 |
width:width |
|
979 |
height:height |
|
980 |
||
981 |
"Modified: 25.1.1997 / 17:29:53 / cg" |
|
982 |
"Created: 25.1.1997 / 17:30:21 / cg" |
|
1 | 983 |
! |
984 |
||
326 | 985 |
align:offset with:someCoordinate |
986 |
"return a new rectangle which is translated (i.e. moved) |
|
987 |
such that the point offset in mySelf is placed on someCoordinate." |
|
988 |
||
989 |
^ Rectangle origin:(someCoordinate - offset + self origin) |
|
990 |
extent:(self extent) |
|
991 |
" |
|
992 |
|r| |
|
993 |
||
994 |
r := Rectangle origin:10@10 corner:50@50. |
|
995 |
r align:(r center) with:100@100. |
|
996 |
" |
|
997 |
! |
|
998 |
||
1 | 999 |
amountToTranslateWithin: aRectangle |
1000 |
"for GNU-ST compatibility" |
|
1001 |
||
1002 |
^(aRectangle origin) - self origin |
|
1003 |
! |
|
1004 |
||
1005 |
areasOutside: aRectangle |
|
1006 |
"---------------------------------------------------------------- |
|
1007 |
| added for GNU-ST compatibility |
|
1008 |
| |
|
1009 |
| author: Doug McCallum <uunet!!ico.isc.com!!dougm> |
|
1010 |
| |
|
1011 |
|areasOutside: aRectangle |
|
1012 |
| most complicated of the Rectangle primitives |
|
1013 |
| The basic methodology is to first determine that there is an |
|
1014 |
| intersection by finding the overlapping rectangle. From the |
|
1015 |
| overlapping rectangle, first determine if it runs along an edge. |
|
1016 |
| If it doesn't, extend the rectangle up to the top edge and add |
|
1017 |
| the new rectangle to the collection and start the rest of the |
|
1018 |
| process. If the left edge does not touch the left edge of self, |
|
1019 |
| extend it to the edge saving the new rectangle. Then do the |
|
1020 |
| same to the right edge. Then check top and bottom edges. Most |
|
1021 |
| of the time only 2 or 3 rectangles get formed, occasionally 4. |
|
1022 |
| It should be possible to never get more than 3 but requires more |
|
1023 |
| work. |
|
1024 |
----------------------------------------------------------------" |
|
1025 |
||
1026 |
| collect iRect tmp | |
|
1124 | 1027 |
|
360 | 1028 |
iRect := self intersect: aRectangle. |
1124 | 1029 |
iRect isNil ifTrue: [^nil]. "case of no intersection" |
186 | 1030 |
"the collect collection gathers Rectangles" |
360 | 1031 |
collect := OrderedCollection new: 4. |
186 | 1032 |
"is it floating or on the edge?" |
1 | 1033 |
(((((iRect top) ~= self top) |
186 | 1034 |
and: [ (iRect bottom) ~= self bottom ]) |
1035 |
and: [ (iRect left) ~= self left ]) |
|
1036 |
and: [ (iRect right) ~= self right ] ) |
|
1037 |
ifTrue: "entirely in the center." |
|
360 | 1038 |
[tmp := Rectangle origin: (Point x: iRect left y: self top) |
1039 |
corner: iRect bottomRight. |
|
186 | 1040 |
collect add: tmp. |
360 | 1041 |
iRect := iRect merge: tmp]. |
1 | 1042 |
((iRect left) ~= self left) |
186 | 1043 |
ifTrue: "doesn't touch left edge so make it touch" |
360 | 1044 |
[tmp := Rectangle origin: (Point x: self left y: iRect top) |
1045 |
corner: iRect bottomLeft. |
|
186 | 1046 |
collect add: tmp. |
1047 |
"merge new (tmp) with overlap to keep track" |
|
360 | 1048 |
iRect := iRect merge: tmp]. |
1 | 1049 |
((iRect right) ~= self right) |
186 | 1050 |
ifTrue: "doesn't touch right edge so extend it" |
360 | 1051 |
[tmp := Rectangle origin: iRect topRight |
1052 |
corner: (Point x: self right y: iRect bottom). |
|
186 | 1053 |
collect add: tmp. |
360 | 1054 |
iRect := iRect merge: tmp]. |
77 | 1055 |
(((iRect left) ~= self left) or: [(iRect top) ~= self top]) |
186 | 1056 |
ifTrue: "whole top part can be taken now" |
360 | 1057 |
[tmp := Rectangle origin: self origin corner: iRect topRight. |
186 | 1058 |
collect add: tmp]. |
77 | 1059 |
(((iRect right) ~= self right) or: [(iRect bottom) ~= self bottom]) |
186 | 1060 |
ifTrue: "whole bottom open and can be taken" |
360 | 1061 |
[tmp := Rectangle origin: iRect bottomLeft corner: self corner. |
186 | 1062 |
collect add: tmp]. |
1 | 1063 |
^collect |
77 | 1064 |
! |
1 | 1065 |
|
637 | 1066 |
expandedBy:delta |
1067 |
"return a new rectangle which is expanded in all directions |
|
1068 |
by amount, a Point, Rectangle or Number" |
|
1069 |
||
1070 |
^ self copy expandBy:delta |
|
1071 |
||
1072 |
" |
|
1073 |
|r| |
|
1074 |
r := Rectangle origin:10@10 corner:100@100. |
|
1075 |
r expandedBy:5. |
|
1076 |
r expandedBy:(5 @ 0). |
|
1077 |
r expandedBy:(10 @ 10). |
|
1078 |
r expandedBy:( 10@10 corner:20@20 ) |
|
1079 |
" |
|
1080 |
! |
|
1081 |
||
1082 |
insetBy: delta |
|
1083 |
"return a new rectangle which is inset in all directions |
|
1084 |
by delta, a Point, Rectangle or Number" |
|
1085 |
||
1086 |
|amountPoint deltaLeft deltaTop deltaWidth deltaHeight| |
|
1087 |
||
1088 |
delta isNumber ifTrue:[ |
|
1089 |
deltaLeft := deltaTop := delta. |
|
1090 |
deltaWidth := deltaHeight := delta * 2. |
|
1091 |
] ifFalse:[ |
|
1092 |
delta isRectangle ifTrue:[ |
|
1093 |
deltaLeft := delta left. |
|
1094 |
deltaTop := delta top. |
|
1095 |
deltaWidth := deltaLeft + delta right. |
|
1096 |
deltaHeight := deltaTop + delta bottom |
|
1097 |
] ifFalse:[ |
|
1098 |
amountPoint := delta asPoint. |
|
1099 |
deltaLeft := amountPoint x. |
|
1100 |
deltaTop := amountPoint y. |
|
1101 |
deltaWidth := deltaLeft * 2. |
|
1102 |
deltaHeight := deltaTop * 2. |
|
1103 |
] |
|
1104 |
]. |
|
1105 |
||
1106 |
^ Rectangle left:(left + deltaLeft) |
|
1107 |
top:(top + deltaTop) |
|
1108 |
width:(width - deltaWidth) |
|
1109 |
height:(height - deltaHeight) |
|
1110 |
" |
|
1111 |
|r| |
|
1112 |
r := Rectangle origin:10@10 corner:100@100. |
|
1113 |
r insetBy:5. |
|
1114 |
r insetBy:(5 @ 0). |
|
1115 |
r insetBy:(10 @ 10). |
|
1116 |
r insetBy:( 10@10 corner:20@20 ) |
|
1117 |
" |
|
1118 |
! |
|
1119 |
||
1120 |
insetOriginBy:originDelta cornerBy:cornerDelta |
|
1121 |
"return a new rectangle which is inset by originDelta |
|
1122 |
and cornerDelta; both may be instances of Point or Number" |
|
1123 |
||
1124 |
^ Rectangle |
|
1125 |
origin:(left @ top) + originDelta asPoint |
|
1126 |
corner:(self corner - cornerDelta asPoint) |
|
1127 |
" |
|
1128 |
|r| |
|
1129 |
r := Rectangle origin:10@10 corner:100@100. |
|
1130 |
r insetOriginBy:5 cornerBy:10. |
|
1131 |
r insetOriginBy:10@5 cornerBy:10. |
|
1132 |
r insetOriginBy:10 cornerBy:10@5. |
|
1133 |
r insetOriginBy:10@10 cornerBy:20@20. |
|
1134 |
" |
|
1135 |
! |
|
1136 |
||
1137 |
intersect:aRectangle |
|
1138 |
"return a new rectangle covering the intersection of the receiver |
|
1139 |
and the argument, aRectangle. |
|
1140 |
the rectangles must intersect for a valid return" |
|
1141 |
||
1142 |
^ Rectangle left:(left max:(aRectangle left)) |
|
1143 |
right:((left + width) min:(aRectangle right)) |
|
1144 |
top:(top max:(aRectangle top)) |
|
1145 |
bottom:((top + height) min:(aRectangle bottom)) |
|
1146 |
||
1147 |
" |
|
1148 |
|r1 r2| |
|
1149 |
||
1150 |
r1 := Rectangle origin:10@10 corner:100@100. |
|
1151 |
r2 := Rectangle origin:50@50 corner:150@75. |
|
1152 |
r1 intersect:r2 |
|
1153 |
" |
|
1154 |
! |
|
1155 |
||
1156 |
merge:aRectangle |
|
1157 |
"return a new rectangle covering both the receiver |
|
1158 |
and the argument, aRectangle" |
|
1159 |
||
1160 |
^ Rectangle left:(left min:(aRectangle left)) |
|
1161 |
right:((left + width) max:(aRectangle right)) |
|
1162 |
top:(top min:(aRectangle top)) |
|
1163 |
bottom:((top + height) max:(aRectangle bottom)) |
|
1164 |
! |
|
1165 |
||
77 | 1166 |
nonIntersections:aRectangle |
1167 |
"this is the same as areasOutside: - for ST/V compatibility only" |
|
1168 |
||
1169 |
^ self areasOutside:aRectangle |
|
637 | 1170 |
! |
1171 |
||
1172 |
rounded |
|
1173 |
"return a copy of the receiver with rounded coordinates" |
|
1 | 1174 |
|
637 | 1175 |
^ Rectangle left:(left rounded) |
1176 |
top:(top rounded) |
|
1177 |
width:(width rounded) |
|
1178 |
height:(height rounded) |
|
1179 |
! |
|
356 | 1180 |
|
637 | 1181 |
scaledBy:scale |
1182 |
"return a new rectangle which is the receiver |
|
1183 |
scaled by scale" |
|
1184 |
||
1185 |
|scalePoint sx sy| |
|
356 | 1186 |
|
637 | 1187 |
scalePoint := scale asPoint. |
1188 |
sx := scalePoint x. |
|
1189 |
sy := scalePoint y. |
|
1190 |
^ Rectangle left:left * sx |
|
1191 |
top:top * sy |
|
1192 |
width:(width * sx) |
|
1193 |
height:(height * sy) |
|
356 | 1194 |
" |
637 | 1195 |
(Rectangle origin:10@10 corner:50@50) scaledBy:2 |
1196 |
" |
|
356 | 1197 |
|
637 | 1198 |
"its NOT destructive:" |
1199 |
" |
|
1200 |
|r1 r2| |
|
356 | 1201 |
|
637 | 1202 |
r1 := Rectangle origin:10@10 corner:50@50. |
1203 |
r2 := r1 scaledBy:2. |
|
1204 |
r1 |
|
356 | 1205 |
" |
1206 |
! |
|
1207 |
||
637 | 1208 |
translatedBy:amount |
1209 |
"return a new rectangle which is translated (i.e. moved) |
|
1210 |
by amount, aPoint or Number" |
|
1211 |
||
1212 |
|amountPoint| |
|
356 | 1213 |
|
637 | 1214 |
amountPoint := amount asPoint. |
1215 |
^ Rectangle left:(left + amountPoint x) |
|
1216 |
top:(top + amountPoint y) |
|
1217 |
width:width |
|
1218 |
height:height |
|
356 | 1219 |
" |
637 | 1220 |
(Rectangle origin:10@10 corner:50@50) translatedBy:10 |
1221 |
" |
|
356 | 1222 |
|
637 | 1223 |
"its NOT destructive:" |
1224 |
" |
|
1225 |
|r1 r2| |
|
356 | 1226 |
|
637 | 1227 |
r1 := Rectangle origin:10@10 corner:50@50. |
1228 |
r2 := r1 translatedBy:10. |
|
1229 |
r1 |
|
356 | 1230 |
" |
1231 |
! ! |
|
1232 |
||
637 | 1233 |
!Rectangle methodsFor:'testing'! |
1 | 1234 |
|
637 | 1235 |
contains:aRectangle |
1236 |
"return true, if the argument, aRectangle is equal to or |
|
1237 |
is contained fully within the receiver" |
|
1 | 1238 |
|
1149
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1239 |
%{ /* NOCONTEXT */ |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1240 |
/* |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1241 |
* claus: this may be often called by objectView |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1242 |
* the primitive code below (although lookung ugly) |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1243 |
* speeds that up by almost a factor of 2 ... |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1244 |
*/ |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1245 |
OBJ slf = self; |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1246 |
OBJ rct = aRectangle; |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1247 |
|
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1248 |
if (__isNonNilObject(rct) |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1249 |
&& __qClass(rct) == Rectangle) { |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1250 |
OBJ r_l, r_w, r_t, r_h, l, w, t, h; |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1251 |
int r_ir, r_il, r_ib, r_it; |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1252 |
int il, it, ir, ib, iw, ih; |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1253 |
|
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1254 |
r_l = __OINST(rct, left); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1255 |
l = __OINST(slf, left); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1256 |
if (__bothSmallInteger(r_l, l)) { |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1257 |
#ifndef POSITIVE_ADDRESSES /* tag in low-bit */ |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1258 |
il = (int)(l); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1259 |
r_il = (int)(r_l); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1260 |
#else |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1261 |
il = __intVal(l); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1262 |
r_il = __intVal(r_l); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1263 |
#endif |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1264 |
if (il > r_il) { RETURN (false); } /* left > aRectangle left */ |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1265 |
|
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1266 |
r_t = __OINST(rct, top); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1267 |
t = __OINST(slf, top); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1268 |
if (__bothSmallInteger(r_t, t)) { |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1269 |
#ifndef POSITIVE_ADDRESSES /* tag in low-bit */ |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1270 |
it = (int)(t); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1271 |
r_it = (int)(r_t); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1272 |
#else |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1273 |
it = __intVal(t); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1274 |
r_it = __intVal(r_t); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1275 |
#endif |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1276 |
if (it > r_it) { RETURN (false); } /* top > aRectangle top */ |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1277 |
|
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1278 |
r_w = __OINST(rct, width); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1279 |
w = __OINST(slf, width); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1280 |
if (__bothSmallInteger(r_w, w)) { |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1281 |
#ifndef POSITIVE_ADDRESSES /* tag in low-bit */ |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1282 |
ir = il + (int)(w); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1283 |
r_ir = r_il + (int)(r_w); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1284 |
#else |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1285 |
ir = il + __intVal(w); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1286 |
r_ir = r_il + __intVal(r_w); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1287 |
#endif |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1288 |
if (ir < r_ir) { RETURN (false); } /* (left + width) < aRectangle right */ |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1289 |
|
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1290 |
r_h = __OINST(rct, height); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1291 |
h = __OINST(slf, height); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1292 |
if (__bothSmallInteger(r_h, h)) { |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1293 |
#ifndef POSITIVE_ADDRESSES /* tag in low-bit */ |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1294 |
ib = it + (int)(h); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1295 |
r_ib = r_it + (int)(r_h); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1296 |
#else |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1297 |
ib = it + __intVal(h); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1298 |
r_ib = r_it + __intVal(r_h); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1299 |
#endif |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1300 |
if (ib < r_ib) { RETURN (false); } /* (top + height) < aRectangle bottom */ |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1301 |
RETURN (true); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1302 |
} |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1303 |
} |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1304 |
} |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1305 |
} |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1306 |
} |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1307 |
%}. |
637 | 1308 |
(left <= aRectangle left) ifTrue:[ |
1309 |
((left + width) >= aRectangle right) ifTrue:[ |
|
1149
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1310 |
(top <= aRectangle top) ifTrue:[ |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1311 |
((top + height) >= aRectangle bottom) ifTrue:[ |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1312 |
^ true |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1313 |
] |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1314 |
] |
637 | 1315 |
] |
1316 |
]. |
|
1317 |
^ false |
|
1149
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1318 |
|
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1319 |
" |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1320 |
(0@0 corner:100@100) contains:(10@10 corner:90@90) |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1321 |
(0@0 corner:100@100) contains:(10@10 corner:100@100) |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1322 |
(0@0 corner:100@100) contains:(10@10 corner:110@100) |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1323 |
(0@0 corner:100@100) contains:(10@10 corner:100@110) |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1324 |
(10@10 corner:100@100) contains:(0@10 corner:100@100) |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1325 |
" |
637 | 1326 |
! |
1327 |
||
1328 |
containsPoint:aPoint |
|
1329 |
"return true, if the argument, aPoint is contained in the receiver" |
|
1330 |
||
1331 |
|px py| |
|
1332 |
||
1333 |
px := aPoint x. |
|
1334 |
(px < left) ifTrue:[^ false]. |
|
1335 |
(px > (left + width)) ifTrue:[^ false]. |
|
1336 |
py := aPoint y. |
|
1337 |
(py < top) ifTrue:[^ false]. |
|
1338 |
(py > (top + height)) ifTrue:[^ false]. |
|
1339 |
^ true |
|
1 | 1340 |
! |
1341 |
||
637 | 1342 |
containsPointX:x y:y |
1343 |
"return true, if the point defined by x@y is contained in the receiver. |
|
1344 |
This is the same as containsPoint:, but can be used if the coordinates |
|
1345 |
are already available as separate numbers to avoid useless creation of a |
|
1346 |
temporary point." |
|
1347 |
||
1348 |
(x < left) ifTrue:[^ false]. |
|
1349 |
(x > (left + width)) ifTrue:[^ false]. |
|
1350 |
(y < top) ifTrue:[^ false]. |
|
1351 |
(y > (top + height)) ifTrue:[^ false]. |
|
1352 |
^ true |
|
1353 |
! |
|
1354 |
||
1355 |
intersects:aRectangle |
|
1356 |
"return true, if the intersection between the argument, aRectangle |
|
1357 |
and the receiver is not empty" |
|
1358 |
||
1359 |
|b r| |
|
1 | 1360 |
|
1149
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1361 |
%{ /* NOCONTEXT */ |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1362 |
/* |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1363 |
* claus: this is one of the mostly called methods in |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1364 |
* the objectView - the primitive code below (although lookung ugly) |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1365 |
* speeds up drawing by almost a factor of 2 ... |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1366 |
*/ |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1367 |
OBJ slf = self; |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1368 |
OBJ rct = aRectangle; |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1369 |
|
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1370 |
if (__isNonNilObject(rct) |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1371 |
&& __qClass(rct) == Rectangle) { |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1372 |
OBJ r_l, r_w, r_t, r_h, l, w, t, h; |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1373 |
int r_ir, r_il, r_ib, r_it; |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1374 |
int il, it, ir, ib, iw, ih; |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1375 |
|
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1376 |
r_l = __OINST(rct, left); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1377 |
r_w = __OINST(rct, width); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1378 |
|
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1379 |
if (__bothSmallInteger(r_l, r_w)) { |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1380 |
r_t = __OINST(rct, top); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1381 |
r_h = __OINST(rct, height); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1382 |
|
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1383 |
l = __OINST(slf, left); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1384 |
w = __OINST(slf, width); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1385 |
|
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1386 |
if (__bothSmallInteger(l, w)) { |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1387 |
t = __OINST(slf, top); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1388 |
h = __OINST(slf, height); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1389 |
|
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1390 |
#ifndef POSITIVE_ADDRESSES /* tag in low-bit */ |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1391 |
r_il = (int)(r_l); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1392 |
r_ir = r_il + (int)(r_w) - 1; |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1393 |
il = (int)(l); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1394 |
if (r_ir < il) { RETURN (false); } |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1395 |
ir = il + (int)(w) - 1; |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1396 |
if (r_il > ir) { RETURN (false); } |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1397 |
#else /* tag in hi-bit */ |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1398 |
r_il = __intVal(r_l); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1399 |
r_ir = r_il + __intVal(r_w); /* aRectangle right */ |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1400 |
il = __intVal(l); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1401 |
if (r_ir < il) { RETURN (false); } /* (aRectangle right) < left */ |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1402 |
|
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1403 |
ir = il + __intVal(w); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1404 |
if (r_il > ir) { RETURN (false); } /* (aRectangle left) > r */ |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1405 |
#endif |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1406 |
|
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1407 |
if (__bothSmallInteger(r_t, r_h)) { |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1408 |
if (__bothSmallInteger(t, h)) { |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1409 |
#ifndef POSITIVE_ADDRESSES |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1410 |
r_it = (int)(r_t); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1411 |
r_ib = r_it + (int)(r_h) - 1; /* aRectangle bottom */ |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1412 |
it = (int)(t); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1413 |
if (r_ib < it) { RETURN (false); } /* (aRectangle bottom) < top */ |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1414 |
|
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1415 |
ib = it + (int)(h) - 1; |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1416 |
if (r_it > ib) { RETURN (false); } /* (aRectangle top) > b */ |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1417 |
#else |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1418 |
r_it = __intVal(r_t); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1419 |
r_ib = r_it + __intVal(r_h); /* aRectangle bottom */ |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1420 |
it = __intVal(t); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1421 |
if (r_ib < it) { RETURN (false); } /* (aRectangle bottom) < top */ |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1422 |
|
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1423 |
ib = it + __intVal(h); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1424 |
if (r_it > ib) { RETURN (false); } /* (aRectangle top) > b */ |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1425 |
#endif |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1426 |
RETURN (true); |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1427 |
} |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1428 |
} |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1429 |
} |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1430 |
} |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1431 |
} |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1432 |
%}. |
637 | 1433 |
(aRectangle right) < left ifTrue:[^ false]. |
1434 |
(aRectangle bottom) < top ifTrue:[^ false]. |
|
1435 |
r := left + width. |
|
1436 |
(aRectangle left) > r ifTrue:[^ false]. |
|
1437 |
b := top + height. |
|
1438 |
(aRectangle top) > b ifTrue:[^ false]. |
|
1439 |
^ true |
|
1440 |
! |
|
1441 |
||
1442 |
isContainedIn:aRectangle |
|
1443 |
"return true, if the receiver is fully contained within |
|
1444 |
the argument, aRectangle" |
|
1445 |
||
1446 |
(aRectangle left <= left) ifTrue:[ |
|
1447 |
(aRectangle right >= (left + width)) ifTrue:[ |
|
1448 |
(aRectangle top <= top) ifTrue:[ |
|
1449 |
(aRectangle bottom >= (top + height)) ifTrue:[ |
|
1450 |
^ true |
|
1451 |
] |
|
1452 |
] |
|
1453 |
] |
|
1454 |
]. |
|
1455 |
^ false |
|
1 | 1456 |
! ! |
637 | 1457 |
|
1458 |
!Rectangle class methodsFor:'documentation'! |
|
1459 |
||
1460 |
version |
|
2381
10af98758a49
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
2373
diff
changeset
|
1461 |
^ '$Header: /cvs/stx/stx/libbasic/Rectangle.st,v 1.51 1997-02-12 12:23:34 cg Exp $' |
637 | 1462 |
! ! |