author | Jan Vrany <jan.vrany@labware.com> |
Tue, 01 Jun 2021 20:19:13 +0100 | |
branch | jv |
changeset 25424 | 51bd8a6b196f |
parent 21387 | e3865533e6a6 |
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 |
" |
|
5552 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
18820 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
356 | 16 |
Geometric subclass:#Rectangle |
1149
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
17 |
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
|
18 |
classVariableNames:'' |
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
19 |
poolDictionaries:'' |
7582 | 20 |
category:'Graphics-Geometry-Objects' |
1 | 21 |
! |
22 |
||
77 | 23 |
!Rectangle class methodsFor:'documentation'! |
24 |
||
88 | 25 |
copyright |
26 |
" |
|
27 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
186 | 28 |
All Rights Reserved |
88 | 29 |
|
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
38 |
||
77 | 39 |
documentation |
40 |
" |
|
41 |
Rectangles represent a rectangular area in 2D space. |
|
42 |
||
43 |
notice, my implementation does not use origin/corner as instance objects |
|
44 |
but left/top/width/height to save space and allocations. This means, that my |
|
45 |
Rectangles cannot be used to represent Rectangles in a higher than 2D |
|
46 |
space. (i.e. only valid if origin/corner are 2D Points) |
|
47 |
||
48 |
(aside from that, you will not see any difference from the outside) |
|
49 |
||
50 |
Instance variables: |
|
51 |
||
20308 | 52 |
left <Number> the left coordinate (i.e origin x) |
53 |
top <Number> the top coordinate (i.e origin y) |
|
54 |
width <Number> the width of the rectangle |
|
55 |
height <Number> the height of the rectangle |
|
77 | 56 |
|
20308 | 57 |
I am not certain, if implementing Rectangle different was a good idea - |
77 | 58 |
subclasses may expect things to be different ... |
59 |
Therefore, this may change. |
|
1290 | 60 |
|
61 |
[author:] |
|
20308 | 62 |
Claus Gittinger |
1290 | 63 |
|
1314 | 64 |
[see also:] |
20308 | 65 |
Point Polygon Circle EllipticalArc Spline |
66 |
LayoutOrigin LayoutFrame AlignmentOrigin Layout |
|
67 |
View GraphicsContext StrokingWrapper FillingWrapper |
|
77 | 68 |
" |
69 |
! ! |
|
70 |
||
1 | 71 |
!Rectangle class methodsFor:'instance creation'! |
72 |
||
20308 | 73 |
center:centerPoint extent:extentPoint |
74 |
"return an instance of me whose center is centerPoint and width |
|
4890 | 75 |
by height is extentPoint." |
76 |
||
20308 | 77 |
^ self |
78 |
origin:centerPoint - (extentPoint//2) |
|
79 |
extent:extentPoint |
|
4890 | 80 |
! |
81 |
||
3239
1114c37ca259
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
82 |
decodeFromLiteralArray:anArray |
1114c37ca259
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
83 |
"create & return a new instance from information encoded in anArray. |
1114c37ca259
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
84 |
Redefined for faster creation." |
1114c37ca259
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
85 |
|
1114c37ca259
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
86 |
|l t w h| |
1114c37ca259
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
87 |
|
1114c37ca259
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
88 |
l := anArray at:2. |
1114c37ca259
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
89 |
t := anArray at:3. |
1114c37ca259
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
90 |
w := (anArray at:4) - l. |
1114c37ca259
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
91 |
h := (anArray at:5) - t. |
1114c37ca259
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
92 |
^ self left:l top:t width:w height:h |
1114c37ca259
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
93 |
|
1114c37ca259
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
94 |
" |
20308 | 95 |
Rectangle decodeFromLiteralArray:#(Rectangle 100 200 300 500) |
3239
1114c37ca259
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
96 |
" |
1114c37ca259
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
97 |
|
1114c37ca259
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
98 |
"Created: / 28.1.1998 / 17:46:52 / cg" |
1114c37ca259
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
99 |
! |
1114c37ca259
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
100 |
|
4864 | 101 |
encompassing:listOfPoints |
102 |
"Return a rectangle, which encompasses all of the given points." |
|
103 |
||
104 |
|topLeft bottomRight| |
|
105 |
||
106 |
topLeft := bottomRight := nil. |
|
20308 | 107 |
listOfPoints do:[:p | |
21311 | 108 |
topLeft isNil |
109 |
ifTrue: [topLeft := bottomRight := p] |
|
110 |
ifFalse: [topLeft := topLeft min: p. |
|
111 |
bottomRight := bottomRight max: p] |
|
4864 | 112 |
]. |
113 |
^ topLeft corner: bottomRight |
|
114 |
||
115 |
" |
|
116 |
Rectangle encompassing:(Array with:10@10 with:50@0 with:0@50) |
|
117 |
" |
|
118 |
! |
|
119 |
||
637 | 120 |
left:left right:right top:top bottom:bottom |
121 |
"create and return a new Rectangle giving left, top, right and bottom coordinates" |
|
122 |
||
123 |
^ (self basicNew) left:left right:right top:top bottom:bottom |
|
124 |
! |
|
125 |
||
1356 | 126 |
left:left top:top extent:extent |
127 |
"create and return a new Rectangle giving left, top, and |
|
128 |
an extent point." |
|
129 |
||
130 |
^ (self basicNew) left:left top:top extent:extent |
|
131 |
||
132 |
"Created: 8.5.1996 / 20:56:29 / cg" |
|
133 |
! |
|
134 |
||
637 | 135 |
left:left top:top right:right bottom:bottom |
136 |
"create and return a new Rectangle giving left, top, right and bottom coordinates" |
|
137 |
||
138 |
^ (self basicNew) left:left top:top right:right bottom:bottom |
|
139 |
! |
|
140 |
||
141 |
left:left top:top width:w height:h |
|
142 |
"create and return a new Rectangle giving left and top coordinates |
|
143 |
and width, height" |
|
144 |
||
145 |
%{ /* NOCONTEXT */ |
|
146 |
REGISTER OBJ newRect; |
|
147 |
OBJ t; |
|
148 |
int spc; |
|
149 |
||
7859
837f47feba96
Have to call __STORE(), when assigning class.
Stefan Vogel <sv@exept.de>
parents:
7582
diff
changeset
|
150 |
if (__CanDoQuickNew(OHDR_SIZE + 4*sizeof(OBJ))) { /* OBJECT ALLOCATION */ |
20308 | 151 |
if (self == Rectangle) { |
152 |
/* |
|
153 |
* short cut - rectangles are created so often ... |
|
154 |
*/ |
|
155 |
__qCheckedNew(newRect, OHDR_SIZE + 4*sizeof(OBJ)); |
|
7859
837f47feba96
Have to call __STORE(), when assigning class.
Stefan Vogel <sv@exept.de>
parents:
7582
diff
changeset
|
156 |
|
20308 | 157 |
__InstPtr(newRect)->o_class = Rectangle; |
158 |
__qSTORE(newRect, Rectangle); |
|
637 | 159 |
|
20308 | 160 |
__InstPtr(newRect)->i_instvars[0] = left; |
161 |
__InstPtr(newRect)->i_instvars[1] = top; |
|
162 |
__InstPtr(newRect)->i_instvars[2] = w; |
|
163 |
__InstPtr(newRect)->i_instvars[3] = h; |
|
637 | 164 |
|
20308 | 165 |
if (! (__bothSmallInteger(left, top) && |
166 |
__bothSmallInteger(w, h))) { |
|
167 |
spc = __qSpace(newRect); |
|
168 |
__STORE_SPC(newRect, left, spc); |
|
169 |
__STORE_SPC(newRect, top, spc); |
|
170 |
__STORE_SPC(newRect, w, spc); |
|
171 |
__STORE_SPC(newRect, h, spc); |
|
172 |
} |
|
637 | 173 |
|
20308 | 174 |
RETURN ( newRect ); |
175 |
} |
|
637 | 176 |
} |
1507 | 177 |
%}. |
637 | 178 |
^ (self basicNew) left:left top:top width:w height:h |
179 |
! |
|
180 |
||
4894 | 181 |
merging: listOfRects |
182 |
"return the merge of a number of rectangles. |
|
183 |
Avoids creation of temp garbage." |
|
184 |
||
185 |
| topLeft bottomRight | |
|
186 |
||
187 |
topLeft := bottomRight := nil. |
|
20308 | 188 |
listOfRects do:[:r | |
21311 | 189 |
topLeft isNil ifTrue: [ |
190 |
topLeft := r topLeft. |
|
191 |
bottomRight := r bottomRight |
|
192 |
] ifFalse: [ |
|
193 |
topLeft := topLeft min: r topLeft. |
|
194 |
bottomRight := bottomRight max: r bottomRight |
|
195 |
] |
|
4894 | 196 |
]. |
197 |
^ topLeft corner: bottomRight |
|
198 |
! |
|
199 |
||
1 | 200 |
origin:origin corner:corner |
201 |
"create and return a new Rectangle giving top-left and bottom-right points" |
|
202 |
||
203 |
^ (self basicNew) origin:origin corner:corner |
|
204 |
! |
|
205 |
||
206 |
origin:origin extent:extent |
|
207 |
"create and return a new Rectangle giving top-left point and extent point" |
|
208 |
||
209 |
%{ /* NOCONTEXT */ |
|
59 | 210 |
REGISTER OBJ newRect; |
293 | 211 |
OBJ t; |
212 |
int spc; |
|
1 | 213 |
|
7859
837f47feba96
Have to call __STORE(), when assigning class.
Stefan Vogel <sv@exept.de>
parents:
7582
diff
changeset
|
214 |
if (__CanDoQuickNew(OHDR_SIZE + 4*sizeof(OBJ))) { /* OBJECT ALLOCATION */ |
20308 | 215 |
/* |
216 |
* short cut - rectangles are created so often ... |
|
217 |
*/ |
|
218 |
if (self == Rectangle) { |
|
219 |
if (__isPoint(origin) && __isPoint(extent)) { |
|
220 |
__qCheckedNew(newRect, OHDR_SIZE + 4*sizeof(OBJ)); |
|
221 |
__InstPtr(newRect)->o_class = Rectangle; |
|
222 |
__qSTORE(newRect, Rectangle); |
|
223 |
spc = __qSpace(newRect); |
|
59 | 224 |
|
20308 | 225 |
t = __PointInstPtr(origin)->p_x; |
226 |
__InstPtr(newRect)->i_instvars[0] = t; |
|
227 |
__STORE_SPC(newRect, t, spc); |
|
293 | 228 |
|
20308 | 229 |
t = __PointInstPtr(origin)->p_y; |
230 |
__InstPtr(newRect)->i_instvars[1] = t; |
|
231 |
__STORE_SPC(newRect, t, spc); |
|
59 | 232 |
|
20308 | 233 |
t = __PointInstPtr(extent)->p_x; |
234 |
__InstPtr(newRect)->i_instvars[2] = t; |
|
235 |
__STORE_SPC(newRect, t, spc); |
|
293 | 236 |
|
20308 | 237 |
t = __PointInstPtr(extent)->p_y; |
238 |
__InstPtr(newRect)->i_instvars[3] = t; |
|
239 |
__STORE_SPC(newRect, t, spc); |
|
240 |
RETURN ( newRect ); |
|
241 |
} |
|
242 |
} |
|
1 | 243 |
} |
1507 | 244 |
%}. |
1 | 245 |
^ (self basicNew) origin:origin extent:extent |
1356 | 246 |
! |
247 |
||
248 |
origin:origin width:w height:h |
|
249 |
"create and return a new Rectangle giving top-left and extent |
|
250 |
as individual width/height" |
|
251 |
||
252 |
^ (self basicNew) origin:origin width:w height:h |
|
253 |
||
254 |
"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
|
255 |
! |
9a98e1acd063
added #vertex:vertex: - ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2274
diff
changeset
|
256 |
|
20308 | 257 |
vertex:vertex1Point vertex:vertex2Point |
2372
9a98e1acd063
added #vertex:vertex: - ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2274
diff
changeset
|
258 |
"create and return a new instance of the receiver, |
3239
1114c37ca259
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
259 |
given two diagonally opposite vertices." |
1114c37ca259
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
260 |
|
1114c37ca259
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
261 |
^ self |
20308 | 262 |
origin: (vertex1Point min: vertex2Point) |
263 |
corner: (vertex1Point max: vertex2Point) |
|
2372
9a98e1acd063
added #vertex:vertex: - ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2274
diff
changeset
|
264 |
|
9a98e1acd063
added #vertex:vertex: - ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2274
diff
changeset
|
265 |
"Created: 10.2.1997 / 12:14:32 / cg" |
1 | 266 |
! ! |
267 |
||
7297 | 268 |
!Rectangle class methodsFor:'instance creation-interactive'! |
4894 | 269 |
|
270 |
fromUser |
|
15842 | 271 |
"let user specify a rectangle on the screen, return it. |
15900
ef5a31a78db8
AbortSignal -> AbortOperationRequest
Stefan Vogel <sv@exept.de>
parents:
15842
diff
changeset
|
272 |
If the user presses ESC, an AbortOperationRequest is raised." |
4894 | 273 |
|
274 |
^ Screen current rectangleFromUser |
|
275 |
||
276 |
" |
|
20308 | 277 |
Rectangle fromUser |
4894 | 278 |
" |
279 |
! |
|
280 |
||
281 |
originFromUser:extent |
|
282 |
"let user specify an origin on the screen, return it" |
|
283 |
||
284 |
^ Screen current originFromUser:extent |
|
285 |
||
286 |
" |
|
20308 | 287 |
Rectangle originFromUser:50@50 |
4894 | 288 |
" |
289 |
! ! |
|
290 |
||
7261 | 291 |
!Rectangle methodsFor:'Compatibility-Squeak'! |
4976 | 292 |
|
4990 | 293 |
amountToTranslateWithin: aRectangle |
6065 | 294 |
"Answer a Point, delta, such that self + delta is forced within aRectangle." |
295 |
"Altered so as to prefer to keep self topLeft inside when all of self |
|
296 |
cannot be made to fit 7/27/96 di" |
|
297 |
||
298 |
| dx dy | |
|
299 |
||
300 |
dx := 0. dy := 0. |
|
301 |
self right > aRectangle right ifTrue: [dx := aRectangle right - self right]. |
|
302 |
self bottom > aRectangle bottom ifTrue: [dy := aRectangle bottom - self bottom]. |
|
303 |
(self left + dx) < aRectangle left ifTrue: [dx := aRectangle left - self left]. |
|
304 |
(self top + dy) < aRectangle top ifTrue: [dy := aRectangle top - self top]. |
|
305 |
^ dx@dy |
|
306 |
! |
|
4990 | 307 |
|
308 |
containsRect:aRect |
|
309 |
"Answer whether aRect is within the receiver (OK to coincide)." |
|
310 |
||
311 |
^ self contains:aRect |
|
312 |
! |
|
313 |
||
5007 | 314 |
isTall |
315 |
"return true, if the receiver is higher then its width" |
|
316 |
||
317 |
^ self height > self width |
|
318 |
! |
|
319 |
||
320 |
isWide |
|
321 |
"return true, if the receiver is wider then its height" |
|
322 |
||
323 |
^ self width > self height |
|
324 |
! |
|
325 |
||
4990 | 326 |
translatedToBeWithin: aRectangle |
6065 | 327 |
"Answer a copy of the receiver that does not extend beyond aRectangle. 7/8/96 sw" |
4990 | 328 |
|
6065 | 329 |
^ self translateBy: (self amountToTranslateWithin: aRectangle) |
330 |
! |
|
4990 | 331 |
|
20308 | 332 |
withHeight:height |
4976 | 333 |
"Return a copy of me with a different height" |
334 |
||
335 |
|origin corner| |
|
336 |
||
337 |
origin := self origin. |
|
338 |
corner := self corner. |
|
339 |
^ origin corner:(corner x @ (origin y + height)) |
|
340 |
! ! |
|
341 |
||
1 | 342 |
!Rectangle methodsFor:'accessing'! |
343 |
||
637 | 344 |
area |
20308 | 345 |
"return the area |
637 | 346 |
- for screen Rectangles this is the number of pixels" |
347 |
||
348 |
^ width * height |
|
349 |
! |
|
350 |
||
351 |
bottom |
|
352 |
"return the y coordinate of the bottom" |
|
353 |
||
354 |
^ (top + height) |
|
355 |
! |
|
356 |
||
357 |
bottom:aNumber |
|
4860 | 358 |
"set the bottom edge - warning: destructive" |
637 | 359 |
|
360 |
height := aNumber - top |
|
361 |
! |
|
362 |
||
363 |
bottomCenter |
|
364 |
"return the bottom center point" |
|
365 |
||
1453
eb73ecdb25ab
center methods should not use //
Claus Gittinger <cg@exept.de>
parents:
1443
diff
changeset
|
366 |
^ (left + (width / 2)) @ (top + height) |
eb73ecdb25ab
center methods should not use //
Claus Gittinger <cg@exept.de>
parents:
1443
diff
changeset
|
367 |
|
eb73ecdb25ab
center methods should not use //
Claus Gittinger <cg@exept.de>
parents:
1443
diff
changeset
|
368 |
"Modified: 4.6.1996 / 16:04:08 / cg" |
637 | 369 |
! |
370 |
||
371 |
bottomLeft |
|
372 |
"return the bottom-left point" |
|
373 |
||
374 |
^ left @ (top + height) |
|
375 |
! |
|
376 |
||
377 |
bottomRight |
|
378 |
"return the bottom-right point" |
|
379 |
||
380 |
^ (left + width) @ (top + height) |
|
381 |
! |
|
382 |
||
383 |
center |
|
384 |
"return the point in the center of the receiver" |
|
385 |
||
1453
eb73ecdb25ab
center methods should not use //
Claus Gittinger <cg@exept.de>
parents:
1443
diff
changeset
|
386 |
^ (left + (width / 2)) @ (top + (height / 2)) |
eb73ecdb25ab
center methods should not use //
Claus Gittinger <cg@exept.de>
parents:
1443
diff
changeset
|
387 |
|
eb73ecdb25ab
center methods should not use //
Claus Gittinger <cg@exept.de>
parents:
1443
diff
changeset
|
388 |
"Modified: 4.6.1996 / 16:03:53 / cg" |
637 | 389 |
! |
390 |
||
391 |
corner |
|
392 |
"return the corner" |
|
393 |
||
17238 | 394 |
%{ /* NOCONTEXT */ |
17239 | 395 |
if (self == @global(Rectangle)) { |
20308 | 396 |
OBJ _tleft = __INST(left); |
397 |
OBJ _ttop = __INST(top); |
|
398 |
OBJ _twidth = __INST(width); |
|
399 |
OBJ _theight = __INST(height); |
|
17238 | 400 |
|
20308 | 401 |
if (__bothSmallInteger(_tleft, _ttop) |
402 |
&& __bothSmallInteger(_twidth, _theight)) { |
|
403 |
if (__CanDoQuickNew(sizeof(struct __Point))) { /* OBJECT ALLOCATION */ |
|
404 |
OBJ newPoint; |
|
405 |
int spc; |
|
406 |
int cX = __intVal(_tleft) + __intVal(_twidth); |
|
407 |
int cY = __intVal(_ttop) + __intVal(_theight); |
|
17238 | 408 |
|
20308 | 409 |
if (__ISVALIDINTEGER(cX) && __ISVALIDINTEGER(cY)) { |
410 |
__qCheckedNew(newPoint, sizeof(struct __Point)); |
|
411 |
__InstPtr(newPoint)->o_class = self; __qSTORE(newPoint, self); |
|
412 |
__PointInstPtr(newPoint)->p_x = __MKSMALLINT(cX); |
|
413 |
__PointInstPtr(newPoint)->p_y = __MKSMALLINT(cY); |
|
414 |
RETURN ( newPoint ); |
|
415 |
} |
|
416 |
} |
|
417 |
} |
|
17238 | 418 |
} |
419 |
%}. |
|
637 | 420 |
^ (left + width) @ (top + height) |
421 |
! |
|
422 |
||
423 |
corner:aPoint |
|
4860 | 424 |
"set the bottom-right corner - warning: destructive" |
637 | 425 |
|
426 |
width := aPoint x - left. |
|
427 |
height := aPoint y - top |
|
428 |
! |
|
429 |
||
430 |
extent |
|
17238 | 431 |
"return the extent" |
432 |
||
433 |
%{ /* NOCONTEXT */ |
|
434 |
||
435 |
if (__CanDoQuickNew(sizeof(struct __Point))) { /* OBJECT ALLOCATION */ |
|
20308 | 436 |
if (self == @global(Rectangle)) { |
437 |
OBJ newPoint; |
|
438 |
int spc; |
|
439 |
OBJ newX = __INST(width); |
|
440 |
OBJ newY = __INST(height); |
|
637 | 441 |
|
20308 | 442 |
__qCheckedNew(newPoint, sizeof(struct __Point)); |
443 |
__InstPtr(newPoint)->o_class = self; __qSTORE(newPoint, self); |
|
444 |
__PointInstPtr(newPoint)->p_x = newX; |
|
445 |
__PointInstPtr(newPoint)->p_y = newY; |
|
446 |
if (! __bothSmallInteger(newX, newY)) { |
|
447 |
spc = __qSpace(newPoint); |
|
448 |
__STORE_SPC(newPoint, newX, spc); |
|
449 |
__STORE_SPC(newPoint, newY, spc); |
|
450 |
} |
|
451 |
RETURN ( newPoint ); |
|
452 |
} |
|
17238 | 453 |
} |
454 |
%}. |
|
8076 | 455 |
^ Point x:width y:height |
637 | 456 |
! |
457 |
||
458 |
extent:aPoint |
|
459 |
"set the extent from the argument, aPoint with width taken from aPoint x |
|
4860 | 460 |
and height taken from aPoint y. |
461 |
warning: destructive" |
|
637 | 462 |
|
463 |
width := aPoint x. |
|
464 |
height := aPoint y |
|
465 |
! |
|
466 |
||
467 |
height |
|
468 |
"return the height of the rectangle" |
|
469 |
||
470 |
^ height |
|
471 |
! |
|
472 |
||
2441 | 473 |
height:aNumber |
474 |
"change the height of the rectangle. |
|
4860 | 475 |
logically, this changes the corner to get the given height. |
476 |
warning: destructive" |
|
2441 | 477 |
|
478 |
height := aNumber |
|
479 |
||
480 |
"Created: 3.3.1997 / 16:33:22 / cg" |
|
481 |
! |
|
482 |
||
637 | 483 |
left |
484 |
"return the x-coordinate of the top-left origin" |
|
485 |
||
486 |
^ left |
|
487 |
! |
|
488 |
||
489 |
left:aNumber |
|
7176
d61fa308f4c4
care for as-yet-uninitialized instvars in left:/top:
Claus Gittinger <cg@exept.de>
parents:
6065
diff
changeset
|
490 |
"set the left edge, adjust width - warning: destructive" |
637 | 491 |
|
7176
d61fa308f4c4
care for as-yet-uninitialized instvars in left:/top:
Claus Gittinger <cg@exept.de>
parents:
6065
diff
changeset
|
492 |
left notNil ifTrue:[ |
20308 | 493 |
"adjust width" |
494 |
width := width + (left - aNumber). |
|
7176
d61fa308f4c4
care for as-yet-uninitialized instvars in left:/top:
Claus Gittinger <cg@exept.de>
parents:
6065
diff
changeset
|
495 |
]. |
637 | 496 |
left := aNumber |
497 |
! |
|
498 |
||
1 | 499 |
left:newLeft right:right top:newTop bottom:bottom |
4860 | 500 |
"set the rectangle given left, top, right and bottom coordinates. |
501 |
warning: destructive" |
|
1 | 502 |
|
503 |
left := newLeft. |
|
504 |
top := newTop. |
|
505 |
width := right - left. |
|
506 |
height := bottom - top |
|
507 |
! |
|
508 |
||
1356 | 509 |
left:newLeft top:newTop extent:extent |
4860 | 510 |
"set the rectangle given left, top, coordinates and an extent. |
511 |
warning: destructive" |
|
1356 | 512 |
|
513 |
left := newLeft. |
|
514 |
top := newTop. |
|
515 |
width := extent x. |
|
516 |
height := extent y |
|
517 |
||
518 |
"Created: 8.5.1996 / 20:55:10 / cg" |
|
519 |
! |
|
520 |
||
1 | 521 |
left:newLeft top:newTop right:right bottom:bottom |
4860 | 522 |
"set the rectangle given left, top, right and bottom coordinates. |
523 |
warning: destructive" |
|
1 | 524 |
|
525 |
left := newLeft. |
|
526 |
top := newTop. |
|
527 |
width := right - left. |
|
528 |
height := bottom - top |
|
529 |
! |
|
530 |
||
531 |
left:newLeft top:newTop width:newWidth height:newHeight |
|
4860 | 532 |
"set the rectangle given left, top coordinates and width, height. |
533 |
warning: destructive" |
|
1 | 534 |
|
535 |
left := newLeft. |
|
536 |
top := newTop. |
|
537 |
width := newWidth. |
|
538 |
height := newHeight |
|
539 |
! |
|
540 |
||
637 | 541 |
leftCenter |
542 |
"return the left center point" |
|
543 |
||
1453
eb73ecdb25ab
center methods should not use //
Claus Gittinger <cg@exept.de>
parents:
1443
diff
changeset
|
544 |
^ left @ (top + (height / 2)) |
eb73ecdb25ab
center methods should not use //
Claus Gittinger <cg@exept.de>
parents:
1443
diff
changeset
|
545 |
|
eb73ecdb25ab
center methods should not use //
Claus Gittinger <cg@exept.de>
parents:
1443
diff
changeset
|
546 |
"Modified: 4.6.1996 / 16:04:01 / cg" |
637 | 547 |
! |
548 |
||
549 |
origin |
|
550 |
"return the origin" |
|
551 |
||
17238 | 552 |
%{ /* NOCONTEXT */ |
553 |
||
554 |
/* |
|
555 |
* claus: I am no longer certain, if this primitive is worth the effort |
|
556 |
*/ |
|
557 |
if (__CanDoQuickNew(sizeof(struct __Point))) { /* OBJECT ALLOCATION */ |
|
20308 | 558 |
if (self == @global(Rectangle)) { |
559 |
OBJ newPoint; |
|
560 |
int spc; |
|
561 |
OBJ newX = __INST(left); |
|
562 |
OBJ newY = __INST(top); |
|
17238 | 563 |
|
20308 | 564 |
__qCheckedNew(newPoint, sizeof(struct __Point)); |
565 |
__InstPtr(newPoint)->o_class = self; __qSTORE(newPoint, self); |
|
566 |
__PointInstPtr(newPoint)->p_x = newX; |
|
567 |
__PointInstPtr(newPoint)->p_y = newY; |
|
568 |
if (! __bothSmallInteger(newX, newY)) { |
|
569 |
spc = __qSpace(newPoint); |
|
570 |
__STORE_SPC(newPoint, newX, spc); |
|
571 |
__STORE_SPC(newPoint, newY, spc); |
|
572 |
} |
|
573 |
RETURN ( newPoint ); |
|
574 |
} |
|
17238 | 575 |
} |
576 |
%}. |
|
8076 | 577 |
^ Point x:left y:top |
637 | 578 |
! |
579 |
||
1 | 580 |
origin:aPoint |
4860 | 581 |
"set the top-left origin. The corner remains unchanged. |
582 |
warning: destructive" |
|
77 | 583 |
|
584 |
|newTop newLeft| |
|
1 | 585 |
|
7176
d61fa308f4c4
care for as-yet-uninitialized instvars in left:/top:
Claus Gittinger <cg@exept.de>
parents:
6065
diff
changeset
|
586 |
newLeft := aPoint x. |
77 | 587 |
newTop := aPoint y. |
7176
d61fa308f4c4
care for as-yet-uninitialized instvars in left:/top:
Claus Gittinger <cg@exept.de>
parents:
6065
diff
changeset
|
588 |
left notNil ifTrue:[ |
20308 | 589 |
width := width + (left - newLeft). |
7176
d61fa308f4c4
care for as-yet-uninitialized instvars in left:/top:
Claus Gittinger <cg@exept.de>
parents:
6065
diff
changeset
|
590 |
]. |
d61fa308f4c4
care for as-yet-uninitialized instvars in left:/top:
Claus Gittinger <cg@exept.de>
parents:
6065
diff
changeset
|
591 |
top notNil ifTrue:[ |
20308 | 592 |
height := height + (top - newTop). |
7176
d61fa308f4c4
care for as-yet-uninitialized instvars in left:/top:
Claus Gittinger <cg@exept.de>
parents:
6065
diff
changeset
|
593 |
]. |
77 | 594 |
left := newLeft. |
595 |
top := newTop |
|
1 | 596 |
! |
597 |
||
637 | 598 |
origin:origin corner:corner |
4860 | 599 |
"set both origin and corner - warning: destructive" |
637 | 600 |
|
601 |
left := origin x. |
|
602 |
top := origin y. |
|
603 |
width := corner x - left. |
|
604 |
height := corner y - top |
|
605 |
! |
|
606 |
||
607 |
origin:origin extent:extent |
|
4860 | 608 |
"set both origin and extent - warning: destructive" |
637 | 609 |
|
610 |
left := origin x. |
|
611 |
top := origin y. |
|
612 |
width := extent x. |
|
613 |
height := extent y |
|
614 |
! |
|
1 | 615 |
|
1356 | 616 |
origin:origin width:w height:h |
20308 | 617 |
"set both origin and extent; |
4860 | 618 |
the extent is given as individual width and height. |
619 |
warning: destructive" |
|
1356 | 620 |
|
621 |
left := origin x. |
|
622 |
top := origin y. |
|
623 |
width := w. |
|
624 |
height := h |
|
625 |
||
626 |
"Created: 8.5.1996 / 20:53:53 / cg" |
|
627 |
! |
|
628 |
||
637 | 629 |
right |
630 |
"return the x coordinate of the right" |
|
631 |
||
632 |
^ (left + width) |
|
633 |
! |
|
634 |
||
635 |
right:aNumber |
|
4860 | 636 |
"set the right edge - warning: destructive" |
637 | 637 |
|
638 |
width := aNumber - left |
|
639 |
! |
|
640 |
||
641 |
rightCenter |
|
642 |
"return the right center point" |
|
643 |
||
1453
eb73ecdb25ab
center methods should not use //
Claus Gittinger <cg@exept.de>
parents:
1443
diff
changeset
|
644 |
^ (left + width) @ (top + (height / 2)) |
eb73ecdb25ab
center methods should not use //
Claus Gittinger <cg@exept.de>
parents:
1443
diff
changeset
|
645 |
|
eb73ecdb25ab
center methods should not use //
Claus Gittinger <cg@exept.de>
parents:
1443
diff
changeset
|
646 |
"Modified: 4.6.1996 / 16:09:05 / cg" |
637 | 647 |
! |
648 |
||
3269 | 649 |
setLeft:newLeft |
4860 | 650 |
"set left without adjusting width - warning: destructive" |
3269 | 651 |
|
652 |
left := newLeft. |
|
653 |
||
654 |
"Modified: / 22.1.1998 / 09:40:35 / av" |
|
655 |
"Created: / 3.2.1998 / 19:01:06 / cg" |
|
656 |
! |
|
657 |
||
4860 | 658 |
setOrigin:newOrigin corner:newCorner |
659 |
"set the rectangles dimensions - warning: destructive" |
|
660 |
||
661 |
self origin:newOrigin corner:newCorner |
|
662 |
! |
|
663 |
||
3269 | 664 |
setTop:newTop |
4860 | 665 |
"set top without adjusting height - warning: destructive" |
3269 | 666 |
|
667 |
top := newTop. |
|
668 |
||
669 |
"Modified: / 20.1.1998 / 18:25:35 / av" |
|
670 |
"Created: / 3.2.1998 / 19:01:01 / cg" |
|
671 |
! |
|
672 |
||
637 | 673 |
top |
674 |
"return the y-coordinate of the top-left" |
|
675 |
||
676 |
^ top |
|
1 | 677 |
! |
678 |
||
679 |
top:aNumber |
|
7176
d61fa308f4c4
care for as-yet-uninitialized instvars in left:/top:
Claus Gittinger <cg@exept.de>
parents:
6065
diff
changeset
|
680 |
"set the top edge, adjust height - warning: destructive" |
1 | 681 |
|
7176
d61fa308f4c4
care for as-yet-uninitialized instvars in left:/top:
Claus Gittinger <cg@exept.de>
parents:
6065
diff
changeset
|
682 |
top notNil ifTrue:[ |
20308 | 683 |
"adjust height" |
684 |
height := height + (top - aNumber). |
|
7176
d61fa308f4c4
care for as-yet-uninitialized instvars in left:/top:
Claus Gittinger <cg@exept.de>
parents:
6065
diff
changeset
|
685 |
]. |
1 | 686 |
top := aNumber |
687 |
! |
|
688 |
||
637 | 689 |
topCenter |
690 |
"return the top center point" |
|
1 | 691 |
|
1453
eb73ecdb25ab
center methods should not use //
Claus Gittinger <cg@exept.de>
parents:
1443
diff
changeset
|
692 |
^ (left + (width / 2)) @ top |
eb73ecdb25ab
center methods should not use //
Claus Gittinger <cg@exept.de>
parents:
1443
diff
changeset
|
693 |
|
eb73ecdb25ab
center methods should not use //
Claus Gittinger <cg@exept.de>
parents:
1443
diff
changeset
|
694 |
"Modified: 4.6.1996 / 16:09:09 / cg" |
1 | 695 |
! |
696 |
||
637 | 697 |
topLeft |
698 |
"return the top-left point - the same as origin" |
|
77 | 699 |
|
637 | 700 |
^ left @ top |
77 | 701 |
! |
702 |
||
703 |
topLeft:aPoint |
|
704 |
"Set the top and left edges. |
|
4860 | 705 |
The bottom right remains unchanged. |
706 |
warning: destructive" |
|
77 | 707 |
|
708 |
|newTop| |
|
709 |
||
710 |
newTop := aPoint y. |
|
711 |
height := height + (top - newTop). |
|
712 |
top := newTop. |
|
713 |
width := aPoint x - left |
|
714 |
! |
|
715 |
||
637 | 716 |
topRight |
717 |
"return the top-right point" |
|
718 |
||
719 |
^ (left + width) @ top |
|
720 |
! |
|
721 |
||
77 | 722 |
topRight:aPoint |
20308 | 723 |
"Set the top and right edges. |
4860 | 724 |
The bottom left remains unchanged. |
725 |
warning: destructive" |
|
77 | 726 |
|
727 |
|newTop| |
|
728 |
||
729 |
newTop := aPoint y. |
|
730 |
height := height + (top - newTop). |
|
731 |
top := newTop. |
|
732 |
width := aPoint x - left |
|
733 |
! |
|
734 |
||
12946 | 735 |
vertices |
736 |
"return the array containing my points as a closed polygon (for Polygon compatibility)" |
|
737 |
||
20308 | 738 |
^ Array |
739 |
with:(self topLeft) |
|
740 |
with:(self topRight) |
|
741 |
with:(self bottomRight) |
|
742 |
with:(self bottomLeft) |
|
743 |
with:(self topLeft) |
|
12946 | 744 |
|
745 |
" |
|
20308 | 746 |
(Rectangle origin:100@100 extent:20@30) vertices |
747 |
(Rectangle origin:100@100 extent:20@30) asPolygon vertices |
|
12946 | 748 |
" |
749 |
||
750 |
"Modified: / 16-07-2010 / 16:59:16 / cg" |
|
751 |
! |
|
752 |
||
77 | 753 |
width |
754 |
"return the width of the rectangle" |
|
755 |
||
756 |
^ width |
|
2441 | 757 |
! |
758 |
||
759 |
width:aNumber |
|
760 |
"change the width of the rectangle. |
|
4860 | 761 |
logically, this changes the corner to get the given width. |
762 |
warning: destructive" |
|
2441 | 763 |
|
764 |
width := aNumber |
|
765 |
||
766 |
"Created: 3.3.1997 / 16:33:48 / cg" |
|
1 | 767 |
! ! |
768 |
||
769 |
!Rectangle methodsFor:'comparing'! |
|
770 |
||
771 |
= aRectangle |
|
772 |
"return true, if the argument aRectangle represents the same |
|
773 |
rectangle as the receiver" |
|
774 |
||
775 |
%{ /* NOCONTEXT */ |
|
59 | 776 |
/* |
1151 | 777 |
* because rectangles are often compared in the graphics code, |
59 | 778 |
* handle the common case quickly |
779 |
*/ |
|
20308 | 780 |
if (__isNonNilObject(aRectangle) |
329 | 781 |
&& __qClass(aRectangle) == Rectangle) { |
1133 | 782 |
if ((__InstPtr(self)->i_instvars[0] == __InstPtr(aRectangle)->i_instvars[0]) |
783 |
&& (__InstPtr(self)->i_instvars[1] == __InstPtr(aRectangle)->i_instvars[1]) |
|
784 |
&& (__InstPtr(self)->i_instvars[2] == __InstPtr(aRectangle)->i_instvars[2]) |
|
785 |
&& (__InstPtr(self)->i_instvars[3] == __InstPtr(aRectangle)->i_instvars[3])) { |
|
186 | 786 |
RETURN ( true ); |
787 |
} |
|
1133 | 788 |
if (__bothSmallInteger(__InstPtr(self)->i_instvars[0], __InstPtr(aRectangle)->i_instvars[0]) |
789 |
&& __bothSmallInteger(__InstPtr(self)->i_instvars[1], __InstPtr(aRectangle)->i_instvars[1]) |
|
790 |
&& __bothSmallInteger(__InstPtr(self)->i_instvars[2], __InstPtr(aRectangle)->i_instvars[2]) |
|
791 |
&& __bothSmallInteger(__InstPtr(self)->i_instvars[3], __InstPtr(aRectangle)->i_instvars[3])) { |
|
325 | 792 |
RETURN ( false ); |
793 |
} |
|
1 | 794 |
} |
1151 | 795 |
%}. |
259 | 796 |
(aRectangle species ~~ self species) ifTrue:[^ false]. |
1 | 797 |
|
798 |
left = aRectangle left ifFalse:[^ false]. |
|
799 |
top = aRectangle top ifFalse:[^ false]. |
|
800 |
width = aRectangle width ifFalse:[^ false]. |
|
801 |
height = aRectangle height ifFalse:[^ false]. |
|
802 |
^ true |
|
59 | 803 |
! |
804 |
||
805 |
hash |
|
806 |
"return an integer useful for hashing - |
|
807 |
redefined since = is redefined here" |
|
808 |
||
809 |
^ ((left hash bitShift:16) bitXor:(top hash bitShift:16)) |
|
810 |
+ ((width hash) bitXor:(height hash)) |
|
1 | 811 |
! ! |
812 |
||
356 | 813 |
!Rectangle methodsFor:'converting'! |
814 |
||
815 |
asFractionalLayout |
|
816 |
"return a layoutFrame in which fractions (top, left, bottom, right) |
|
817 |
are taken from corresponding edges of the receiver. |
|
818 |
You have to make certain that those are in 0..1." |
|
819 |
||
820 |
|l| |
|
821 |
||
822 |
l := LayoutFrame new. |
|
823 |
l |
|
20606 | 824 |
leftFraction:(self left); |
825 |
rightFraction:(self right); |
|
826 |
topFraction:(self top); |
|
827 |
bottomFraction:(self bottom). |
|
356 | 828 |
^ l |
829 |
||
830 |
" |
|
20308 | 831 |
(0.5@0.5 corner:0.75@0.75) asFractionalLayout |
832 |
(0.5@0.5 corner:0.75@0.75) asOffsetLayout |
|
833 |
(0.5@0.5 corner:0.75@0.75) asLayout |
|
20606 | 834 |
(0@0 corner:1@1) asLayout |
835 |
(0@0 corner:1@1) asFractionalLayout |
|
836 |
(0@0 corner:1@1) asOffsetLayout |
|
356 | 837 |
" |
838 |
! |
|
839 |
||
840 |
asLayout |
|
841 |
"return a layoutFrame in which offsets (top, left, bottom, right) |
|
842 |
are taken from corresponding edges of the receiver. |
|
1151 | 843 |
If all values are between 0.0 .. 1.0, a fractionalLayout is created, |
356 | 844 |
otherwise, an offsetLayout" |
845 |
||
1454 | 846 |
|newLayout l r t b| |
356 | 847 |
|
1151 | 848 |
newLayout := LayoutFrame new. |
1454 | 849 |
l := left. |
850 |
r := (self right). |
|
851 |
t := top. |
|
852 |
b := (self bottom). |
|
853 |
((l between:0.0 and:1.0) |
|
854 |
and:[(r between:0.0 and:1.0) |
|
855 |
and:[(t between:0.0 and:1.0) |
|
856 |
and:[(b between:0.0 and:1.0)]]]) ifTrue:[ |
|
20308 | 857 |
newLayout |
858 |
leftFraction:l; |
|
859 |
rightFraction:r; |
|
860 |
topFraction:t; |
|
861 |
bottomFraction:b. |
|
356 | 862 |
] ifFalse:[ |
20308 | 863 |
newLayout |
864 |
leftOffset:l; |
|
865 |
rightFraction:0 offset:r; |
|
866 |
topOffset:t; |
|
867 |
bottomFraction:0 offset:b. |
|
356 | 868 |
]. |
1151 | 869 |
^ newLayout |
356 | 870 |
|
871 |
" |
|
20308 | 872 |
(0.5@0.5 corner:0.75@0.75) asFractionalLayout |
873 |
(0.5@0.5 corner:0.75@0.75) asOffsetLayout |
|
874 |
(0.5@0.5 corner:0.75@0.75) asLayout |
|
875 |
(0@0 corner:1@1) asLayout |
|
876 |
(0@0 corner:1@1) asFractionalLayout |
|
877 |
(0@0 corner:1@1) asOffsetLayout |
|
356 | 878 |
" |
1454 | 879 |
|
880 |
"Modified: 5.6.1996 / 00:45:46 / cg" |
|
356 | 881 |
! |
882 |
||
637 | 883 |
asOffsetLayout |
884 |
"return a layoutFrame in which offsets (top, left, bottom, right) |
|
885 |
are taken from corresponding edges of the receiver. |
|
886 |
You have to make certain that those are in 0..1." |
|
887 |
||
1151 | 888 |
|newLayout| |
637 | 889 |
|
1151 | 890 |
newLayout := LayoutFrame new. |
891 |
newLayout |
|
637 | 892 |
leftOffset:(self left); |
893 |
rightFraction:0 offset:(self right); |
|
894 |
topOffset:(self top); |
|
895 |
bottomFraction:0 offset:(self bottom). |
|
1151 | 896 |
^ newLayout |
637 | 897 |
|
898 |
" |
|
20308 | 899 |
(0.5@0.5 corner:0.75@0.75) asFractionalLayout |
900 |
(0.5@0.5 corner:0.75@0.75) asOffsetLayout |
|
901 |
(0.5@0.5 corner:0.75@0.75) asLayout |
|
637 | 902 |
|
20308 | 903 |
(10@10 corner:20@20) asFractionalLayout |
904 |
(10@10 corner:20@20) asOffsetLayout |
|
905 |
(10@10 corner:20@20) asLayout |
|
637 | 906 |
" |
907 |
||
908 |
! |
|
909 |
||
356 | 910 |
asPointArray |
911 |
"return an array containing my corners (clockwise) and |
|
912 |
the origin again as 5th element. Can be used to convert |
|
913 |
a rectangle into a polygon." |
|
914 |
||
915 |
|org| |
|
916 |
||
917 |
^ Array with:(org := self origin) |
|
918 |
with:self topRight |
|
919 |
with:self corner |
|
920 |
with:self bottomLeft |
|
20308 | 921 |
with:org |
356 | 922 |
|
923 |
" |
|
20308 | 924 |
(10@10 corner:100@100) asPointArray |
356 | 925 |
" |
926 |
! |
|
927 |
||
1354 | 928 |
asPolygon |
929 |
"return a polygon from the receiver" |
|
930 |
||
931 |
^ Polygon vertices:self asPointArray |
|
932 |
||
933 |
" |
|
934 |
(10@10 corner:100@100) asPolygon |
|
935 |
" |
|
936 |
||
937 |
"Modified: 8.5.1996 / 20:14:44 / cg" |
|
938 |
! |
|
939 |
||
637 | 940 |
fromLiteralArrayEncoding:encoding |
941 |
"read my values from an encoding. |
|
942 |
The encoding is supposed to be of the form: (Rectangle orgX orgY cornX cornY)" |
|
356 | 943 |
|
637 | 944 |
left := encoding at:2. |
945 |
top := encoding at:3. |
|
946 |
width := (encoding at:4) - left. |
|
947 |
height := (encoding at:5) - top |
|
948 |
||
356 | 949 |
|
950 |
" |
|
20308 | 951 |
Rectangle new fromLiteralArrayEncoding:#(Rectangle 100 200 300 500) |
356 | 952 |
" |
379 | 953 |
! |
954 |
||
421 | 955 |
literalArrayEncoding |
1245 | 956 |
"encode myself as an array, from which a copy of the receiver |
957 |
can be reconstructed with #decodeAsLiteralArray. |
|
421 | 958 |
The encoding is: (Rectangle orgX orgY cornX cornY)" |
959 |
||
960 |
^ Array |
|
20308 | 961 |
with:#Rectangle |
962 |
with:left |
|
963 |
with:top |
|
964 |
with:(left + width) |
|
965 |
with:(top + height) |
|
421 | 966 |
|
967 |
||
968 |
" |
|
20308 | 969 |
Rectangle new fromLiteralArrayEncoding:#(Rectangle 100 200 300 500) |
970 |
(100@200 corner:300@500) literalArrayEncoding |
|
421 | 971 |
" |
972 |
||
973 |
"Modified: 1.9.1995 / 02:16:54 / claus" |
|
1245 | 974 |
"Modified: 22.4.1996 / 13:00:36 / cg" |
421 | 975 |
! |
976 |
||
3488
6e1feb9ead68
prefRect may now be a valueHolder or block
Claus Gittinger <cg@exept.de>
parents:
3269
diff
changeset
|
977 |
rectangleRelativeTo:aRectangle preferred:prefRectHolder |
637 | 978 |
"compute a displayRectangle, treating the receiver like a |
20308 | 979 |
layoutFrame. |
1454 | 980 |
This allows rectangles to be used interchangable with Layouts." |
379 | 981 |
|
3488
6e1feb9ead68
prefRect may now be a valueHolder or block
Claus Gittinger <cg@exept.de>
parents:
3269
diff
changeset
|
982 |
^ (self asLayout) rectangleRelativeTo:aRectangle preferred:prefRectHolder |
379 | 983 |
|
984 |
" |
|
20308 | 985 |
(10@20 corner:20@30) rectangleRelativeTo:(0@0 corner:100@100) preferred:(0@0 corner:50@50) |
637 | 986 |
|
20308 | 987 |
(0.5@0.5) rectangleRelativeTo:(0@0 corner:100@100) preferred:(0@0 corner:50@50) |
379 | 988 |
" |
1454 | 989 |
|
3488
6e1feb9ead68
prefRect may now be a valueHolder or block
Claus Gittinger <cg@exept.de>
parents:
3269
diff
changeset
|
990 |
"Modified: / 27.5.1998 / 10:20:20 / cg" |
356 | 991 |
! ! |
992 |
||
186 | 993 |
!Rectangle methodsFor:'destructive rectangle operations'! |
994 |
||
356 | 995 |
expandBy:delta |
996 |
"destructively expanded the receiver in all directions |
|
997 |
by amount, a Point, Rectangle or Number. |
|
998 |
Warning: this is a destructive operation, modifying the receiver |
|
999 |
NOT returning a copy. You have to be certain to be the exclusive |
|
1000 |
owner of the receiver to avoid side effects. See also: #expandedBy:" |
|
1001 |
||
1002 |
|amountPoint deltaLeft deltaTop deltaWidth deltaHeight| |
|
1003 |
||
1004 |
delta isNumber ifTrue:[ |
|
1005 |
deltaLeft := deltaTop := delta. |
|
1006 |
deltaWidth := deltaHeight := delta * 2. |
|
1007 |
] ifFalse:[ |
|
1008 |
delta isRectangle ifTrue:[ |
|
1009 |
deltaLeft := delta left. |
|
1010 |
deltaTop := delta top. |
|
1011 |
deltaWidth := deltaLeft + delta right. |
|
1012 |
deltaHeight := deltaTop + delta bottom |
|
1013 |
] ifFalse:[ |
|
1014 |
amountPoint := delta asPoint. |
|
1015 |
deltaLeft := amountPoint x. |
|
1016 |
deltaTop := amountPoint y. |
|
1017 |
deltaWidth := deltaLeft * 2. |
|
1018 |
deltaHeight := deltaTop * 2. |
|
1019 |
] |
|
1020 |
]. |
|
1021 |
||
1022 |
left := (left - deltaLeft). |
|
1023 |
top := (top - deltaTop). |
|
1024 |
width := (width + deltaWidth). |
|
1025 |
height := (height + deltaHeight). |
|
1026 |
||
1027 |
" |
|
1028 |
|r| |
|
1029 |
r := Rectangle origin:10@10 corner:100@100. |
|
1030 |
r expandBy:5. |
|
1031 |
||
1032 |
r := Rectangle origin:10@10 corner:100@100. |
|
1033 |
r expandBy:(5 @ 0). |
|
1034 |
||
1035 |
r := Rectangle origin:10@10 corner:100@100. |
|
1036 |
r expandBy:(10 @ 10). |
|
1037 |
||
1038 |
r := Rectangle origin:10@10 corner:100@100. |
|
1039 |
r expandBy:( 10@10 corner:20@20 ) |
|
1040 |
" |
|
637 | 1041 |
! |
1042 |
||
1043 |
moveBy:aPoint |
|
1044 |
"destructively translate the rectangle by some distance. |
|
1045 |
sorry for the name inconsistency - but GNU-ST named it that way" |
|
1046 |
||
1047 |
left := left + aPoint x. |
|
1048 |
top := top + aPoint y |
|
1049 |
! |
|
1050 |
||
1051 |
moveTo:aPoint |
|
1052 |
"destructively translate the rectangle to have its origin at aPoint." |
|
1053 |
||
1054 |
|diff| |
|
1055 |
||
1056 |
diff := aPoint - self origin. |
|
1057 |
self origin:aPoint corner:self corner + diff |
|
1058 |
! |
|
1059 |
||
1060 |
scaleBy:scale |
|
1061 |
"scale the receiver rectangle by scale (a Number or Point). |
|
20308 | 1062 |
This is destructive (modifies the receiver, not a copy) and |
1063 |
should only be used if you know, that you are the exclusive owner |
|
637 | 1064 |
of the receiver. (use scaledBy if in doubt)" |
1065 |
||
1066 |
|scalePoint sx sy| |
|
1067 |
||
1068 |
(scale isMemberOf:Point) ifTrue:[ "type hint to stc" |
|
1069 |
sx := scale x. |
|
1070 |
sy := scale y |
|
1071 |
] ifFalse:[ |
|
1072 |
scalePoint := scale asPoint. |
|
1073 |
sx := scalePoint x. |
|
1074 |
sy := scalePoint y |
|
1075 |
]. |
|
1076 |
width := width * sx. |
|
1077 |
height := height * sy. |
|
1078 |
left := left * sx. |
|
1079 |
top := top * sy |
|
1080 |
||
1081 |
" |
|
20308 | 1082 |
(Rectangle origin:10@10 corner:50@50) scaleBy:2 |
637 | 1083 |
" |
1084 |
||
1085 |
"its destructive:" |
|
1086 |
" |
|
1087 |
|r1 r2| |
|
1088 |
||
1089 |
r1 := Rectangle origin:10@10 corner:50@50. |
|
1090 |
r2 := r1 scaleBy:2. |
|
20308 | 1091 |
r1 |
637 | 1092 |
" |
1093 |
! |
|
1094 |
||
1095 |
translateBy:amount |
|
20308 | 1096 |
"translate (i.e. move) the receiver rectangle |
637 | 1097 |
by amount, a Point or Number. |
20308 | 1098 |
This is destructive (modifies the receiver, not a copy) and |
1099 |
should only be used if you know, that you are the exclusive owner |
|
637 | 1100 |
of the receiver. (use translatedBy if in doubt)" |
1101 |
||
1102 |
|amountPoint| |
|
1103 |
||
1104 |
(amount isMemberOf:Point) ifTrue:[ "type hint to stc" |
|
1105 |
left := left + amount x. |
|
1106 |
top := top + amount y |
|
1107 |
] ifFalse:[ |
|
1108 |
amountPoint := amount asPoint. |
|
1109 |
left := left + amountPoint x. |
|
1110 |
top := top + amountPoint y |
|
1111 |
] |
|
1112 |
||
1113 |
" |
|
1114 |
(Rectangle origin:10@10 corner:50@50) translateBy:10 |
|
1115 |
" |
|
1116 |
||
1117 |
"its destructive:" |
|
1118 |
" |
|
1119 |
|r1 r2| |
|
1120 |
||
1121 |
r1 := Rectangle origin:10@10 corner:50@50. |
|
1122 |
r2 := r1 translateBy:10. |
|
20308 | 1123 |
r1 |
637 | 1124 |
" |
1125 |
! ! |
|
1126 |
||
1127 |
!Rectangle methodsFor:'displaying'! |
|
1128 |
||
1129 |
displayFilledOn:aGC |
|
20308 | 1130 |
"display a filled rectangle as represented by the receiver in |
1314 | 1131 |
the graphicsContext, aGC" |
1132 |
||
637 | 1133 |
aGC fillRectangleX:left y:top width:width height:height |
1134 |
||
1135 |
" |
|
1136 |
|v| |
|
1137 |
||
1349
275388fd5271
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
1314
diff
changeset
|
1138 |
v := View new openAndWait. |
637 | 1139 |
|
1140 |
(Rectangle origin:10@10 corner:50@50) displayFilledOn:v |
|
1141 |
" |
|
1314 | 1142 |
|
1349
275388fd5271
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
1314
diff
changeset
|
1143 |
"Modified: 8.5.1996 / 14:40:42 / cg" |
637 | 1144 |
! |
1145 |
||
1146 |
displayStrokedOn:aGC |
|
20308 | 1147 |
"display an unfilled rectangle as represented by the receiver in |
1314 | 1148 |
the graphicsContext, aGC" |
1149 |
||
637 | 1150 |
aGC displayRectangleX:left y:top width:width height:height |
1151 |
||
1152 |
" |
|
1153 |
|v| |
|
1154 |
||
1349
275388fd5271
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
1314
diff
changeset
|
1155 |
v := View new openAndWait. |
637 | 1156 |
|
1157 |
(Rectangle origin:10@10 corner:50@50) displayStrokedOn:v |
|
1158 |
" |
|
1314 | 1159 |
|
1349
275388fd5271
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
1314
diff
changeset
|
1160 |
"Modified: 8.5.1996 / 14:40:53 / cg" |
637 | 1161 |
! ! |
1162 |
||
15730 | 1163 |
|
637 | 1164 |
!Rectangle methodsFor:'printing & storing'! |
1165 |
||
1166 |
printOn:aStream |
|
1167 |
"print the receiver on aStream" |
|
1168 |
||
1169 |
aStream nextPutAll:(self class name). |
|
1170 |
aStream nextPutAll:' origin:'. |
|
1171 |
(self origin) printOn:aStream. |
|
1443 | 1172 |
aStream nextPutAll:' extent:'. |
1173 |
(self extent) printOn:aStream |
|
1174 |
||
1175 |
"Modified: 29.5.1996 / 00:17:39 / cg" |
|
637 | 1176 |
! |
1177 |
||
1178 |
storeOn:aStream |
|
1179 |
"store the receiver on aStream; i.e. print an expression which will |
|
1180 |
reconstruct the receiver" |
|
1181 |
||
1182 |
aStream nextPut:$(. |
|
1183 |
aStream nextPutAll:(self class name). |
|
1184 |
aStream nextPutAll:' origin:'. |
|
1185 |
(self origin) storeOn:aStream. |
|
1443 | 1186 |
aStream nextPutAll:' extent:'. |
1187 |
(self extent) storeOn:aStream. |
|
637 | 1188 |
aStream nextPut:$) |
1443 | 1189 |
|
1190 |
"Modified: 29.5.1996 / 00:17:42 / cg" |
|
637 | 1191 |
! ! |
1192 |
||
1193 |
!Rectangle methodsFor:'queries'! |
|
1194 |
||
1349
275388fd5271
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
1314
diff
changeset
|
1195 |
bounds |
275388fd5271
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
1314
diff
changeset
|
1196 |
"return the smallest enclosing rectangle" |
275388fd5271
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
1314
diff
changeset
|
1197 |
|
275388fd5271
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
1314
diff
changeset
|
1198 |
^ self |
275388fd5271
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
1314
diff
changeset
|
1199 |
|
275388fd5271
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
1314
diff
changeset
|
1200 |
"Created: 8.5.1996 / 13:56:24 / cg" |
275388fd5271
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
1314
diff
changeset
|
1201 |
"Modified: 8.5.1996 / 14:06:38 / cg" |
275388fd5271
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
1314
diff
changeset
|
1202 |
! |
275388fd5271
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
1314
diff
changeset
|
1203 |
|
2381
10af98758a49
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
2373
diff
changeset
|
1204 |
computeBounds |
10af98758a49
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
2373
diff
changeset
|
1205 |
"return the smallest enclosing rectangle" |
10af98758a49
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
2373
diff
changeset
|
1206 |
|
10af98758a49
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
2373
diff
changeset
|
1207 |
^ self |
10af98758a49
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
2373
diff
changeset
|
1208 |
|
10af98758a49
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
2373
diff
changeset
|
1209 |
"Modified: 8.5.1996 / 14:06:38 / cg" |
10af98758a49
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
2373
diff
changeset
|
1210 |
"Created: 12.2.1997 / 11:44:45 / cg" |
10af98758a49
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
2373
diff
changeset
|
1211 |
! |
10af98758a49
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
2373
diff
changeset
|
1212 |
|
11743 | 1213 |
contains:aRectangle |
1214 |
"return true, if the argument, aRectangle is equal to or |
|
1215 |
is contained fully within the receiver" |
|
1216 |
||
1217 |
%{ /* NOCONTEXT */ |
|
1218 |
/* |
|
1219 |
* claus: this may be often called by objectView |
|
19960 | 1220 |
* the primitive code below (although looking ugly) |
11743 | 1221 |
* speeds that up by almost a factor of 2 ... |
1222 |
*/ |
|
1223 |
OBJ slf = self; |
|
1224 |
OBJ rct = aRectangle; |
|
1225 |
||
20308 | 1226 |
if (__isNonNilObject(rct) |
11743 | 1227 |
&& __qClass(rct) == Rectangle) { |
20308 | 1228 |
OBJ r_l, r_w, r_t, r_h, l, w, t, h; |
1229 |
INT r_ir, r_il, r_ib, r_it; |
|
1230 |
INT il, it, ir, ib, iw, ih; |
|
11743 | 1231 |
|
20308 | 1232 |
r_l = __OINST(rct, left); |
1233 |
l = __OINST(slf, left); |
|
1234 |
if (__bothSmallInteger(r_l, l)) { |
|
11743 | 1235 |
#ifndef POSITIVE_ADDRESSES /* tag in low-bit */ |
20308 | 1236 |
il = (INT)(l); |
1237 |
r_il = (INT)(r_l); |
|
11743 | 1238 |
#else |
20308 | 1239 |
il = __intVal(l); |
1240 |
r_il = __intVal(r_l); |
|
11743 | 1241 |
#endif |
20308 | 1242 |
if (il > r_il) { RETURN (false); } /* left > aRectangle left */ |
11743 | 1243 |
|
20308 | 1244 |
r_t = __OINST(rct, top); |
1245 |
t = __OINST(slf, top); |
|
1246 |
if (__bothSmallInteger(r_t, t)) { |
|
11743 | 1247 |
#ifndef POSITIVE_ADDRESSES /* tag in low-bit */ |
20308 | 1248 |
it = (INT)(t); |
1249 |
r_it = (INT)(r_t); |
|
11743 | 1250 |
#else |
20308 | 1251 |
it = __intVal(t); |
1252 |
r_it = __intVal(r_t); |
|
11743 | 1253 |
#endif |
20308 | 1254 |
if (it > r_it) { RETURN (false); } /* top > aRectangle top */ |
11743 | 1255 |
|
20308 | 1256 |
r_w = __OINST(rct, width); |
1257 |
w = __OINST(slf, width); |
|
1258 |
if (__bothSmallInteger(r_w, w)) { |
|
11743 | 1259 |
#ifndef POSITIVE_ADDRESSES /* tag in low-bit */ |
20308 | 1260 |
ir = il + (INT)(w); |
1261 |
r_ir = r_il + (INT)(r_w); |
|
11743 | 1262 |
#else |
20308 | 1263 |
ir = il + __intVal(w); |
1264 |
r_ir = r_il + __intVal(r_w); |
|
11743 | 1265 |
#endif |
20308 | 1266 |
if (ir < r_ir) { RETURN (false); } /* (left + width) < aRectangle right */ |
11743 | 1267 |
|
20308 | 1268 |
r_h = __OINST(rct, height); |
1269 |
h = __OINST(slf, height); |
|
1270 |
if (__bothSmallInteger(r_h, h)) { |
|
11743 | 1271 |
#ifndef POSITIVE_ADDRESSES /* tag in low-bit */ |
20308 | 1272 |
ib = it + (INT)(h); |
1273 |
r_ib = r_it + (INT)(r_h); |
|
11743 | 1274 |
#else |
20308 | 1275 |
ib = it + __intVal(h); |
1276 |
r_ib = r_it + __intVal(r_h); |
|
11743 | 1277 |
#endif |
20308 | 1278 |
if (ib < r_ib) { RETURN (false); } /* (top + height) < aRectangle bottom */ |
1279 |
RETURN (true); |
|
1280 |
} |
|
1281 |
} |
|
1282 |
} |
|
1283 |
} |
|
11743 | 1284 |
} |
1285 |
%}. |
|
1286 |
(left <= aRectangle left) ifTrue:[ |
|
1287 |
((left + width) >= aRectangle right) ifTrue:[ |
|
20308 | 1288 |
(top <= aRectangle top) ifTrue:[ |
1289 |
((top + height) >= aRectangle bottom) ifTrue:[ |
|
1290 |
^ true |
|
1291 |
] |
|
1292 |
] |
|
11743 | 1293 |
] |
1294 |
]. |
|
1295 |
^ false |
|
1296 |
||
1297 |
" |
|
20308 | 1298 |
(0@0 corner:100@100) contains:(10@10 corner:90@90) |
1299 |
(0@0 corner:100@100) contains:(10@10 corner:100@100) |
|
1300 |
(0@0 corner:100@100) contains:(10@10 corner:110@100) |
|
1301 |
(0@0 corner:100@100) contains:(10@10 corner:100@110) |
|
1302 |
(10@10 corner:100@100) contains:(0@10 corner:100@100) |
|
11743 | 1303 |
" |
1304 |
! |
|
1305 |
||
1306 |
containsPoint:aPoint |
|
1307 |
"return true, if the argument, aPoint is contained in the receiver" |
|
1308 |
||
1309 |
|px py| |
|
1310 |
||
1311 |
px := aPoint x. |
|
1312 |
(px < left) ifTrue:[^ false]. |
|
1313 |
(px > (left + width)) ifTrue:[^ false]. |
|
1314 |
py := aPoint y. |
|
1315 |
(py < top) ifTrue:[^ false]. |
|
1316 |
(py > (top + height)) ifTrue:[^ false]. |
|
1317 |
^ true |
|
1318 |
! |
|
1319 |
||
1320 |
containsPointX:x y:y |
|
1321 |
"return true, if the point defined by x@y is contained in the receiver. |
|
1322 |
This is the same as containsPoint:, but can be used if the coordinates |
|
1323 |
are already available as separate numbers to avoid useless creation of a |
|
1324 |
temporary point." |
|
1325 |
||
1326 |
(x < left) ifTrue:[^ false]. |
|
1327 |
(x > (left + width)) ifTrue:[^ false]. |
|
1328 |
(y < top) ifTrue:[^ false]. |
|
1329 |
(y > (top + height)) ifTrue:[^ false]. |
|
1330 |
^ true |
|
1331 |
! |
|
1332 |
||
4890 | 1333 |
corners |
1334 |
"Return an array of corner points" |
|
1335 |
||
1336 |
^ Array |
|
20308 | 1337 |
with: self topLeft |
1338 |
with: self bottomLeft |
|
1339 |
with: self bottomRight |
|
1340 |
with: self topRight |
|
4890 | 1341 |
|
1342 |
||
1343 |
! |
|
1344 |
||
4894 | 1345 |
innerCorners |
1346 |
"Return an array of inner corner points, |
|
1347 |
ie, the most extreme pixels included. |
|
1348 |
Added for Aqueak compatibility." |
|
1349 |
||
1350 |
^ (self topLeft corner:(self bottomRight - (1@1))) corners |
|
1351 |
||
1352 |
" |
|
20308 | 1353 |
(10@10 corner:100@100) corners |
1354 |
(10@10 corner:100@100) innerCorners |
|
4894 | 1355 |
" |
1356 |
! |
|
1357 |
||
11743 | 1358 |
intersects:aRectangle |
1359 |
"return true, if the intersection between the argument, aRectangle |
|
1360 |
and the receiver is not empty" |
|
1361 |
||
1362 |
|b r| |
|
1363 |
||
1364 |
%{ /* NOCONTEXT */ |
|
1365 |
/* |
|
1366 |
* claus: this is one of the mostly called methods in |
|
19960 | 1367 |
* the objectView - the primitive code below (although looking ugly) |
11743 | 1368 |
* speeds up drawing by almost a factor of 2 ... |
1369 |
*/ |
|
1370 |
OBJ slf = self; |
|
1371 |
OBJ rct = aRectangle; |
|
1372 |
||
20308 | 1373 |
if (__isNonNilObject(rct) |
11743 | 1374 |
&& __qClass(rct) == Rectangle) { |
20308 | 1375 |
OBJ r_l, r_w, r_t, r_h, l, w, t, h; |
1376 |
INT r_ir, r_il, r_ib, r_it; |
|
1377 |
INT il, it, ir, ib, iw, ih; |
|
11743 | 1378 |
|
20308 | 1379 |
r_l = __OINST(rct, left); |
1380 |
r_w = __OINST(rct, width); |
|
11743 | 1381 |
|
20308 | 1382 |
if (__bothSmallInteger(r_l, r_w)) { |
1383 |
r_t = __OINST(rct, top); |
|
1384 |
r_h = __OINST(rct, height); |
|
11743 | 1385 |
|
20308 | 1386 |
l = __OINST(slf, left); |
1387 |
w = __OINST(slf, width); |
|
11743 | 1388 |
|
20308 | 1389 |
if (__bothSmallInteger(l, w)) { |
1390 |
t = __OINST(slf, top); |
|
1391 |
h = __OINST(slf, height); |
|
11743 | 1392 |
|
1393 |
#ifndef POSITIVE_ADDRESSES /* tag in low-bit */ |
|
20308 | 1394 |
r_il = (INT)(r_l); |
1395 |
r_ir = r_il + (INT)(r_w) - 1; |
|
1396 |
il = (INT)(l); |
|
1397 |
if (r_ir < il) { RETURN (false); } |
|
1398 |
ir = il + (INT)(w) - 1; |
|
1399 |
if (r_il > ir) { RETURN (false); } |
|
11743 | 1400 |
#else /* tag in hi-bit */ |
20308 | 1401 |
r_il = __intVal(r_l); |
1402 |
r_ir = r_il + __intVal(r_w); /* aRectangle right */ |
|
1403 |
il = __intVal(l); |
|
1404 |
if (r_ir < il) { RETURN (false); } /* (aRectangle right) < left */ |
|
11743 | 1405 |
|
20308 | 1406 |
ir = il + __intVal(w); |
1407 |
if (r_il > ir) { RETURN (false); } /* (aRectangle left) > r */ |
|
11743 | 1408 |
#endif |
637 | 1409 |
|
20308 | 1410 |
if (__bothSmallInteger(r_t, r_h)) { |
1411 |
if (__bothSmallInteger(t, h)) { |
|
11743 | 1412 |
#ifndef POSITIVE_ADDRESSES |
20308 | 1413 |
r_it = (INT)(r_t); |
1414 |
r_ib = r_it + (INT)(r_h) - 1; /* aRectangle bottom */ |
|
1415 |
it = (INT)(t); |
|
1416 |
if (r_ib < it) { RETURN (false); } /* (aRectangle bottom) < top */ |
|
11743 | 1417 |
|
20308 | 1418 |
ib = it + (INT)(h) - 1; |
1419 |
if (r_it > ib) { RETURN (false); } /* (aRectangle top) > b */ |
|
11743 | 1420 |
#else |
20308 | 1421 |
r_it = __intVal(r_t); |
1422 |
r_ib = r_it + __intVal(r_h); /* aRectangle bottom */ |
|
1423 |
it = __intVal(t); |
|
1424 |
if (r_ib < it) { RETURN (false); } /* (aRectangle bottom) < top */ |
|
11743 | 1425 |
|
20308 | 1426 |
ib = it + __intVal(h); |
1427 |
if (r_it > ib) { RETURN (false); } /* (aRectangle top) > b */ |
|
11743 | 1428 |
#endif |
20308 | 1429 |
RETURN (true); |
1430 |
} |
|
1431 |
} |
|
1432 |
} |
|
1433 |
} |
|
11743 | 1434 |
} |
1435 |
%}. |
|
1436 |
(aRectangle right) < left ifTrue:[^ false]. |
|
1437 |
(aRectangle bottom) < top ifTrue:[^ false]. |
|
1438 |
r := left + width. |
|
1439 |
(aRectangle left) > r ifTrue:[^ false]. |
|
1440 |
b := top + height. |
|
1441 |
(aRectangle top) > b ifTrue:[^ false]. |
|
637 | 1442 |
^ true |
11743 | 1443 |
! |
1444 |
||
1445 |
isContainedIn:aRectangle |
|
20308 | 1446 |
"return true, if the receiver is fully contained within |
11743 | 1447 |
the argument, aRectangle" |
1448 |
||
1449 |
(aRectangle left <= left) ifTrue:[ |
|
1450 |
(aRectangle right >= (left + width)) ifTrue:[ |
|
20308 | 1451 |
(aRectangle top <= top) ifTrue:[ |
1452 |
(aRectangle bottom >= (top + height)) ifTrue:[ |
|
1453 |
^ true |
|
1454 |
] |
|
1455 |
] |
|
11743 | 1456 |
] |
1457 |
]. |
|
1458 |
^ false |
|
1459 |
||
1460 |
" |
|
1461 |
|r| |
|
1462 |
||
1463 |
r := Rectangle origin:10@10 corner:100@100. |
|
1464 |
r isContainedIn: (Rectangle origin:10@10 corner:100@100). |
|
1465 |
r isContainedIn: (Rectangle origin:11@10 corner:100@100). |
|
1466 |
r isContainedIn: (Rectangle origin:9@10 corner:100@100). |
|
1467 |
" |
|
186 | 1468 |
! ! |
1469 |
||
1 | 1470 |
!Rectangle methodsFor:'rectangle operations'! |
1471 |
||
1472 |
+ aPoint |
|
356 | 1473 |
"return a new rectangle with same extent as receiver but |
1 | 1474 |
origin translated by the argument, aPoint" |
1475 |
||
217 | 1476 |
|amountPoint| |
1477 |
||
2274 | 1478 |
(aPoint isMemberOf:SmallInteger) ifTrue:[ |
20308 | 1479 |
"/ this is an stc optimization |
1480 |
^ Rectangle |
|
1481 |
left:(left + aPoint) |
|
1482 |
top:(top + aPoint) |
|
1483 |
width:width |
|
1484 |
height:height |
|
2274 | 1485 |
]. |
1486 |
||
217 | 1487 |
amountPoint := aPoint asPoint. |
1488 |
^ Rectangle left:(left + amountPoint x) |
|
20308 | 1489 |
top:(top + amountPoint y) |
1490 |
width:width |
|
1491 |
height:height |
|
2274 | 1492 |
|
1493 |
"Modified: 25.1.1997 / 17:29:53 / cg" |
|
1494 |
! |
|
1495 |
||
1496 |
- aPoint |
|
1497 |
"return a new rectangle with same extent as receiver but |
|
1498 |
origin translated by the argument, aPoint" |
|
1499 |
||
1500 |
|amountPoint| |
|
1501 |
||
1502 |
(aPoint isMemberOf:SmallInteger) ifTrue:[ |
|
20308 | 1503 |
"/ this is an stc optimization |
1504 |
^ Rectangle |
|
1505 |
left:(left - aPoint) |
|
1506 |
top:(top - aPoint) |
|
1507 |
width:width |
|
1508 |
height:height |
|
2274 | 1509 |
]. |
1510 |
||
1511 |
amountPoint := aPoint asPoint. |
|
1512 |
^ Rectangle left:(left - amountPoint x) |
|
20308 | 1513 |
top:(top - amountPoint y) |
1514 |
width:width |
|
1515 |
height:height |
|
2274 | 1516 |
|
1517 |
"Modified: 25.1.1997 / 17:29:53 / cg" |
|
1518 |
"Created: 25.1.1997 / 17:30:21 / cg" |
|
1 | 1519 |
! |
1520 |
||
21269 | 1521 |
areasOutside:aRectangle |
1522 |
"Answer an Array of Rectangles comprising the parts of the receiver |
|
1523 |
not intersecting aRectangle. |
|
1524 |
That is all areas with no common pixels." |
|
1525 |
||
1526 |
|areas| |
|
1527 |
||
1528 |
"Make sure the intersection is non-empty" |
|
1529 |
(self origin <= aRectangle corner and: [aRectangle origin <= self corner]) ifFalse: [ |
|
1530 |
^ Array with: self |
|
1531 |
]. |
|
1532 |
areas := OrderedCollection new. |
|
1533 |
self areasOutside:aRectangle do:[:r | areas add:r]. |
|
1534 |
^ areas asArray |
|
1535 |
||
1536 |
"/ cg: the old code below was wrong ... |
|
4863 | 1537 |
|
21269 | 1538 |
"/ "---------------------------------------------------------------- |
1539 |
"/ | added for GNU-ST compatibility |
|
1540 |
"/ | |
|
1541 |
"/ | author: Doug McCallum <uunet!!ico.isc.com!!dougm> |
|
1542 |
"/ | |
|
1543 |
"/ |areasOutside: aRectangle |
|
1544 |
"/ | most complicated of the Rectangle primitives |
|
1545 |
"/ | The basic methodology is to first determine that there is an |
|
1546 |
"/ | intersection by finding the overlapping rectangle. From the |
|
1547 |
"/ | overlapping rectangle, first determine if it runs along an edge. |
|
1548 |
"/ | If it doesn't, extend the rectangle up to the top edge and add |
|
1549 |
"/ | the new rectangle to the collection and start the rest of the |
|
1550 |
"/ | process. If the left edge does not touch the left edge of self, |
|
1551 |
"/ | extend it to the edge saving the new rectangle. Then do the |
|
1552 |
"/ | same to the right edge. Then check top and bottom edges. Most |
|
1553 |
"/ | of the time only 2 or 3 rectangles get formed, occasionally 4. |
|
1554 |
"/ | It should be possible to never get more than 3 but requires more |
|
1555 |
"/ | work. |
|
1556 |
"/ ----------------------------------------------------------------" |
|
1557 |
"/ |
|
1558 |
"/ | collect iRect tmp | |
|
1559 |
"/ |
|
1560 |
"/ iRect := self intersect: aRectangle. |
|
1561 |
"/ iRect isNil ifTrue: [^nil]. "case of no intersection" |
|
1562 |
"/ "the collect collection gathers Rectangles" |
|
1563 |
"/ collect := OrderedCollection new: 4. |
|
1564 |
"/ "is it floating or on the edge?" |
|
1565 |
"/ (((((iRect top) ~= self top) |
|
1566 |
"/ and: [ (iRect bottom) ~= self bottom ]) |
|
1567 |
"/ and: [ (iRect left) ~= self left ]) |
|
1568 |
"/ and: [ (iRect right) ~= self right ] ) |
|
1569 |
"/ ifTrue: "entirely in the center." |
|
1570 |
"/ [tmp := Rectangle origin: (Point x: iRect left y: self top) |
|
1571 |
"/ corner: iRect bottomRight. |
|
1572 |
"/ collect add: tmp. |
|
1573 |
"/ iRect := iRect merge: tmp]. |
|
1574 |
"/ ((iRect left) ~= self left) |
|
1575 |
"/ ifTrue: "doesn't touch left edge so make it touch" |
|
1576 |
"/ [tmp := Rectangle origin: (Point x: self left y: iRect top) |
|
1577 |
"/ corner: iRect bottomLeft. |
|
1578 |
"/ collect add: tmp. |
|
1579 |
"/ "merge new (tmp) with overlap to keep track" |
|
1580 |
"/ iRect := iRect merge: tmp]. |
|
1581 |
"/ ((iRect right) ~= self right) |
|
1582 |
"/ ifTrue: "doesn't touch right edge so extend it" |
|
1583 |
"/ [tmp := Rectangle origin: iRect topRight |
|
1584 |
"/ corner: (Point x: self right y: iRect bottom). |
|
1585 |
"/ collect add: tmp. |
|
1586 |
"/ iRect := iRect merge: tmp]. |
|
1587 |
"/ (((iRect left) ~= self left) or: [(iRect top) ~= self top]) |
|
1588 |
"/ ifTrue: "whole top part can be taken now" |
|
1589 |
"/ [tmp := Rectangle origin: self origin corner: iRect topRight. |
|
1590 |
"/ collect add: tmp]. |
|
1591 |
"/ (((iRect right) ~= self right) or: [(iRect bottom) ~= self bottom]) |
|
1592 |
"/ ifTrue: "whole bottom open and can be taken" |
|
1593 |
"/ [tmp := Rectangle origin: iRect bottomLeft corner: self corner. |
|
1594 |
"/ collect add: tmp]. |
|
1595 |
"/ ^collect |
|
1596 |
! |
|
1597 |
||
1598 |
areasOutside:aRectangle do:aBlock |
|
1599 |
"evaluate aBlock for Rectangles comprising the parts of the receiver |
|
1600 |
not intersecting aRectangle. |
|
1601 |
That is all areas with no common pixels." |
|
1602 |
||
1603 |
|yOrigin yCorner origin corner| |
|
4863 | 1604 |
|
1605 |
origin := self origin. |
|
1606 |
corner := self corner. |
|
1 | 1607 |
|
4863 | 1608 |
"Make sure the intersection is non-empty" |
1609 |
(origin <= aRectangle corner and: [aRectangle origin <= corner]) |
|
21269 | 1610 |
ifFalse: [ aBlock value:self. ^ self]. |
4863 | 1611 |
|
21269 | 1612 |
aRectangle origin y > origin y ifTrue: [ |
1613 |
aBlock value: (origin corner: corner x @ (yOrigin := aRectangle origin y)) |
|
1614 |
] ifFalse: [ |
|
1615 |
yOrigin := origin y |
|
1616 |
]. |
|
1617 |
aRectangle corner y < corner y ifTrue: [ |
|
1618 |
aBlock value: (origin x @ (yCorner := aRectangle corner y) corner: corner) |
|
1619 |
] ifFalse: [ |
|
1620 |
yCorner := corner y |
|
1621 |
]. |
|
1622 |
aRectangle origin x > origin x ifTrue: [ |
|
1623 |
aBlock value: (origin x @ yOrigin corner: aRectangle origin x @ yCorner) |
|
1624 |
]. |
|
1625 |
aRectangle corner x < corner x ifTrue: [ |
|
1626 |
aBlock value: (aRectangle corner x @ yOrigin corner: corner x @ yCorner) |
|
1627 |
]. |
|
1628 |
||
1629 |
" |
|
1630 |
self assert: |
|
1631 |
((0@0 corner:100@100) areasOutside:(0@0 corner:50@50)) asArray |
|
1632 |
= { (Rectangle origin:0@50 extent:100@50) . (Rectangle origin:50@0 extent:50@50) } |
|
1633 |
||
1634 |
self assert: |
|
1635 |
((0@0 corner:150@100) areasOutside:(0@0 corner:100@100)) asArray |
|
1636 |
= { (Rectangle origin:100@0 extent:50@100) } |
|
1637 |
||
1638 |
self assert: |
|
1639 |
((0@0 corner:100@150) areasOutside:(0@0 corner:100@100)) asArray |
|
1640 |
= { (Rectangle origin:0@100 extent:100@50) } |
|
1641 |
" |
|
1642 |
||
4863 | 1643 |
"/ cg: the old code below was wrong ... |
1124 | 1644 |
|
4863 | 1645 |
"/ "---------------------------------------------------------------- |
1646 |
"/ | added for GNU-ST compatibility |
|
1647 |
"/ | |
|
1648 |
"/ | author: Doug McCallum <uunet!!ico.isc.com!!dougm> |
|
1649 |
"/ | |
|
1650 |
"/ |areasOutside: aRectangle |
|
1651 |
"/ | most complicated of the Rectangle primitives |
|
20308 | 1652 |
"/ | The basic methodology is to first determine that there is an |
4863 | 1653 |
"/ | intersection by finding the overlapping rectangle. From the |
1654 |
"/ | overlapping rectangle, first determine if it runs along an edge. |
|
1655 |
"/ | If it doesn't, extend the rectangle up to the top edge and add |
|
1656 |
"/ | the new rectangle to the collection and start the rest of the |
|
1657 |
"/ | process. If the left edge does not touch the left edge of self, |
|
20308 | 1658 |
"/ | extend it to the edge saving the new rectangle. Then do the |
4863 | 1659 |
"/ | same to the right edge. Then check top and bottom edges. Most |
1660 |
"/ | of the time only 2 or 3 rectangles get formed, occasionally 4. |
|
1661 |
"/ | It should be possible to never get more than 3 but requires more |
|
1662 |
"/ | work. |
|
1663 |
"/ ----------------------------------------------------------------" |
|
1664 |
"/ |
|
1665 |
"/ | collect iRect tmp | |
|
1666 |
"/ |
|
1667 |
"/ iRect := self intersect: aRectangle. |
|
1668 |
"/ iRect isNil ifTrue: [^nil]. "case of no intersection" |
|
1669 |
"/ "the collect collection gathers Rectangles" |
|
1670 |
"/ collect := OrderedCollection new: 4. |
|
1671 |
"/ "is it floating or on the edge?" |
|
20308 | 1672 |
"/ (((((iRect top) ~= self top) |
4863 | 1673 |
"/ and: [ (iRect bottom) ~= self bottom ]) |
1674 |
"/ and: [ (iRect left) ~= self left ]) |
|
1675 |
"/ and: [ (iRect right) ~= self right ] ) |
|
1676 |
"/ ifTrue: "entirely in the center." |
|
1677 |
"/ [tmp := Rectangle origin: (Point x: iRect left y: self top) |
|
1678 |
"/ corner: iRect bottomRight. |
|
1679 |
"/ collect add: tmp. |
|
1680 |
"/ iRect := iRect merge: tmp]. |
|
1681 |
"/ ((iRect left) ~= self left) |
|
1682 |
"/ ifTrue: "doesn't touch left edge so make it touch" |
|
1683 |
"/ [tmp := Rectangle origin: (Point x: self left y: iRect top) |
|
1684 |
"/ corner: iRect bottomLeft. |
|
1685 |
"/ collect add: tmp. |
|
1686 |
"/ "merge new (tmp) with overlap to keep track" |
|
1687 |
"/ iRect := iRect merge: tmp]. |
|
1688 |
"/ ((iRect right) ~= self right) |
|
1689 |
"/ ifTrue: "doesn't touch right edge so extend it" |
|
1690 |
"/ [tmp := Rectangle origin: iRect topRight |
|
1691 |
"/ corner: (Point x: self right y: iRect bottom). |
|
1692 |
"/ collect add: tmp. |
|
1693 |
"/ iRect := iRect merge: tmp]. |
|
1694 |
"/ (((iRect left) ~= self left) or: [(iRect top) ~= self top]) |
|
1695 |
"/ ifTrue: "whole top part can be taken now" |
|
1696 |
"/ [tmp := Rectangle origin: self origin corner: iRect topRight. |
|
1697 |
"/ collect add: tmp]. |
|
1698 |
"/ (((iRect right) ~= self right) or: [(iRect bottom) ~= self bottom]) |
|
1699 |
"/ ifTrue: "whole bottom open and can be taken" |
|
1700 |
"/ [tmp := Rectangle origin: iRect bottomLeft corner: self corner. |
|
1701 |
"/ collect add: tmp]. |
|
1702 |
"/ ^collect |
|
77 | 1703 |
! |
1 | 1704 |
|
20308 | 1705 |
encompass:aPoint |
4864 | 1706 |
"return a Rectangle that contains both the receiver and aPoint." |
1707 |
||
20308 | 1708 |
^ Rectangle |
1709 |
origin: (self origin min: aPoint) |
|
1710 |
corner: (self corner max: aPoint) |
|
4864 | 1711 |
|
1712 |
! |
|
1713 |
||
637 | 1714 |
expandedBy:delta |
1715 |
"return a new rectangle which is expanded in all directions |
|
1716 |
by amount, a Point, Rectangle or Number" |
|
1717 |
||
1718 |
^ self copy expandBy:delta |
|
1719 |
||
1720 |
" |
|
1721 |
|r| |
|
1722 |
r := Rectangle origin:10@10 corner:100@100. |
|
20308 | 1723 |
r expandedBy:5. |
1724 |
r expandedBy:(5 @ 0). |
|
1725 |
r expandedBy:(10 @ 10). |
|
1726 |
r expandedBy:( 10@10 corner:20@20 ) |
|
637 | 1727 |
" |
1728 |
! |
|
1729 |
||
1730 |
insetBy: delta |
|
1731 |
"return a new rectangle which is inset in all directions |
|
1732 |
by delta, a Point, Rectangle or Number" |
|
1733 |
||
1734 |
|amountPoint deltaLeft deltaTop deltaWidth deltaHeight| |
|
1735 |
||
1736 |
delta isNumber ifTrue:[ |
|
1737 |
deltaLeft := deltaTop := delta. |
|
1738 |
deltaWidth := deltaHeight := delta * 2. |
|
1739 |
] ifFalse:[ |
|
1740 |
delta isRectangle ifTrue:[ |
|
1741 |
deltaLeft := delta left. |
|
1742 |
deltaTop := delta top. |
|
1743 |
deltaWidth := deltaLeft + delta right. |
|
1744 |
deltaHeight := deltaTop + delta bottom |
|
1745 |
] ifFalse:[ |
|
1746 |
amountPoint := delta asPoint. |
|
1747 |
deltaLeft := amountPoint x. |
|
1748 |
deltaTop := amountPoint y. |
|
1749 |
deltaWidth := deltaLeft * 2. |
|
1750 |
deltaHeight := deltaTop * 2. |
|
1751 |
] |
|
1752 |
]. |
|
1753 |
||
1754 |
^ Rectangle left:(left + deltaLeft) |
|
1755 |
top:(top + deltaTop) |
|
1756 |
width:(width - deltaWidth) |
|
1757 |
height:(height - deltaHeight) |
|
1758 |
" |
|
1759 |
|r| |
|
1760 |
r := Rectangle origin:10@10 corner:100@100. |
|
1761 |
r insetBy:5. |
|
1762 |
r insetBy:(5 @ 0). |
|
1763 |
r insetBy:(10 @ 10). |
|
1764 |
r insetBy:( 10@10 corner:20@20 ) |
|
1765 |
" |
|
1766 |
! |
|
1767 |
||
1768 |
insetOriginBy:originDelta cornerBy:cornerDelta |
|
20308 | 1769 |
"return a new rectangle which is inset by originDelta |
637 | 1770 |
and cornerDelta; both may be instances of Point or Number" |
1771 |
||
1772 |
^ Rectangle |
|
1773 |
origin:(left @ top) + originDelta asPoint |
|
1774 |
corner:(self corner - cornerDelta asPoint) |
|
1775 |
" |
|
1776 |
|r| |
|
1777 |
r := Rectangle origin:10@10 corner:100@100. |
|
20308 | 1778 |
r insetOriginBy:5 cornerBy:10. |
637 | 1779 |
r insetOriginBy:10@5 cornerBy:10. |
20308 | 1780 |
r insetOriginBy:10 cornerBy:10@5. |
637 | 1781 |
r insetOriginBy:10@10 cornerBy:20@20. |
1782 |
" |
|
1783 |
! |
|
1784 |
||
1785 |
intersect:aRectangle |
|
1786 |
"return a new rectangle covering the intersection of the receiver |
|
5016 | 1787 |
and the argument, aRectangle (i.e. the area covered by both). |
637 | 1788 |
the rectangles must intersect for a valid return" |
1789 |
||
1790 |
^ Rectangle left:(left max:(aRectangle left)) |
|
20308 | 1791 |
right:((left + width) min:(aRectangle right)) |
1792 |
top:(top max:(aRectangle top)) |
|
1793 |
bottom:((top + height) min:(aRectangle bottom)) |
|
637 | 1794 |
|
1795 |
" |
|
1796 |
|r1 r2| |
|
1797 |
||
1798 |
r1 := Rectangle origin:10@10 corner:100@100. |
|
1799 |
r2 := Rectangle origin:50@50 corner:150@75. |
|
1800 |
r1 intersect:r2 |
|
1801 |
" |
|
1802 |
! |
|
1803 |
||
1804 |
merge:aRectangle |
|
20308 | 1805 |
"return a new rectangle covering both the receiver |
637 | 1806 |
and the argument, aRectangle" |
1807 |
||
1808 |
^ Rectangle left:(left min:(aRectangle left)) |
|
20308 | 1809 |
right:((left + width) max:(aRectangle right)) |
1810 |
top:(top min:(aRectangle top)) |
|
1811 |
bottom:((top + height) max:(aRectangle bottom)) |
|
4859 | 1812 |
|
1813 |
" |
|
1814 |
(Rectangle origin:10@10 corner:100@100) |
|
20308 | 1815 |
merge:(Rectangle origin:20@20 corner:110@110) |
4859 | 1816 |
|
1817 |
(Rectangle origin:10@10 corner:100@100) |
|
20308 | 1818 |
merge:(Rectangle origin:20@20 corner:100@100) |
4859 | 1819 |
" |
637 | 1820 |
! |
1821 |
||
77 | 1822 |
nonIntersections:aRectangle |
1823 |
"this is the same as areasOutside: - for ST/V compatibility only" |
|
1824 |
||
1825 |
^ self areasOutside:aRectangle |
|
637 | 1826 |
! |
1827 |
||
20308 | 1828 |
quickMerge: aRectangle |
4859 | 1829 |
"return the receiver if it encloses the given rectangle, |
20308 | 1830 |
or the merge of the two rectangles if it doesn't. |
8884 | 1831 |
This method is an optimized version of merge: to reduce extra rectangle creations." |
4859 | 1832 |
|
4869
45bae3b31eef
Complete previous fix. (Stefan)
Claus Gittinger <cg@exept.de>
parents:
4868
diff
changeset
|
1833 |
| useRcvr rLeft rTop rRight rBottom minX maxX minY maxY | |
4859 | 1834 |
|
1835 |
useRcvr := true. |
|
8884 | 1836 |
|
4859 | 1837 |
rLeft := aRectangle left. |
1838 |
rTop := aRectangle top. |
|
1839 |
rRight := aRectangle right. |
|
1840 |
rBottom := aRectangle bottom. |
|
8001 | 1841 |
|
1842 |
minX := left. |
|
1843 |
rLeft < minX ifTrue: [useRcvr := false. minX := rLeft]. |
|
1844 |
maxX := self right. |
|
1845 |
rRight > maxX ifTrue: [useRcvr := false. maxX := rRight]. |
|
1846 |
minY := top. |
|
1847 |
rTop < minY ifTrue: [useRcvr := false. minY := rTop]. |
|
1848 |
maxY := self bottom. |
|
1849 |
rBottom > maxY ifTrue: [useRcvr := false. maxY := rBottom]. |
|
4859 | 1850 |
|
8884 | 1851 |
useRcvr ifTrue: [ |
20308 | 1852 |
^ self |
8884 | 1853 |
]. |
1854 |
||
1855 |
minX = rLeft ifTrue:[ |
|
20308 | 1856 |
maxX = rRight ifTrue:[ |
1857 |
minY = rTop ifTrue:[ |
|
1858 |
maxY = rBottom ifTrue:[ |
|
1859 |
^ aRectangle |
|
1860 |
]. |
|
1861 |
]. |
|
1862 |
]. |
|
8884 | 1863 |
]. |
8001 | 1864 |
|
4859 | 1865 |
^ Rectangle left:minX top:minY right:maxX bottom:maxY. |
1866 |
||
1867 |
" |
|
20308 | 1868 |
(Rectangle origin:10@10 corner:100@100) |
1869 |
quickMerge:(Rectangle origin:20@20 corner:110@110) |
|
4859 | 1870 |
|
1871 |
(Rectangle origin:10@10 corner:100@100) |
|
20308 | 1872 |
quickMerge:(Rectangle origin:20@20 corner:100@100) |
4859 | 1873 |
" |
1874 |
! |
|
1875 |
||
637 | 1876 |
scaledBy:scale |
1877 |
"return a new rectangle which is the receiver |
|
1878 |
scaled by scale" |
|
1879 |
||
1880 |
|scalePoint sx sy| |
|
356 | 1881 |
|
18820 | 1882 |
(scale isMemberOf:SmallInteger) ifTrue:[ |
20308 | 1883 |
"/ this is an stc optimization |
1884 |
^ Rectangle left:left * scale |
|
1885 |
top:top * scale |
|
1886 |
width:width * scale |
|
1887 |
height:height * scale |
|
18820 | 1888 |
]. |
1889 |
||
637 | 1890 |
scalePoint := scale asPoint. |
1891 |
sx := scalePoint x. |
|
1892 |
sy := scalePoint y. |
|
1893 |
^ Rectangle left:left * sx |
|
20308 | 1894 |
top:top * sy |
1895 |
width:width * sx |
|
1896 |
height:height * sy |
|
356 | 1897 |
" |
20308 | 1898 |
(Rectangle origin:10@10 corner:50@50) scaledBy:2 |
637 | 1899 |
" |
356 | 1900 |
|
20018 | 1901 |
"it is NOT destructive:" |
637 | 1902 |
" |
1903 |
|r1 r2| |
|
356 | 1904 |
|
637 | 1905 |
r1 := Rectangle origin:10@10 corner:50@50. |
20308 | 1906 |
r2 := r1 scaledBy:2. |
1907 |
r1 |
|
356 | 1908 |
" |
1909 |
! |
|
1910 |
||
20308 | 1911 |
scaledBy:scale translatedBy:translation |
18892 | 1912 |
"return a new rectangle which is translated (i.e. moved) |
1913 |
by translation, aPoint or Number and scaled by scale, aPoint or number." |
|
1914 |
||
1915 |
|x y w h translationPoint scalePoint sx sy| |
|
1916 |
||
1917 |
(translation isNil and:[scale isNil]) ifTrue:[ |
|
20308 | 1918 |
^ self. |
18892 | 1919 |
]. |
1920 |
||
1921 |
x := left. |
|
1922 |
y := top. |
|
1923 |
w := width. |
|
1924 |
h := height. |
|
1925 |
||
1926 |
scale notNil ifTrue:[ |
|
20308 | 1927 |
(scale isMemberOf:SmallInteger) ifTrue:[ |
1928 |
"/ this is an stc optimization |
|
1929 |
x := x * scale. |
|
1930 |
y := y * scale. |
|
1931 |
w := w * scale. |
|
1932 |
h := h * scale. |
|
1933 |
] ifFalse:[ |
|
1934 |
scalePoint := scale asPoint. |
|
1935 |
sx := scalePoint x. |
|
1936 |
sy := scalePoint y. |
|
1937 |
x := x * sx. |
|
1938 |
y := y * sy. |
|
1939 |
w := w * sx. |
|
1940 |
h := h * sy. |
|
1941 |
]. |
|
18892 | 1942 |
]. |
1943 |
translation notNil ifTrue:[ |
|
20308 | 1944 |
(translation isMemberOf:SmallInteger) ifTrue:[ |
1945 |
"/ this is an stc optimization |
|
1946 |
x := x + translation. |
|
1947 |
y := y + translation. |
|
1948 |
] ifFalse:[ |
|
1949 |
translationPoint := translation asPoint. |
|
1950 |
x := x + translationPoint x. |
|
1951 |
y := y + translationPoint y. |
|
1952 |
]. |
|
18892 | 1953 |
]. |
1954 |
||
1955 |
^ Rectangle left:x top:y width:w height:h. |
|
1956 |
||
1957 |
" |
|
20308 | 1958 |
(Rectangle origin:10@10 corner:50@50) scaledBy:2 translatedBy:10 |
18892 | 1959 |
" |
1960 |
! |
|
1961 |
||
637 | 1962 |
translatedBy:amount |
1963 |
"return a new rectangle which is translated (i.e. moved) |
|
1964 |
by amount, aPoint or Number" |
|
1965 |
||
1966 |
|amountPoint| |
|
356 | 1967 |
|
18892 | 1968 |
(amount isMemberOf:SmallInteger) ifTrue:[ |
20308 | 1969 |
"/ this is an stc optimization |
1970 |
^ Rectangle |
|
1971 |
left:(left + amount) |
|
1972 |
top:(top + amount) |
|
1973 |
width:width |
|
1974 |
height:height |
|
18892 | 1975 |
]. |
1976 |
||
637 | 1977 |
amountPoint := amount asPoint. |
20308 | 1978 |
^ Rectangle left:(left + amountPoint x) |
1979 |
top:(top + amountPoint y) |
|
1980 |
width:width |
|
1981 |
height:height |
|
356 | 1982 |
" |
637 | 1983 |
(Rectangle origin:10@10 corner:50@50) translatedBy:10 |
1984 |
" |
|
356 | 1985 |
|
637 | 1986 |
"its NOT destructive:" |
1987 |
" |
|
1988 |
|r1 r2| |
|
356 | 1989 |
|
637 | 1990 |
r1 := Rectangle origin:10@10 corner:50@50. |
1991 |
r2 := r1 translatedBy:10. |
|
20308 | 1992 |
r1 |
356 | 1993 |
" |
1994 |
! ! |
|
1995 |
||
637 | 1996 |
!Rectangle methodsFor:'testing'! |
1 | 1997 |
|
11743 | 1998 |
canBeFilled |
1999 |
"return true, if the receiver can be drawn as a filled geometric. |
|
2000 |
Always true here." |
|
1149
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
2001 |
|
11743 | 2002 |
^ true |
1149
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
2003 |
|
11743 | 2004 |
"Created: 8.5.1996 / 08:16:47 / cg" |
1 | 2005 |
! |
2006 |
||
11743 | 2007 |
isRectangle |
2008 |
"return true, if the receiver is some kind of rectangle" |
|
1149
69b30e3494fa
added specially tuned code for #intersects: & #contains: (for objectView performance)
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
2009 |
|
637 | 2010 |
^ true |
1 | 2011 |
! ! |
637 | 2012 |
|
15730 | 2013 |
!Rectangle methodsFor:'transformations'! |
2014 |
||
2015 |
align:offset with:someCoordinate |
|
2016 |
"return a new rectangle which is translated (i.e. moved) |
|
2017 |
such that the point offset in mySelf is placed on someCoordinate." |
|
2018 |
||
2019 |
^ Rectangle origin:(someCoordinate - offset + self origin) |
|
2020 |
extent:(self extent) |
|
2021 |
" |
|
2022 |
|r| |
|
2023 |
||
2024 |
r := Rectangle origin:10@10 corner:50@50. |
|
2025 |
r align:(r center) with:100@100. |
|
2026 |
" |
|
2027 |
! ! |
|
2028 |
||
5552 | 2029 |
!Rectangle methodsFor:'truncation & rounding'! |
4861 | 2030 |
|
2031 |
rounded |
|
2032 |
"return a copy of the receiver with rounded coordinates. |
|
2033 |
Return the receiver if its coordinates are already integral." |
|
2034 |
||
20308 | 2035 |
(left isInteger |
2036 |
and:[top isInteger |
|
2037 |
and:[width isInteger |
|
4861 | 2038 |
and:[height isInteger]]]) |
20308 | 2039 |
ifTrue: [^ self]. |
4861 | 2040 |
|
20308 | 2041 |
^ Rectangle left:(left rounded) |
2042 |
top:(top rounded) |
|
2043 |
width:(width rounded) |
|
2044 |
height:(height rounded) |
|
4861 | 2045 |
! |
2046 |
||
2047 |
truncated |
|
2048 |
"return a Rectangle whose origin and corner have any fractional parts removed. |
|
2049 |
Return the receiver if its coordinates are already integral." |
|
2050 |
||
20308 | 2051 |
(left isInteger |
2052 |
and:[top isInteger |
|
2053 |
and:[width isInteger |
|
4861 | 2054 |
and:[height isInteger]]]) |
20308 | 2055 |
ifTrue: [^ self]. |
4861 | 2056 |
|
20308 | 2057 |
^ Rectangle |
2058 |
left:left truncated |
|
2059 |
top:top truncated |
|
2060 |
width:width truncated |
|
2061 |
height:height truncated. |
|
4861 | 2062 |
! ! |
2063 |
||
637 | 2064 |
!Rectangle class methodsFor:'documentation'! |
2065 |
||
2066 |
version |
|
18820 | 2067 |
^ '$Header$' |
12946 | 2068 |
! |
2069 |
||
2070 |
version_CVS |
|
18820 | 2071 |
^ '$Header$' |
637 | 2072 |
! ! |
15730 | 2073 |