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