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