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