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