author | Claus Gittinger <cg@exept.de> |
Thu, 09 Jul 1998 10:21:23 +0200 | |
changeset 3636 | cba3cd259b32 |
parent 3562 | 813c3f1e99d7 |
child 4857 | c29c6910114b |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
180 | 3 |
All Rights Reserved |
1 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
13 |
ArithmeticValue subclass:#Point |
|
1245 | 14 |
instanceVariableNames:'x y' |
15 |
classVariableNames:'PointZero PointOne' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Graphics-Geometry' |
|
1 | 18 |
! |
19 |
||
2273 | 20 |
!Point class methodsFor:'documentation'! |
77 | 21 |
|
88 | 22 |
copyright |
23 |
" |
|
24 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
180 | 25 |
All Rights Reserved |
88 | 26 |
|
27 |
This software is furnished under a license and may be used |
|
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
35 |
||
77 | 36 |
documentation |
37 |
" |
|
38 |
I represent a point in 2D space. Or I can be used to represent |
|
39 |
an extent (of a rectangle, for example), in which case my x-coordinate |
|
40 |
represents the width, and y-coordinate the height of something. |
|
41 |
||
42 |
The x and y coordinate are usually numbers. |
|
43 |
||
1290 | 44 |
[Instance variables:] |
77 | 45 |
|
1290 | 46 |
x <Number> the x-coordinate of myself |
47 |
y <Number> the y-coordinate of myself |
|
48 |
||
49 |
[author:] |
|
50 |
Claus Gittinger |
|
1314 | 51 |
|
52 |
[see also:] |
|
53 |
Rectangle Polygon |
|
54 |
LayoutOrigin LayoutFrame AlignmentOrigin Layout |
|
55 |
View GraphicsContext |
|
77 | 56 |
" |
57 |
! ! |
|
58 |
||
2273 | 59 |
!Point class methodsFor:'initialization'! |
1 | 60 |
|
61 |
initialize |
|
302 | 62 |
PointZero isNil ifTrue:[ |
63 |
PointZero := 0 @ 0. |
|
64 |
PointOne := 1 @ 1 |
|
65 |
] |
|
1 | 66 |
! ! |
67 |
||
2273 | 68 |
!Point class methodsFor:'instance creation'! |
1 | 69 |
|
3238
f96eaf89d55d
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
70 |
decodeFromLiteralArray:anArray |
f96eaf89d55d
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
71 |
"create & return a new instance from information encoded in anArray. |
f96eaf89d55d
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
72 |
Redefined for faster creation." |
f96eaf89d55d
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
73 |
|
f96eaf89d55d
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
74 |
^ self x:(anArray at:2) y:(anArray at:3) |
f96eaf89d55d
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
75 |
|
f96eaf89d55d
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
76 |
" |
f96eaf89d55d
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
77 |
Point |
f96eaf89d55d
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
78 |
decodeFromLiteralArray:#(Point 10 10) |
f96eaf89d55d
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
79 |
" |
f96eaf89d55d
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
80 |
|
f96eaf89d55d
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
81 |
"Created: / 28.1.1998 / 17:44:08 / cg" |
f96eaf89d55d
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
82 |
! |
f96eaf89d55d
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
83 |
|
1354 | 84 |
r:distance angle:angle |
85 |
"create and return a new point given polar coordinates. |
|
86 |
The angle is given in degrees" |
|
87 |
||
88 |
|x y rad| |
|
89 |
||
90 |
rad := angle degreesToRadians. |
|
91 |
x := distance * rad cos. |
|
92 |
y := distance * rad sin. |
|
93 |
^ x @ y |
|
94 |
||
95 |
" |
|
96 |
Point r:100 angle:0 |
|
97 |
Point r:100 angle:90 |
|
98 |
Point r:100 angle:45 |
|
99 |
Point r:100 angle:180 |
|
100 |
" |
|
101 |
||
102 |
"Modified: 8.5.1996 / 20:01:50 / cg" |
|
103 |
! |
|
104 |
||
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
105 |
r:distance theta:angleInRadians |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
106 |
"create and return a new point given polar coordinates. |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
107 |
The angle is given in radians" |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
108 |
|
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
109 |
|x y| |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
110 |
|
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
111 |
x := distance * angleInRadians cos. |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
112 |
y := distance * angleInRadians sin. |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
113 |
^ x @ y |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
114 |
|
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
115 |
" |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
116 |
Point r:100 theta:0 |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
117 |
" |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
118 |
|
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
119 |
"Modified: 2.4.1997 / 00:01:40 / cg" |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
120 |
! |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
121 |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
122 |
readFrom:aStringOrStream onError:exceptionBlock |
1 | 123 |
"return the next Point from the (character-)stream aStream; |
202 | 124 |
skipping all whitespace first; return the value of exceptionBlock, |
125 |
if no point can be read." |
|
1 | 126 |
|
1701
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1354
diff
changeset
|
127 |
ErrorSignal handle:[:ex | |
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1354
diff
changeset
|
128 |
^ exceptionBlock value |
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1354
diff
changeset
|
129 |
] do:[ |
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1354
diff
changeset
|
130 |
|str newX newY| |
1 | 131 |
|
1701
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1354
diff
changeset
|
132 |
str := aStringOrStream readStream. |
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
133 |
|
1701
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1354
diff
changeset
|
134 |
newX := Number readFrom:str onError:nil. |
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1354
diff
changeset
|
135 |
newX notNil ifTrue:[ |
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1354
diff
changeset
|
136 |
(str skipSeparators == $@) ifTrue:[ |
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1354
diff
changeset
|
137 |
str next. |
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1354
diff
changeset
|
138 |
newY := Number readFrom:str onError:nil. |
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1354
diff
changeset
|
139 |
newY notNil ifTrue:[ |
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1354
diff
changeset
|
140 |
^ self x:newX y:newY |
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1354
diff
changeset
|
141 |
] |
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1354
diff
changeset
|
142 |
] |
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1354
diff
changeset
|
143 |
]. |
345 | 144 |
]. |
145 |
^ exceptionBlock value |
|
77 | 146 |
|
202 | 147 |
" |
148 |
Point readFrom:('1.234 @ 5.678' readStream) |
|
345 | 149 |
Point readFrom:('1' readStream) |
150 |
Point readFrom:('1' readStream) onError:[1@1] |
|
202 | 151 |
Point readFrom:('fooBar' readStream) onError:[0@0] |
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
152 |
" |
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
153 |
|
1701
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1354
diff
changeset
|
154 |
"Modified: 8.10.1996 / 19:31:39 / cg" |
637 | 155 |
! |
156 |
||
157 |
x:newX y:newY |
|
158 |
"create and return a new point with coordinates newX and newY" |
|
159 |
||
160 |
%{ /* NOCONTEXT */ |
|
161 |
||
162 |
/* |
|
163 |
* claus: I am no longer certain, if this primitive is worth the effort |
|
164 |
*/ |
|
2894 | 165 |
if (__CanDoQuickNew(sizeof(struct __point))) { /* OBJECT ALLOCATION */ |
1133 | 166 |
if (self == @global(Point)) { |
637 | 167 |
OBJ newPoint; |
168 |
int spc; |
|
169 |
||
835 | 170 |
__qCheckedAlignedNew(newPoint, sizeof(struct __point)); |
1133 | 171 |
__InstPtr(newPoint)->o_class = self; |
172 |
__PointInstPtr(newPoint)->p_x = newX; |
|
173 |
__PointInstPtr(newPoint)->p_y = newY; |
|
637 | 174 |
if (! __bothSmallInteger(newX, newY)) { |
175 |
spc = __qSpace(newPoint); |
|
176 |
__STORE_SPC(newPoint, newX, spc); |
|
177 |
__STORE_SPC(newPoint, newY, spc); |
|
178 |
} |
|
179 |
RETURN ( newPoint ); |
|
180 |
} |
|
181 |
} |
|
1133 | 182 |
%}. |
637 | 183 |
^ (self basicNew) x:newX y:newY |
1 | 184 |
! ! |
185 |
||
2273 | 186 |
!Point class methodsFor:'constants'! |
356 | 187 |
|
637 | 188 |
unity |
189 |
"return the neutral element for multiplication" |
|
190 |
||
191 |
^ PointOne |
|
192 |
! |
|
193 |
||
356 | 194 |
zero |
195 |
"return the neutral element for addition" |
|
196 |
||
197 |
^ PointZero |
|
198 |
! ! |
|
199 |
||
2273 | 200 |
!Point class methodsFor:'queries'! |
356 | 201 |
|
202 |
isBuiltInClass |
|
1264 | 203 |
"return true if this class is known by the run-time-system. |
204 |
Here, true is returned for myself, false for subclasses." |
|
356 | 205 |
|
206 |
^ self == Point |
|
1264 | 207 |
|
208 |
"Modified: 23.4.1996 / 16:00:18 / cg" |
|
356 | 209 |
! ! |
210 |
||
637 | 211 |
!Point methodsFor:'accessing'! |
212 |
||
213 |
x |
|
214 |
"return the x coordinate" |
|
215 |
||
216 |
^ x |
|
217 |
! |
|
356 | 218 |
|
637 | 219 |
x:newX |
220 |
"set the x coordinate to be the argument, aNumber. |
|
221 |
This is destructive (modifies the receiver, not a copy) and |
|
222 |
should only be used if you know, that you are the exclusive owner |
|
223 |
of the receiver." |
|
356 | 224 |
|
637 | 225 |
x := newX |
356 | 226 |
! |
227 |
||
637 | 228 |
x:newX y:newY |
229 |
"set both the x and y coordinates. |
|
230 |
This is destructive (modifies the receiver, not a copy) and |
|
231 |
should only be used if you know, that you are the exclusive owner |
|
232 |
of the receiver." |
|
1 | 233 |
|
637 | 234 |
x := newX. |
235 |
y := newY |
|
236 |
! |
|
237 |
||
238 |
y |
|
239 |
"return the y coordinate" |
|
240 |
||
241 |
^ y |
|
356 | 242 |
! |
243 |
||
637 | 244 |
y:newY |
245 |
"set the y coordinate to be the argument, aNumber. |
|
246 |
This is destructive (modifies the receiver, not a copy) and |
|
247 |
should only be used if you know, that you are the exclusive owner |
|
248 |
of the receiver." |
|
356 | 249 |
|
637 | 250 |
y := newY |
251 |
! ! |
|
252 |
||
253 |
!Point methodsFor:'coercing'! |
|
254 |
||
255 |
coerce:anObject |
|
256 |
"return aNumber converted into receivers type" |
|
257 |
||
258 |
^ anObject asPoint |
|
356 | 259 |
! |
1 | 260 |
|
637 | 261 |
generality |
262 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
263 |
||
264 |
^ 120 |
|
265 |
! ! |
|
356 | 266 |
|
637 | 267 |
!Point methodsFor:'comparing'! |
268 |
||
269 |
< aPoint |
|
270 |
"return true if the receiver is above and to the left |
|
271 |
of the argument, aPoint" |
|
272 |
||
273 |
|p| |
|
356 | 274 |
|
637 | 275 |
p := aPoint asPoint. |
1339 | 276 |
x < (p x) ifTrue:[ |
277 |
y < (p y) ifTrue:[^ true]. |
|
278 |
]. |
|
279 |
^ false |
|
280 |
||
281 |
"notice the funny result if one coordinate has the same value ... |
|
282 |
||
283 |
(3@4) < (4@4) |
|
284 |
(3@4) <= (4@4) |
|
285 |
(3@4) > (4@4) |
|
286 |
(4@4) >= (3@4) |
|
287 |
(4@4) > (3@4) |
|
288 |
" |
|
289 |
||
290 |
"Modified: 7.5.1996 / 12:14:25 / cg" |
|
637 | 291 |
! |
292 |
||
293 |
= aPoint |
|
294 |
"return true if the receiver represents the same point as |
|
295 |
the argument, aPoint" |
|
356 | 296 |
|
637 | 297 |
|p| |
298 |
||
299 |
(aPoint isMemberOf:Point) ifTrue:[ "this is a hint to STC" |
|
300 |
x ~= (aPoint x) ifTrue:[^ false]. |
|
301 |
y ~= (aPoint y) ifTrue:[^ false]. |
|
302 |
^ true |
|
303 |
]. |
|
304 |
aPoint respondsToArithmetic ifFalse:[ ^ false]. |
|
305 |
p := aPoint asPoint. |
|
306 |
x ~= (p x) ifTrue:[^ false]. |
|
307 |
y ~= (p y) ifTrue:[^ false]. |
|
308 |
^ true |
|
1 | 309 |
! |
310 |
||
637 | 311 |
> aPoint |
312 |
"return true if the receiver is below and to the right |
|
313 |
of the argument, aPoint" |
|
314 |
||
315 |
|p| |
|
356 | 316 |
|
637 | 317 |
p := aPoint asPoint. |
1339 | 318 |
(p x) < x ifTrue:[ |
319 |
(p y) < y ifTrue:[^ true]. |
|
320 |
]. |
|
321 |
^ false |
|
322 |
||
323 |
"Modified: 7.5.1996 / 12:11:15 / cg" |
|
637 | 324 |
! |
356 | 325 |
|
637 | 326 |
hash |
327 |
"return a number for hashing" |
|
356 | 328 |
|
637 | 329 |
x = y ifTrue:[^ x hash]. |
330 |
" |
|
331 |
used to be: |
|
332 |
^ (x hash) bitXor:(y hash) |
|
333 |
the following handles 1@x vs. x@1 better: |
|
334 |
" |
|
335 |
^ (x hash) bitXor:(y hash bitShift:12) |
|
336 |
! |
|
356 | 337 |
|
637 | 338 |
max:aPoint |
339 |
"return the lower right corner of the rectangle uniquely defined by |
|
340 |
the receiver and the argument, aPoint" |
|
341 |
||
342 |
|p maxX maxY| |
|
356 | 343 |
|
637 | 344 |
p := aPoint asPoint. |
345 |
maxX := x max:(p x). |
|
346 |
maxY := y max:(p y). |
|
347 |
^ maxX @ maxY |
|
348 |
! |
|
349 |
||
350 |
min:aPoint |
|
351 |
"return the upper left corner of the rectangle uniquely defined by |
|
352 |
the receiver and the argument, aPoint" |
|
353 |
||
354 |
|p minX minY| |
|
355 |
||
356 |
p := aPoint asPoint. |
|
357 |
minX := x min:(p x). |
|
358 |
minY := y min:(p y). |
|
359 |
^ minX @ minY |
|
356 | 360 |
! ! |
361 |
||
362 |
!Point methodsFor:'converting'! |
|
363 |
||
3636 | 364 |
asComplex |
365 |
"Return a complex number whose real and imaginary components are the x and y |
|
366 |
coordinates of the receiver." |
|
367 |
||
368 |
^ x % y |
|
369 |
||
370 |
"Modified: / 9.7.1998 / 10:21:10 / cg" |
|
371 |
! |
|
372 |
||
637 | 373 |
asFractionalLayout |
374 |
"return a LayoutOrigin from the receiver, |
|
375 |
treating the receiver coordinates as fractional parts |
|
376 |
(i.e. relative to superview). |
|
377 |
Notice: in 10.5.x LayoutOrigin is not yet officially released." |
|
378 |
||
379 |
^ LayoutOrigin fractionalFromPoint:self |
|
356 | 380 |
|
637 | 381 |
" |
382 |
(0@0.5) asFractionalLayout |
|
383 |
(0@0.5) asLayout |
|
384 |
(0@10) asLayout |
|
385 |
(0@10) asOffsetLayout |
|
386 |
" |
|
356 | 387 |
|
388 |
! |
|
1 | 389 |
|
356 | 390 |
asLayout |
391 |
"return a LayoutOrigin from the receiver. |
|
392 |
If the receiver coordinates are between 0 and 1, take |
|
393 |
them as fractional parts (relative to superview). |
|
394 |
Otherwise, treat them as absolute offsets. |
|
395 |
Notice: in 10.5.x LayoutOrigin is not yet released." |
|
396 |
||
397 |
^ LayoutOrigin fromPoint:self |
|
398 |
||
399 |
" |
|
400 |
(0@0.5) asFractionalLayout |
|
401 |
(0@0.5) asLayout |
|
402 |
(0@10) asLayout |
|
403 |
(0@10) asOffsetLayout |
|
404 |
" |
|
405 |
||
406 |
! |
|
407 |
||
408 |
asOffsetLayout |
|
409 |
"return a LayoutOrigin from the receiver, |
|
410 |
treating the receiver coordinates as absolute offsets. |
|
411 |
Notice: in 10.5.x LayoutOrigin is not yet released." |
|
412 |
||
413 |
^ LayoutOrigin offsetFromPoint:self |
|
414 |
||
415 |
" |
|
416 |
(0@0.5) asFractionalLayout |
|
417 |
(0@0.5) asLayout |
|
418 |
(0@10) asLayout |
|
419 |
(0@10) asOffsetLayout |
|
420 |
" |
|
421 |
||
1 | 422 |
! |
423 |
||
637 | 424 |
asPoint |
425 |
"return the receiver as Point - this is the receiver" |
|
356 | 426 |
|
637 | 427 |
^ self |
356 | 428 |
! |
429 |
||
430 |
asRectangle |
|
431 |
"return a zero-width rectangle consisting of origin |
|
432 |
and corner being the receiver" |
|
433 |
||
434 |
^ self corner:self |
|
435 |
||
436 |
" |
|
437 |
(0@10) asRectangle |
|
438 |
" |
|
439 |
! |
|
440 |
||
637 | 441 |
corner:aPoint |
442 |
"return a rectangle whose origin is self and corner is aPoint" |
|
443 |
||
444 |
^ Rectangle origin:self corner:aPoint |
|
445 |
! |
|
446 |
||
447 |
extent:aPoint |
|
448 |
"return a rectangle whose origin is self and extent is aPoint" |
|
356 | 449 |
|
637 | 450 |
^ Rectangle origin:self extent:aPoint |
451 |
! |
|
452 |
||
453 |
fromLiteralArrayEncoding:encoding |
|
454 |
"read my values from an encoding. |
|
455 |
The encoding is supposed to be of the form: (Point xValue yValue)" |
|
456 |
||
457 |
x := encoding at:2. |
|
458 |
y := encoding at:3. |
|
356 | 459 |
|
460 |
" |
|
637 | 461 |
Point new fromLiteralArrayEncoding:#(Point 10 20) |
356 | 462 |
" |
379 | 463 |
! |
464 |
||
421 | 465 |
literalArrayEncoding |
1245 | 466 |
"encode myself as an array, from which a copy of the receiver |
467 |
can be reconstructed with #decodeAsLiteralArray. |
|
421 | 468 |
The encoding is: (Point xValue yValue)" |
469 |
||
470 |
^ Array |
|
1245 | 471 |
with:#Point |
472 |
with:x |
|
473 |
with:y |
|
421 | 474 |
|
475 |
||
476 |
" |
|
477 |
Point new fromLiteralArrayEncoding:#(Point 10 20) |
|
478 |
(10@20) literalArrayEncoding |
|
479 |
" |
|
480 |
||
481 |
"Modified: 1.9.1995 / 02:18:29 / claus" |
|
1245 | 482 |
"Modified: 22.4.1996 / 13:00:32 / cg" |
421 | 483 |
! |
484 |
||
3488
6e1feb9ead68
prefRect may now be a valueHolder or block
Claus Gittinger <cg@exept.de>
parents:
3238
diff
changeset
|
485 |
rectangleRelativeTo:aRectangle preferred:prefRectHolder |
637 | 486 |
"compute a displayRectangle, treating the receiver like a |
487 |
layoutorigin. This allows point to be used interchangable with |
|
488 |
LayoutOrigins." |
|
379 | 489 |
|
3488
6e1feb9ead68
prefRect may now be a valueHolder or block
Claus Gittinger <cg@exept.de>
parents:
3238
diff
changeset
|
490 |
^ (self asLayout) rectangleRelativeTo:aRectangle preferred:prefRectHolder |
379 | 491 |
|
492 |
" |
|
637 | 493 |
consider the case, where a view has a preferred extent of 50@50 |
494 |
and is to be positioned in its superview which has size 100@100. |
|
495 |
For absolute origin: |
|
3488
6e1feb9ead68
prefRect may now be a valueHolder or block
Claus Gittinger <cg@exept.de>
parents:
3238
diff
changeset
|
496 |
(10@20) rectangleRelativeTo:(0@0 corner:100@100) preferred:(0@0 corner:50@50) |
1 | 497 |
|
637 | 498 |
for relative origin: |
3488
6e1feb9ead68
prefRect may now be a valueHolder or block
Claus Gittinger <cg@exept.de>
parents:
3238
diff
changeset
|
499 |
(0.5@0.5) rectangleRelativeTo:(0@0 corner:100@100) preferred:(0@0 corner:50@50) |
637 | 500 |
" |
3488
6e1feb9ead68
prefRect may now be a valueHolder or block
Claus Gittinger <cg@exept.de>
parents:
3238
diff
changeset
|
501 |
|
6e1feb9ead68
prefRect may now be a valueHolder or block
Claus Gittinger <cg@exept.de>
parents:
3238
diff
changeset
|
502 |
"Modified: / 27.5.1998 / 10:20:13 / cg" |
1 | 503 |
! ! |
504 |
||
77 | 505 |
!Point methodsFor:'destructive transformations'! |
506 |
||
507 |
scaleBy:aScale |
|
508 |
"scale the receiver, by replacing coordinates by the product |
|
180 | 509 |
of the receivers coordinates and the scale (a Point or Number). |
510 |
This is destructive (modifies the receiver, not a copy) and |
|
511 |
should only be used if you know, that you are the exclusive owner |
|
512 |
of the receiver." |
|
77 | 513 |
|
514 |
|scalePoint| |
|
515 |
||
180 | 516 |
(aScale isMemberOf:Point) ifTrue:[ "type hint to stc" |
517 |
x := x * aScale x. |
|
518 |
y := y * aScale y. |
|
519 |
^ self |
|
77 | 520 |
]. |
521 |
aScale isNumber ifTrue:[ |
|
180 | 522 |
x := x * aScale. |
523 |
y := y * aScale. |
|
524 |
^ self |
|
77 | 525 |
]. |
526 |
||
527 |
"this is the general (& clean) code ..." |
|
528 |
||
529 |
scalePoint := aScale asPoint. |
|
530 |
x := x * scalePoint x. |
|
531 |
y := y * scalePoint y |
|
532 |
! |
|
533 |
||
534 |
translateBy:anOffset |
|
535 |
"translate the receiver, by replacing coordinates by the sum |
|
180 | 536 |
of the receivers coordinated and the scale (a Point or Number). |
537 |
This is destructive (modifies the receiver, not a copy) and |
|
538 |
should only be used if you know, that you are the exclusive owner |
|
539 |
of the receiver." |
|
77 | 540 |
|
541 |
|offsetPoint| |
|
542 |
||
180 | 543 |
(anOffset isMemberOf:Point) ifTrue:[ "type hint to stc" |
544 |
x := x + anOffset x. |
|
545 |
y := y + anOffset y. |
|
546 |
^ self |
|
77 | 547 |
]. |
548 |
anOffset isNumber ifTrue:[ |
|
180 | 549 |
x := x + anOffset. |
550 |
y := y + anOffset. |
|
551 |
^ self |
|
77 | 552 |
]. |
553 |
||
554 |
"this is the general (& clean) code ..." |
|
555 |
||
556 |
offsetPoint := anOffset asPoint. |
|
557 |
x := x + anOffset x. |
|
558 |
y := y + anOffset y |
|
559 |
! ! |
|
560 |
||
637 | 561 |
!Point methodsFor:'misc'! |
562 |
||
563 |
abs |
|
564 |
"return a new point with my coordinates taken from the absolute values." |
|
565 |
||
566 |
^ (x abs) @ (y abs) |
|
567 |
! |
|
568 |
||
3562
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
569 |
ceiling |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
570 |
"return a new point with my coordinates truncated towards positive infinity." |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
571 |
|
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
572 |
^ (x ceiling) @ (y ceiling) |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
573 |
! |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
574 |
|
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
575 |
floor |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
576 |
"return a new point with my coordinates truncated towards negative infinity." |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
577 |
|
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
578 |
^ (x floor) @ (y floor) |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
579 |
! |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
580 |
|
637 | 581 |
quadrant |
582 |
"return the number of the quadrant containing the receiver. |
|
583 |
quadrants are named as follows: |
|
356 | 584 |
|
637 | 585 |
^ 2 | 3 |
586 |
Y ------ |
|
587 |
1 | 0 |
|
588 |
||
589 |
X > |
|
590 |
||
591 |
Q: what is to be returned if any coordinate is 0 ? |
|
592 |
" |
|
77 | 593 |
|
637 | 594 |
^ 0@0 quadrantContaining:self |
595 |
||
596 |
" |
|
597 |
(0@0) quadrant |
|
598 |
(1@1) quadrant |
|
599 |
(-1@1) quadrant |
|
600 |
(-1@-1) quadrant |
|
601 |
(1@-1) quadrant |
|
602 |
" |
|
356 | 603 |
! |
77 | 604 |
|
637 | 605 |
quadrantContaining:aPoint |
606 |
"return the number of the quadrant containing aPoint placing |
|
607 |
the receiver at the origin, where the quadrants are numbered as |
|
608 |
follows: |
|
609 |
^ 2 | 3 |
|
610 |
Y ------ |
|
611 |
1 | 0 |
|
612 |
||
613 |
X > |
|
614 |
This can be used for polygon operations (see Foley for examples). |
|
615 |
" |
|
616 |
||
617 |
aPoint x > x ifTrue:[ |
|
618 |
aPoint y >= y ifTrue:[^ 3]. |
|
619 |
^ 0 |
|
620 |
]. |
|
621 |
aPoint y >= y ifTrue: [^ 2]. |
|
622 |
^ 1 |
|
356 | 623 |
|
637 | 624 |
" |
625 |
(10 @ 10) quadrantContaining:(15 @ 15) |
|
626 |
(10 @ 10) quadrantContaining:(5 @ 5) |
|
627 |
(10 @ 10) quadrantContaining:(5 @ 15) |
|
628 |
(10 @ 10) quadrantContaining:(15 @ 5) |
|
629 |
" |
|
630 |
! |
|
631 |
||
3562
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
632 |
roundTo:aNumber |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
633 |
"return a new point with my coordinates rounded towards the next |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
634 |
multiple of aNumber." |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
635 |
|
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
636 |
^ (self / aNumber) rounded * aNumber |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
637 |
! |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
638 |
|
3636 | 639 |
rounded |
640 |
"return a new point with my coordinates rounded to the next integer." |
|
3562
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
641 |
|
3636 | 642 |
^ (x rounded) @ (y rounded) |
3562
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
643 |
! |
637 | 644 |
|
3562
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
645 |
truncateTo:aNumber |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
646 |
"return a new point with my coordinates truncated towards zero to the next |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
647 |
multiple of aNumber." |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
648 |
|
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
649 |
^ (self quo:aNumber) rounded * aNumber |
3636 | 650 |
! |
651 |
||
652 |
truncated |
|
653 |
"return a new point with my coordinates truncated as integer." |
|
654 |
||
655 |
^ (x truncated) @ (y truncated) |
|
77 | 656 |
! ! |
657 |
||
356 | 658 |
!Point methodsFor:'point functions'! |
1 | 659 |
|
660 |
dist:aPoint |
|
661 |
"return the distance between aPoint and the receiver." |
|
662 |
||
663 |
^ (aPoint - self) r |
|
664 |
! |
|
665 |
||
666 |
dotProduct:aPoint |
|
57 | 667 |
"return a number that is the dot product of the receiver and |
1 | 668 |
the argument, aPoint. That is, the two points are |
57 | 669 |
multiplied and the coordinates of the result summed." |
1 | 670 |
|
671 |
|temp| |
|
672 |
||
673 |
temp := self * aPoint. |
|
674 |
^ temp x abs + temp y abs |
|
675 |
! |
|
676 |
||
677 |
grid:gridPoint |
|
678 |
"return a new point with coordinates grided (i.e. rounded to the |
|
679 |
nearest point on the grid)" |
|
680 |
||
681 |
|newX newY gridX gridY| |
|
682 |
||
683 |
gridX := gridPoint x. |
|
684 |
(gridX <= 1) ifTrue:[ |
|
180 | 685 |
newX := x asInteger |
1 | 686 |
] ifFalse:[ |
180 | 687 |
newX := ((x + (gridX // 2)) // gridX) * gridX |
1 | 688 |
]. |
689 |
gridY := gridPoint y. |
|
690 |
(gridY <= 1) ifTrue:[ |
|
180 | 691 |
newY := y asInteger |
1 | 692 |
] ifFalse:[ |
180 | 693 |
newY := ((y + (gridY // 2)) // gridY) * gridY |
1 | 694 |
]. |
695 |
^ newX @ newY |
|
77 | 696 |
! |
697 |
||
276 | 698 |
nearestIntegerPointOnLineFrom: point1 to: point2 |
699 |
"return the closest integer point to the receiver on the line |
|
700 |
determined by (point1, point2)--much faster than the more |
|
701 |
accurate version if the receiver and arguments are integer points. |
|
702 |
This method was found in the Manchester goody library." |
|
703 |
||
704 |
| dX dY newX newY dX2 dY2 intersect scale coeff | |
|
705 |
||
706 |
dX := point2 x - point1 x. |
|
707 |
dY := point2 y - point1 y. |
|
708 |
(dX = 0)ifTrue: [ |
|
709 |
(dY = 0) ifTrue: [ |
|
710 |
intersect := point1 |
|
711 |
] ifFalse: [ |
|
712 |
newX := point1 x. |
|
713 |
scale := (y - point1 y) / dY. |
|
359 | 714 |
scale > 1 ifTrue:[ |
715 |
newY := point2 y |
|
716 |
] ifFalse: [ |
|
717 |
scale < 0 ifTrue: [ |
|
718 |
newY := point1 y |
|
719 |
] ifFalse: [ |
|
720 |
newY := y |
|
721 |
] |
|
722 |
]. |
|
276 | 723 |
|
724 |
^ (newX @ newY) rounded |
|
725 |
] |
|
726 |
] ifFalse: [ |
|
727 |
(dY = 0) ifTrue: [ |
|
728 |
intersect := x @ point1 y |
|
729 |
] ifFalse:[ |
|
730 |
dX2 := dX * dX. |
|
731 |
dY2 := dY * dY. |
|
732 |
coeff := ((dX * (y - point1 y)) - |
|
733 |
((x - point1 x) * dY)) / (dX2 + dY2). |
|
734 |
newX := x + (dY * coeff). |
|
735 |
newY := y - (dX * coeff). |
|
736 |
intersect := newX @ newY |
|
737 |
] |
|
738 |
]. |
|
739 |
||
740 |
scale := (intersect x - point1 x) / dX. |
|
741 |
||
742 |
^ (scale > 1 ifTrue: [point2] ifFalse: [ |
|
743 |
scale < 0 ifTrue: [point1] ifFalse: [intersect]]) rounded |
|
744 |
||
745 |
" |
|
746 |
120@40 nearestIntegerPointOnLineFrom: 30@120 to: 100@120 |
|
747 |
0@0 nearestIntegerPointOnLineFrom: 10@10 to: 100@100 |
|
748 |
" |
|
1 | 749 |
! ! |
750 |
||
356 | 751 |
!Point methodsFor:'polar coordinates'! |
752 |
||
637 | 753 |
angle |
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
754 |
"return the receiver's angle (in degrees) in a polar coordinate system. |
637 | 755 |
(i.e. the angle of a vector from 0@0 to the receiver). |
756 |
OBSOLETE ST/X interface; use theta for ST-80 compatibility." |
|
757 |
||
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
758 |
^ self theta radiansToDegrees |
637 | 759 |
|
760 |
" |
|
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
761 |
(1@1) angle |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
762 |
(2@1) angle |
637 | 763 |
" |
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
764 |
|
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
765 |
"Modified: 2.4.1997 / 00:02:17 / cg" |
637 | 766 |
! |
767 |
||
356 | 768 |
r |
769 |
"return the receiver's radius in a polar coordinate system. |
|
770 |
(i.e. the length of a vector from 0@0 to the receiver)" |
|
771 |
||
2578 | 772 |
^ ((x*x) + (y*y)) sqrt |
1 | 773 |
|
356 | 774 |
" |
2578 | 775 |
(1@1) r |
776 |
(2@1) r |
|
777 |
(2@0) r |
|
778 |
(0@2) r |
|
779 |
(-2@-2) r |
|
780 |
(2@2) r |
|
356 | 781 |
" |
1 | 782 |
! |
783 |
||
356 | 784 |
theta |
785 |
"return the receiver's angle (in radians) in a polar coordinate system. |
|
786 |
(i.e. the angle of a vector from 0@0 to the receiver)" |
|
1 | 787 |
|
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
788 |
|theta t| |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
789 |
|
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
790 |
x = 0 ifTrue:[ |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
791 |
y >= 0 ifTrue:[ |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
792 |
^ Float pi * 0.5 |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
793 |
]. |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
794 |
^ Float pi * 1.5. |
356 | 795 |
]. |
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
796 |
|
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
797 |
t := y asFloat / x asFloat. |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
798 |
theta := t arcTan. |
356 | 799 |
x < 0 ifTrue:[ |
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
800 |
^ theta +Float pi |
356 | 801 |
]. |
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
802 |
theta < 0 ifTrue:[ |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
803 |
^ theta + (Float pi * 2.0) |
356 | 804 |
]. |
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
805 |
^ theta. |
356 | 806 |
" |
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
807 |
(1@1) theta |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
808 |
(2@1) theta |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
809 |
(-2@1) theta |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
810 |
(-2@-1) theta |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
811 |
(0@-1) theta |
356 | 812 |
" |
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
813 |
|
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
814 |
"Modified: 2.4.1997 / 00:15:12 / cg" |
1 | 815 |
! ! |
356 | 816 |
|
637 | 817 |
!Point methodsFor:'printing & storing'! |
818 |
||
819 |
printOn:aStream |
|
820 |
"append a printed representation of the receiver to aStream" |
|
821 |
||
822 |
x printOn:aStream. |
|
823 |
aStream nextPut:$@. |
|
824 |
y printOn:aStream |
|
825 |
! |
|
826 |
||
827 |
storeOn:aStream |
|
828 |
"append my storeString to aStream" |
|
829 |
||
830 |
aStream nextPut:$(. |
|
831 |
x storeOn:aStream. |
|
832 |
aStream nextPut:$@. |
|
833 |
y storeOn:aStream. |
|
834 |
aStream nextPut:$) |
|
835 |
! ! |
|
836 |
||
837 |
!Point methodsFor:'queries'! |
|
838 |
||
839 |
isPoint |
|
840 |
"return true, if the receiver is some kind of point" |
|
841 |
||
842 |
^ true |
|
843 |
! ! |
|
844 |
||
845 |
!Point methodsFor:'transformations'! |
|
846 |
||
847 |
* scale |
|
848 |
"Return a new Point that is the product of the |
|
849 |
receiver and scale (which is a Point or Number)." |
|
850 |
||
851 |
|scalePoint| |
|
852 |
||
853 |
"speedup for common cases ..." |
|
854 |
||
855 |
(scale isMemberOf:Point) ifTrue:[ |
|
2273 | 856 |
^ Point x:(x * scale x) y:(y * scale y) |
637 | 857 |
]. |
858 |
(scale isMemberOf:SmallInteger) ifTrue:[ |
|
2273 | 859 |
^ Point x:(x * scale) y:(y * scale) |
637 | 860 |
]. |
861 |
scale isNumber ifTrue:[ |
|
2273 | 862 |
^ Point x:(x * scale) y:(y * scale) |
637 | 863 |
]. |
864 |
||
865 |
"this is the general (& clean) code ..." |
|
866 |
||
867 |
scalePoint := scale asPoint. |
|
868 |
^ (x * scalePoint x) @ (y * scalePoint y) |
|
2273 | 869 |
|
870 |
"Modified: 25.1.1997 / 17:28:11 / cg" |
|
637 | 871 |
! |
872 |
||
873 |
+ scale |
|
874 |
"Return a new Point that is the sum of the |
|
875 |
receiver and scale (which is a Point or Number)." |
|
876 |
||
877 |
|scalePoint| |
|
878 |
||
879 |
"speedup for common cases ..." |
|
880 |
||
881 |
(scale isMemberOf:Point) ifTrue:[ |
|
2273 | 882 |
^ Point x:(x + scale x) y:(y + scale y) |
637 | 883 |
]. |
884 |
(scale isMemberOf:SmallInteger) ifTrue:[ |
|
2273 | 885 |
"/ same as below, but stc can do better here |
886 |
^ Point x:(x + scale) y:(y + scale) |
|
637 | 887 |
]. |
888 |
scale isNumber ifTrue:[ |
|
2273 | 889 |
^ Point x:(x + scale) y:(y + scale) |
637 | 890 |
]. |
891 |
||
892 |
"this is the general (& clean) code ..." |
|
893 |
||
894 |
scalePoint := scale asPoint. |
|
895 |
^ (x + scalePoint x) @ (y + scalePoint y) |
|
2273 | 896 |
|
897 |
"Modified: 25.1.1997 / 17:27:46 / cg" |
|
637 | 898 |
! |
899 |
||
900 |
- scale |
|
901 |
"Return a new Point that is the difference of the |
|
902 |
receiver and scale (which is a Point or Number)." |
|
903 |
||
904 |
|scalePoint| |
|
905 |
||
906 |
"speedup for common cases ..." |
|
907 |
||
908 |
(scale isMemberOf:Point) ifTrue:[ |
|
2273 | 909 |
^ Point x:(x - scale x) y:(y - scale y) |
637 | 910 |
]. |
911 |
(scale isMemberOf:SmallInteger) ifTrue:[ |
|
2273 | 912 |
"/ same as below, but stc can do better here |
913 |
^ Point x:(x - scale) y:(y - scale) |
|
637 | 914 |
]. |
915 |
scale isNumber ifTrue:[ |
|
2273 | 916 |
^ Point x:(x - scale) y:(y - scale) |
637 | 917 |
]. |
918 |
||
919 |
"this is the general (& clean) code ..." |
|
920 |
||
921 |
scalePoint := scale asPoint. |
|
922 |
^ (x - scalePoint x) @ (y - scalePoint y) |
|
2273 | 923 |
|
924 |
"Modified: 25.1.1997 / 17:27:40 / cg" |
|
637 | 925 |
! |
926 |
||
927 |
/ scale |
|
928 |
"Return a new Point that is the integer quotient of the |
|
929 |
receiver and scale (which is a Point or Number)." |
|
930 |
||
931 |
|scalePoint| |
|
932 |
||
933 |
"speedup for common cases ..." |
|
934 |
||
935 |
(scale isMemberOf:Point) ifTrue:[ |
|
936 |
^ (x / scale x) @ (y / scale y) |
|
937 |
]. |
|
938 |
scale isNumber ifTrue:[ |
|
939 |
^ (x / scale) @ (y / scale) |
|
940 |
]. |
|
941 |
||
942 |
"this is the general (& clean) code ..." |
|
943 |
||
944 |
scalePoint := scale asPoint. |
|
945 |
^ (x / scalePoint x) @ (y / scalePoint y) |
|
946 |
! |
|
947 |
||
948 |
// scale |
|
949 |
"Return a new Point that is the quotient of the |
|
950 |
receiver and scale (which is a Point or Number)." |
|
951 |
||
952 |
|scalePoint| |
|
953 |
||
954 |
scalePoint := scale asPoint. |
|
955 |
^ (x // scalePoint x) @ (y // scalePoint y) |
|
956 |
! |
|
957 |
||
958 |
negated |
|
959 |
"return a new point with my coordinates negated |
|
960 |
i.e. the receiver mirrored at the origin" |
|
961 |
||
962 |
^ (x negated) @ (y negated) |
|
963 |
! |
|
964 |
||
965 |
reciprocal |
|
966 |
"return a new point where the coordinates are |
|
967 |
the reciproce of mine" |
|
968 |
||
969 |
^ (1 / x) @ (1 / y) |
|
970 |
! |
|
971 |
||
972 |
scaledBy:aScale |
|
973 |
"return a new Point that is the product of the |
|
974 |
receiver and scale (which is a Point or Number)." |
|
975 |
||
976 |
^ self * aScale |
|
977 |
! |
|
978 |
||
979 |
translatedBy:anOffset |
|
980 |
"return a new Point that is the sum of the |
|
981 |
receiver and scale (which is a Point or Number)." |
|
982 |
||
983 |
^ self + anOffset |
|
984 |
! ! |
|
985 |
||
2273 | 986 |
!Point class methodsFor:'documentation'! |
637 | 987 |
|
988 |
version |
|
3636 | 989 |
^ '$Header: /cvs/stx/stx/libbasic/Point.st,v 1.47 1998-07-09 08:21:23 cg Exp $' |
637 | 990 |
! ! |
356 | 991 |
Point initialize! |