author | Claus Gittinger <cg@exept.de> |
Wed, 06 Oct 1999 20:53:33 +0200 | |
changeset 4857 | c29c6910114b |
parent 3636 | cba3cd259b32 |
child 4865 | 55da20740fcd |
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 |
" |
|
4857 | 335 |
^ (x hash) bitXor:(y hash bitShift:16) |
336 |
||
337 |
||
338 |
||
339 |
||
340 |
||
637 | 341 |
! |
356 | 342 |
|
637 | 343 |
max:aPoint |
344 |
"return the lower right corner of the rectangle uniquely defined by |
|
345 |
the receiver and the argument, aPoint" |
|
346 |
||
347 |
|p maxX maxY| |
|
356 | 348 |
|
637 | 349 |
p := aPoint asPoint. |
350 |
maxX := x max:(p x). |
|
351 |
maxY := y max:(p y). |
|
352 |
^ maxX @ maxY |
|
353 |
! |
|
354 |
||
355 |
min:aPoint |
|
356 |
"return the upper left corner of the rectangle uniquely defined by |
|
357 |
the receiver and the argument, aPoint" |
|
358 |
||
359 |
|p minX minY| |
|
360 |
||
361 |
p := aPoint asPoint. |
|
362 |
minX := x min:(p x). |
|
363 |
minY := y min:(p y). |
|
364 |
^ minX @ minY |
|
356 | 365 |
! ! |
366 |
||
367 |
!Point methodsFor:'converting'! |
|
368 |
||
3636 | 369 |
asComplex |
370 |
"Return a complex number whose real and imaginary components are the x and y |
|
371 |
coordinates of the receiver." |
|
372 |
||
373 |
^ x % y |
|
374 |
||
375 |
"Modified: / 9.7.1998 / 10:21:10 / cg" |
|
376 |
! |
|
377 |
||
637 | 378 |
asFractionalLayout |
379 |
"return a LayoutOrigin from the receiver, |
|
380 |
treating the receiver coordinates as fractional parts |
|
381 |
(i.e. relative to superview). |
|
382 |
Notice: in 10.5.x LayoutOrigin is not yet officially released." |
|
383 |
||
384 |
^ LayoutOrigin fractionalFromPoint:self |
|
356 | 385 |
|
637 | 386 |
" |
387 |
(0@0.5) asFractionalLayout |
|
388 |
(0@0.5) asLayout |
|
389 |
(0@10) asLayout |
|
390 |
(0@10) asOffsetLayout |
|
391 |
" |
|
356 | 392 |
|
393 |
! |
|
1 | 394 |
|
356 | 395 |
asLayout |
396 |
"return a LayoutOrigin from the receiver. |
|
397 |
If the receiver coordinates are between 0 and 1, take |
|
398 |
them as fractional parts (relative to superview). |
|
399 |
Otherwise, treat them as absolute offsets. |
|
400 |
Notice: in 10.5.x LayoutOrigin is not yet released." |
|
401 |
||
402 |
^ LayoutOrigin fromPoint:self |
|
403 |
||
404 |
" |
|
405 |
(0@0.5) asFractionalLayout |
|
406 |
(0@0.5) asLayout |
|
407 |
(0@10) asLayout |
|
408 |
(0@10) asOffsetLayout |
|
409 |
" |
|
410 |
||
411 |
! |
|
412 |
||
413 |
asOffsetLayout |
|
414 |
"return a LayoutOrigin from the receiver, |
|
415 |
treating the receiver coordinates as absolute offsets. |
|
416 |
Notice: in 10.5.x LayoutOrigin is not yet released." |
|
417 |
||
418 |
^ LayoutOrigin offsetFromPoint:self |
|
419 |
||
420 |
" |
|
421 |
(0@0.5) asFractionalLayout |
|
422 |
(0@0.5) asLayout |
|
423 |
(0@10) asLayout |
|
424 |
(0@10) asOffsetLayout |
|
425 |
" |
|
426 |
||
1 | 427 |
! |
428 |
||
637 | 429 |
asPoint |
430 |
"return the receiver as Point - this is the receiver" |
|
356 | 431 |
|
637 | 432 |
^ self |
356 | 433 |
! |
434 |
||
435 |
asRectangle |
|
436 |
"return a zero-width rectangle consisting of origin |
|
437 |
and corner being the receiver" |
|
438 |
||
439 |
^ self corner:self |
|
440 |
||
441 |
" |
|
442 |
(0@10) asRectangle |
|
443 |
" |
|
444 |
! |
|
445 |
||
637 | 446 |
corner:aPoint |
447 |
"return a rectangle whose origin is self and corner is aPoint" |
|
448 |
||
449 |
^ Rectangle origin:self corner:aPoint |
|
450 |
! |
|
451 |
||
452 |
extent:aPoint |
|
453 |
"return a rectangle whose origin is self and extent is aPoint" |
|
356 | 454 |
|
637 | 455 |
^ Rectangle origin:self extent:aPoint |
456 |
! |
|
457 |
||
458 |
fromLiteralArrayEncoding:encoding |
|
459 |
"read my values from an encoding. |
|
460 |
The encoding is supposed to be of the form: (Point xValue yValue)" |
|
461 |
||
462 |
x := encoding at:2. |
|
463 |
y := encoding at:3. |
|
356 | 464 |
|
465 |
" |
|
637 | 466 |
Point new fromLiteralArrayEncoding:#(Point 10 20) |
356 | 467 |
" |
379 | 468 |
! |
469 |
||
421 | 470 |
literalArrayEncoding |
1245 | 471 |
"encode myself as an array, from which a copy of the receiver |
472 |
can be reconstructed with #decodeAsLiteralArray. |
|
421 | 473 |
The encoding is: (Point xValue yValue)" |
474 |
||
475 |
^ Array |
|
1245 | 476 |
with:#Point |
477 |
with:x |
|
478 |
with:y |
|
421 | 479 |
|
480 |
||
481 |
" |
|
482 |
Point new fromLiteralArrayEncoding:#(Point 10 20) |
|
483 |
(10@20) literalArrayEncoding |
|
484 |
" |
|
485 |
||
486 |
"Modified: 1.9.1995 / 02:18:29 / claus" |
|
1245 | 487 |
"Modified: 22.4.1996 / 13:00:32 / cg" |
421 | 488 |
! |
489 |
||
3488
6e1feb9ead68
prefRect may now be a valueHolder or block
Claus Gittinger <cg@exept.de>
parents:
3238
diff
changeset
|
490 |
rectangleRelativeTo:aRectangle preferred:prefRectHolder |
637 | 491 |
"compute a displayRectangle, treating the receiver like a |
492 |
layoutorigin. This allows point to be used interchangable with |
|
493 |
LayoutOrigins." |
|
379 | 494 |
|
3488
6e1feb9ead68
prefRect may now be a valueHolder or block
Claus Gittinger <cg@exept.de>
parents:
3238
diff
changeset
|
495 |
^ (self asLayout) rectangleRelativeTo:aRectangle preferred:prefRectHolder |
379 | 496 |
|
497 |
" |
|
637 | 498 |
consider the case, where a view has a preferred extent of 50@50 |
499 |
and is to be positioned in its superview which has size 100@100. |
|
500 |
For absolute origin: |
|
3488
6e1feb9ead68
prefRect may now be a valueHolder or block
Claus Gittinger <cg@exept.de>
parents:
3238
diff
changeset
|
501 |
(10@20) rectangleRelativeTo:(0@0 corner:100@100) preferred:(0@0 corner:50@50) |
1 | 502 |
|
637 | 503 |
for relative origin: |
3488
6e1feb9ead68
prefRect may now be a valueHolder or block
Claus Gittinger <cg@exept.de>
parents:
3238
diff
changeset
|
504 |
(0.5@0.5) rectangleRelativeTo:(0@0 corner:100@100) preferred:(0@0 corner:50@50) |
637 | 505 |
" |
3488
6e1feb9ead68
prefRect may now be a valueHolder or block
Claus Gittinger <cg@exept.de>
parents:
3238
diff
changeset
|
506 |
|
6e1feb9ead68
prefRect may now be a valueHolder or block
Claus Gittinger <cg@exept.de>
parents:
3238
diff
changeset
|
507 |
"Modified: / 27.5.1998 / 10:20:13 / cg" |
1 | 508 |
! ! |
509 |
||
77 | 510 |
!Point methodsFor:'destructive transformations'! |
511 |
||
512 |
scaleBy:aScale |
|
513 |
"scale the receiver, by replacing coordinates by the product |
|
180 | 514 |
of the receivers coordinates and the scale (a Point or Number). |
515 |
This is destructive (modifies the receiver, not a copy) and |
|
516 |
should only be used if you know, that you are the exclusive owner |
|
517 |
of the receiver." |
|
77 | 518 |
|
519 |
|scalePoint| |
|
520 |
||
180 | 521 |
(aScale isMemberOf:Point) ifTrue:[ "type hint to stc" |
522 |
x := x * aScale x. |
|
523 |
y := y * aScale y. |
|
524 |
^ self |
|
77 | 525 |
]. |
526 |
aScale isNumber ifTrue:[ |
|
180 | 527 |
x := x * aScale. |
528 |
y := y * aScale. |
|
529 |
^ self |
|
77 | 530 |
]. |
531 |
||
532 |
"this is the general (& clean) code ..." |
|
533 |
||
534 |
scalePoint := aScale asPoint. |
|
535 |
x := x * scalePoint x. |
|
536 |
y := y * scalePoint y |
|
537 |
! |
|
538 |
||
539 |
translateBy:anOffset |
|
540 |
"translate the receiver, by replacing coordinates by the sum |
|
180 | 541 |
of the receivers coordinated and the scale (a Point or Number). |
542 |
This is destructive (modifies the receiver, not a copy) and |
|
543 |
should only be used if you know, that you are the exclusive owner |
|
544 |
of the receiver." |
|
77 | 545 |
|
546 |
|offsetPoint| |
|
547 |
||
180 | 548 |
(anOffset isMemberOf:Point) ifTrue:[ "type hint to stc" |
549 |
x := x + anOffset x. |
|
550 |
y := y + anOffset y. |
|
551 |
^ self |
|
77 | 552 |
]. |
553 |
anOffset isNumber ifTrue:[ |
|
180 | 554 |
x := x + anOffset. |
555 |
y := y + anOffset. |
|
556 |
^ self |
|
77 | 557 |
]. |
558 |
||
559 |
"this is the general (& clean) code ..." |
|
560 |
||
561 |
offsetPoint := anOffset asPoint. |
|
562 |
x := x + anOffset x. |
|
563 |
y := y + anOffset y |
|
564 |
! ! |
|
565 |
||
637 | 566 |
!Point methodsFor:'misc'! |
567 |
||
568 |
abs |
|
569 |
"return a new point with my coordinates taken from the absolute values." |
|
570 |
||
571 |
^ (x abs) @ (y abs) |
|
572 |
! |
|
573 |
||
3562
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
574 |
ceiling |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
575 |
"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
|
576 |
|
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
577 |
^ (x ceiling) @ (y ceiling) |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
578 |
! |
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 |
floor |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
581 |
"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
|
582 |
|
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
583 |
^ (x floor) @ (y floor) |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
584 |
! |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
585 |
|
637 | 586 |
quadrant |
587 |
"return the number of the quadrant containing the receiver. |
|
588 |
quadrants are named as follows: |
|
356 | 589 |
|
637 | 590 |
^ 2 | 3 |
591 |
Y ------ |
|
592 |
1 | 0 |
|
593 |
||
594 |
X > |
|
595 |
||
596 |
Q: what is to be returned if any coordinate is 0 ? |
|
597 |
" |
|
77 | 598 |
|
637 | 599 |
^ 0@0 quadrantContaining:self |
600 |
||
601 |
" |
|
602 |
(0@0) quadrant |
|
603 |
(1@1) quadrant |
|
604 |
(-1@1) quadrant |
|
605 |
(-1@-1) quadrant |
|
606 |
(1@-1) quadrant |
|
607 |
" |
|
356 | 608 |
! |
77 | 609 |
|
637 | 610 |
quadrantContaining:aPoint |
611 |
"return the number of the quadrant containing aPoint placing |
|
612 |
the receiver at the origin, where the quadrants are numbered as |
|
613 |
follows: |
|
614 |
^ 2 | 3 |
|
615 |
Y ------ |
|
616 |
1 | 0 |
|
617 |
||
618 |
X > |
|
619 |
This can be used for polygon operations (see Foley for examples). |
|
620 |
" |
|
621 |
||
622 |
aPoint x > x ifTrue:[ |
|
623 |
aPoint y >= y ifTrue:[^ 3]. |
|
624 |
^ 0 |
|
625 |
]. |
|
626 |
aPoint y >= y ifTrue: [^ 2]. |
|
627 |
^ 1 |
|
356 | 628 |
|
637 | 629 |
" |
630 |
(10 @ 10) quadrantContaining:(15 @ 15) |
|
631 |
(10 @ 10) quadrantContaining:(5 @ 5) |
|
632 |
(10 @ 10) quadrantContaining:(5 @ 15) |
|
633 |
(10 @ 10) quadrantContaining:(15 @ 5) |
|
634 |
" |
|
635 |
! |
|
636 |
||
3562
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
637 |
roundTo:aNumber |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
638 |
"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
|
639 |
multiple of aNumber." |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
640 |
|
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
641 |
^ (self / aNumber) rounded * aNumber |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
642 |
! |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
643 |
|
3636 | 644 |
rounded |
645 |
"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
|
646 |
|
3636 | 647 |
^ (x rounded) @ (y rounded) |
3562
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
648 |
! |
637 | 649 |
|
3562
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
650 |
truncateTo:aNumber |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
651 |
"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
|
652 |
multiple of aNumber." |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
653 |
|
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
654 |
^ (self quo:aNumber) rounded * aNumber |
3636 | 655 |
! |
656 |
||
657 |
truncated |
|
658 |
"return a new point with my coordinates truncated as integer." |
|
659 |
||
660 |
^ (x truncated) @ (y truncated) |
|
77 | 661 |
! ! |
662 |
||
356 | 663 |
!Point methodsFor:'point functions'! |
1 | 664 |
|
665 |
dist:aPoint |
|
666 |
"return the distance between aPoint and the receiver." |
|
667 |
||
668 |
^ (aPoint - self) r |
|
669 |
! |
|
670 |
||
671 |
dotProduct:aPoint |
|
57 | 672 |
"return a number that is the dot product of the receiver and |
1 | 673 |
the argument, aPoint. That is, the two points are |
57 | 674 |
multiplied and the coordinates of the result summed." |
1 | 675 |
|
676 |
|temp| |
|
677 |
||
678 |
temp := self * aPoint. |
|
679 |
^ temp x abs + temp y abs |
|
680 |
! |
|
681 |
||
682 |
grid:gridPoint |
|
683 |
"return a new point with coordinates grided (i.e. rounded to the |
|
684 |
nearest point on the grid)" |
|
685 |
||
686 |
|newX newY gridX gridY| |
|
687 |
||
688 |
gridX := gridPoint x. |
|
689 |
(gridX <= 1) ifTrue:[ |
|
180 | 690 |
newX := x asInteger |
1 | 691 |
] ifFalse:[ |
180 | 692 |
newX := ((x + (gridX // 2)) // gridX) * gridX |
1 | 693 |
]. |
694 |
gridY := gridPoint y. |
|
695 |
(gridY <= 1) ifTrue:[ |
|
180 | 696 |
newY := y asInteger |
1 | 697 |
] ifFalse:[ |
180 | 698 |
newY := ((y + (gridY // 2)) // gridY) * gridY |
1 | 699 |
]. |
700 |
^ newX @ newY |
|
77 | 701 |
! |
702 |
||
276 | 703 |
nearestIntegerPointOnLineFrom: point1 to: point2 |
704 |
"return the closest integer point to the receiver on the line |
|
705 |
determined by (point1, point2)--much faster than the more |
|
706 |
accurate version if the receiver and arguments are integer points. |
|
707 |
This method was found in the Manchester goody library." |
|
708 |
||
709 |
| dX dY newX newY dX2 dY2 intersect scale coeff | |
|
710 |
||
711 |
dX := point2 x - point1 x. |
|
712 |
dY := point2 y - point1 y. |
|
713 |
(dX = 0)ifTrue: [ |
|
714 |
(dY = 0) ifTrue: [ |
|
715 |
intersect := point1 |
|
716 |
] ifFalse: [ |
|
717 |
newX := point1 x. |
|
718 |
scale := (y - point1 y) / dY. |
|
359 | 719 |
scale > 1 ifTrue:[ |
720 |
newY := point2 y |
|
721 |
] ifFalse: [ |
|
722 |
scale < 0 ifTrue: [ |
|
723 |
newY := point1 y |
|
724 |
] ifFalse: [ |
|
725 |
newY := y |
|
726 |
] |
|
727 |
]. |
|
276 | 728 |
|
729 |
^ (newX @ newY) rounded |
|
730 |
] |
|
731 |
] ifFalse: [ |
|
732 |
(dY = 0) ifTrue: [ |
|
733 |
intersect := x @ point1 y |
|
734 |
] ifFalse:[ |
|
735 |
dX2 := dX * dX. |
|
736 |
dY2 := dY * dY. |
|
737 |
coeff := ((dX * (y - point1 y)) - |
|
738 |
((x - point1 x) * dY)) / (dX2 + dY2). |
|
739 |
newX := x + (dY * coeff). |
|
740 |
newY := y - (dX * coeff). |
|
741 |
intersect := newX @ newY |
|
742 |
] |
|
743 |
]. |
|
744 |
||
745 |
scale := (intersect x - point1 x) / dX. |
|
746 |
||
747 |
^ (scale > 1 ifTrue: [point2] ifFalse: [ |
|
748 |
scale < 0 ifTrue: [point1] ifFalse: [intersect]]) rounded |
|
749 |
||
750 |
" |
|
751 |
120@40 nearestIntegerPointOnLineFrom: 30@120 to: 100@120 |
|
752 |
0@0 nearestIntegerPointOnLineFrom: 10@10 to: 100@100 |
|
753 |
" |
|
1 | 754 |
! ! |
755 |
||
356 | 756 |
!Point methodsFor:'polar coordinates'! |
757 |
||
637 | 758 |
angle |
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
759 |
"return the receiver's angle (in degrees) in a polar coordinate system. |
637 | 760 |
(i.e. the angle of a vector from 0@0 to the receiver). |
761 |
OBSOLETE ST/X interface; use theta for ST-80 compatibility." |
|
762 |
||
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
763 |
^ self theta radiansToDegrees |
637 | 764 |
|
765 |
" |
|
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
766 |
(1@1) angle |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
767 |
(2@1) angle |
637 | 768 |
" |
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
769 |
|
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
770 |
"Modified: 2.4.1997 / 00:02:17 / cg" |
637 | 771 |
! |
772 |
||
356 | 773 |
r |
774 |
"return the receiver's radius in a polar coordinate system. |
|
775 |
(i.e. the length of a vector from 0@0 to the receiver)" |
|
776 |
||
2578 | 777 |
^ ((x*x) + (y*y)) sqrt |
1 | 778 |
|
356 | 779 |
" |
2578 | 780 |
(1@1) r |
781 |
(2@1) r |
|
782 |
(2@0) r |
|
783 |
(0@2) r |
|
784 |
(-2@-2) r |
|
785 |
(2@2) r |
|
356 | 786 |
" |
1 | 787 |
! |
788 |
||
356 | 789 |
theta |
790 |
"return the receiver's angle (in radians) in a polar coordinate system. |
|
791 |
(i.e. the angle of a vector from 0@0 to the receiver)" |
|
1 | 792 |
|
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
793 |
|theta t| |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
794 |
|
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
795 |
x = 0 ifTrue:[ |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
796 |
y >= 0 ifTrue:[ |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
797 |
^ Float pi * 0.5 |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
798 |
]. |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
799 |
^ Float pi * 1.5. |
356 | 800 |
]. |
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
801 |
|
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
802 |
t := y asFloat / x asFloat. |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
803 |
theta := t arcTan. |
356 | 804 |
x < 0 ifTrue:[ |
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
805 |
^ theta +Float pi |
356 | 806 |
]. |
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
807 |
theta < 0 ifTrue:[ |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
808 |
^ theta + (Float pi * 2.0) |
356 | 809 |
]. |
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
810 |
^ theta. |
356 | 811 |
" |
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
812 |
(1@1) theta |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
813 |
(2@1) theta |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
814 |
(-2@1) theta |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
815 |
(-2@-1) theta |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
816 |
(0@-1) theta |
356 | 817 |
" |
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
818 |
|
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
819 |
"Modified: 2.4.1997 / 00:15:12 / cg" |
1 | 820 |
! ! |
356 | 821 |
|
637 | 822 |
!Point methodsFor:'printing & storing'! |
823 |
||
824 |
printOn:aStream |
|
825 |
"append a printed representation of the receiver to aStream" |
|
826 |
||
827 |
x printOn:aStream. |
|
828 |
aStream nextPut:$@. |
|
829 |
y printOn:aStream |
|
830 |
! |
|
831 |
||
832 |
storeOn:aStream |
|
833 |
"append my storeString to aStream" |
|
834 |
||
835 |
aStream nextPut:$(. |
|
836 |
x storeOn:aStream. |
|
837 |
aStream nextPut:$@. |
|
838 |
y storeOn:aStream. |
|
839 |
aStream nextPut:$) |
|
840 |
! ! |
|
841 |
||
842 |
!Point methodsFor:'queries'! |
|
843 |
||
844 |
isPoint |
|
845 |
"return true, if the receiver is some kind of point" |
|
846 |
||
847 |
^ true |
|
848 |
! ! |
|
849 |
||
850 |
!Point methodsFor:'transformations'! |
|
851 |
||
852 |
* scale |
|
853 |
"Return a new Point that is the product of the |
|
854 |
receiver and scale (which is a Point or Number)." |
|
855 |
||
856 |
|scalePoint| |
|
857 |
||
858 |
"speedup for common cases ..." |
|
859 |
||
860 |
(scale isMemberOf:Point) ifTrue:[ |
|
2273 | 861 |
^ Point x:(x * scale x) y:(y * scale y) |
637 | 862 |
]. |
863 |
(scale isMemberOf:SmallInteger) ifTrue:[ |
|
2273 | 864 |
^ Point x:(x * scale) y:(y * scale) |
637 | 865 |
]. |
866 |
scale isNumber ifTrue:[ |
|
2273 | 867 |
^ Point x:(x * scale) y:(y * scale) |
637 | 868 |
]. |
869 |
||
870 |
"this is the general (& clean) code ..." |
|
871 |
||
872 |
scalePoint := scale asPoint. |
|
873 |
^ (x * scalePoint x) @ (y * scalePoint y) |
|
2273 | 874 |
|
875 |
"Modified: 25.1.1997 / 17:28:11 / cg" |
|
637 | 876 |
! |
877 |
||
878 |
+ scale |
|
879 |
"Return a new Point that is the sum of the |
|
880 |
receiver and scale (which is a Point or Number)." |
|
881 |
||
882 |
|scalePoint| |
|
883 |
||
884 |
"speedup for common cases ..." |
|
885 |
||
886 |
(scale isMemberOf:Point) ifTrue:[ |
|
2273 | 887 |
^ Point x:(x + scale x) y:(y + scale y) |
637 | 888 |
]. |
889 |
(scale isMemberOf:SmallInteger) ifTrue:[ |
|
2273 | 890 |
"/ same as below, but stc can do better here |
891 |
^ Point x:(x + scale) y:(y + scale) |
|
637 | 892 |
]. |
893 |
scale isNumber ifTrue:[ |
|
2273 | 894 |
^ Point x:(x + scale) y:(y + scale) |
637 | 895 |
]. |
896 |
||
897 |
"this is the general (& clean) code ..." |
|
898 |
||
899 |
scalePoint := scale asPoint. |
|
900 |
^ (x + scalePoint x) @ (y + scalePoint y) |
|
2273 | 901 |
|
902 |
"Modified: 25.1.1997 / 17:27:46 / cg" |
|
637 | 903 |
! |
904 |
||
905 |
- scale |
|
906 |
"Return a new Point that is the difference of the |
|
907 |
receiver and scale (which is a Point or Number)." |
|
908 |
||
909 |
|scalePoint| |
|
910 |
||
911 |
"speedup for common cases ..." |
|
912 |
||
913 |
(scale isMemberOf:Point) ifTrue:[ |
|
2273 | 914 |
^ Point x:(x - scale x) y:(y - scale y) |
637 | 915 |
]. |
916 |
(scale isMemberOf:SmallInteger) ifTrue:[ |
|
2273 | 917 |
"/ same as below, but stc can do better here |
918 |
^ Point x:(x - scale) y:(y - scale) |
|
637 | 919 |
]. |
920 |
scale isNumber ifTrue:[ |
|
2273 | 921 |
^ Point x:(x - scale) y:(y - scale) |
637 | 922 |
]. |
923 |
||
924 |
"this is the general (& clean) code ..." |
|
925 |
||
926 |
scalePoint := scale asPoint. |
|
927 |
^ (x - scalePoint x) @ (y - scalePoint y) |
|
2273 | 928 |
|
929 |
"Modified: 25.1.1997 / 17:27:40 / cg" |
|
637 | 930 |
! |
931 |
||
932 |
/ scale |
|
933 |
"Return a new Point that is the integer quotient of the |
|
934 |
receiver and scale (which is a Point or Number)." |
|
935 |
||
936 |
|scalePoint| |
|
937 |
||
938 |
"speedup for common cases ..." |
|
939 |
||
940 |
(scale isMemberOf:Point) ifTrue:[ |
|
941 |
^ (x / scale x) @ (y / scale y) |
|
942 |
]. |
|
943 |
scale isNumber ifTrue:[ |
|
944 |
^ (x / scale) @ (y / scale) |
|
945 |
]. |
|
946 |
||
947 |
"this is the general (& clean) code ..." |
|
948 |
||
949 |
scalePoint := scale asPoint. |
|
950 |
^ (x / scalePoint x) @ (y / scalePoint y) |
|
951 |
! |
|
952 |
||
953 |
// scale |
|
954 |
"Return a new Point that is the quotient of the |
|
955 |
receiver and scale (which is a Point or Number)." |
|
956 |
||
957 |
|scalePoint| |
|
958 |
||
959 |
scalePoint := scale asPoint. |
|
960 |
^ (x // scalePoint x) @ (y // scalePoint y) |
|
961 |
! |
|
962 |
||
963 |
negated |
|
964 |
"return a new point with my coordinates negated |
|
965 |
i.e. the receiver mirrored at the origin" |
|
966 |
||
967 |
^ (x negated) @ (y negated) |
|
968 |
! |
|
969 |
||
970 |
reciprocal |
|
971 |
"return a new point where the coordinates are |
|
972 |
the reciproce of mine" |
|
973 |
||
974 |
^ (1 / x) @ (1 / y) |
|
975 |
! |
|
976 |
||
977 |
scaledBy:aScale |
|
978 |
"return a new Point that is the product of the |
|
979 |
receiver and scale (which is a Point or Number)." |
|
980 |
||
981 |
^ self * aScale |
|
982 |
! |
|
983 |
||
984 |
translatedBy:anOffset |
|
985 |
"return a new Point that is the sum of the |
|
986 |
receiver and scale (which is a Point or Number)." |
|
987 |
||
988 |
^ self + anOffset |
|
989 |
! ! |
|
990 |
||
2273 | 991 |
!Point class methodsFor:'documentation'! |
637 | 992 |
|
993 |
version |
|
4857 | 994 |
^ '$Header: /cvs/stx/stx/libbasic/Point.st,v 1.48 1999-10-06 18:53:33 cg Exp $' |
637 | 995 |
! ! |
356 | 996 |
Point initialize! |