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