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