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