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