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
|
|
14 |
instanceVariableNames:'x y'
|
|
15 |
classVariableNames:'PointZero PointOne'
|
|
16 |
poolDictionaries:''
|
308
|
17 |
category:'Graphics-Geometry'
|
1
|
18 |
!
|
|
19 |
|
|
20 |
Point comment:'
|
5
|
21 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
180
|
22 |
All Rights Reserved
|
93
|
23 |
|
345
|
24 |
$Header: /cvs/stx/stx/libbasic/Point.st,v 1.22 1995-05-16 17:08:22 claus Exp $
|
1
|
25 |
'!
|
|
26 |
|
77
|
27 |
!Point class methodsFor:'documentation'!
|
|
28 |
|
88
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
180
|
32 |
All Rights Reserved
|
88
|
33 |
|
|
34 |
This software is furnished under a license and may be used
|
|
35 |
only in accordance with the terms of that license and with the
|
|
36 |
inclusion of the above copyright notice. This software may not
|
|
37 |
be provided or otherwise made available to, or used by, any
|
|
38 |
other person. No title to or ownership of the software is
|
|
39 |
hereby transferred.
|
|
40 |
"
|
|
41 |
!
|
|
42 |
|
|
43 |
version
|
|
44 |
"
|
345
|
45 |
$Header: /cvs/stx/stx/libbasic/Point.st,v 1.22 1995-05-16 17:08:22 claus Exp $
|
88
|
46 |
"
|
|
47 |
!
|
|
48 |
|
77
|
49 |
documentation
|
|
50 |
"
|
|
51 |
I represent a point in 2D space. Or I can be used to represent
|
|
52 |
an extent (of a rectangle, for example), in which case my x-coordinate
|
|
53 |
represents the width, and y-coordinate the height of something.
|
|
54 |
|
|
55 |
The x and y coordinate are usually numbers.
|
|
56 |
|
|
57 |
Instance variables:
|
|
58 |
|
180
|
59 |
x <Number> the x-coordinate of myself
|
|
60 |
y <Number> the y-coordinate of myself
|
77
|
61 |
"
|
|
62 |
! !
|
|
63 |
|
1
|
64 |
!Point class methodsFor:'initialization'!
|
|
65 |
|
|
66 |
initialize
|
302
|
67 |
PointZero isNil ifTrue:[
|
|
68 |
PointZero := 0 @ 0.
|
|
69 |
PointOne := 1 @ 1
|
|
70 |
]
|
1
|
71 |
! !
|
|
72 |
|
|
73 |
!Point class methodsFor:'constants'!
|
|
74 |
|
|
75 |
zero
|
44
|
76 |
"return the neutral element for addition"
|
|
77 |
|
1
|
78 |
^ PointZero
|
|
79 |
!
|
|
80 |
|
|
81 |
unity
|
44
|
82 |
"return the neutral element for multiplication"
|
|
83 |
|
1
|
84 |
^ PointOne
|
|
85 |
! !
|
|
86 |
|
3
|
87 |
!Point class methodsFor:'queries'!
|
|
88 |
|
|
89 |
isBuiltInClass
|
|
90 |
"this class is known by the run-time-system"
|
|
91 |
|
|
92 |
^ self == Point
|
|
93 |
! !
|
|
94 |
|
1
|
95 |
!Point class methodsFor:'instance creation'!
|
|
96 |
|
|
97 |
x:newX y:newY
|
|
98 |
"create and return a new point with coordinates newX and newY"
|
|
99 |
|
|
100 |
%{ /* NOCONTEXT */
|
|
101 |
|
77
|
102 |
/*
|
|
103 |
* claus: I am no longer certain, if this primitive is worth the effort
|
|
104 |
*/
|
325
|
105 |
if (_CanDoQuickNew(sizeof(struct __point))) {
|
308
|
106 |
if (self == Point) {
|
180
|
107 |
OBJ newPoint;
|
293
|
108 |
int spc;
|
1
|
109 |
|
325
|
110 |
_qCheckedAlignedNew(newPoint, sizeof(struct __point));
|
180
|
111 |
_InstPtr(newPoint)->o_class = Point;
|
|
112 |
_PointInstPtr(newPoint)->p_x = newX;
|
|
113 |
_PointInstPtr(newPoint)->p_y = newY;
|
308
|
114 |
if (! __bothSmallInteger(newX, newY)) {
|
325
|
115 |
spc = __qSpace(newPoint);
|
308
|
116 |
__STORE_SPC(newPoint, newX, spc);
|
|
117 |
__STORE_SPC(newPoint, newY, spc);
|
|
118 |
}
|
180
|
119 |
RETURN ( newPoint );
|
|
120 |
}
|
1
|
121 |
}
|
|
122 |
%}
|
|
123 |
.
|
|
124 |
^ (self basicNew) x:newX y:newY
|
|
125 |
!
|
|
126 |
|
202
|
127 |
readFrom:aStream onError:exceptionBlock
|
1
|
128 |
"return the next Point from the (character-)stream aStream;
|
202
|
129 |
skipping all whitespace first; return the value of exceptionBlock,
|
|
130 |
if no point can be read."
|
1
|
131 |
|
|
132 |
|newX newY|
|
|
133 |
|
345
|
134 |
newX := Number readFrom:aStream onError:nil.
|
|
135 |
newX notNil ifTrue:[
|
|
136 |
(aStream skipSeparators == $@) ifTrue:[
|
|
137 |
aStream next.
|
|
138 |
newY := Number readFrom:aStream onError:nil.
|
|
139 |
newY notNil ifTrue:[
|
|
140 |
^ (self basicNew) x:newX y:newY
|
|
141 |
]
|
|
142 |
]
|
|
143 |
].
|
|
144 |
^ exceptionBlock value
|
77
|
145 |
|
202
|
146 |
"
|
|
147 |
Point readFrom:('1.234 @ 5.678' readStream)
|
345
|
148 |
Point readFrom:('1' readStream)
|
|
149 |
Point readFrom:('1' readStream) onError:[1@1]
|
202
|
150 |
Point readFrom:('fooBar' readStream) onError:[0@0]
|
|
151 |
"
|
1
|
152 |
! !
|
|
153 |
|
|
154 |
!Point methodsFor:'accessing'!
|
|
155 |
|
|
156 |
x
|
|
157 |
"return the x coordinate"
|
|
158 |
|
77
|
159 |
^ x
|
1
|
160 |
!
|
|
161 |
|
|
162 |
y
|
|
163 |
"return the y coordinate"
|
|
164 |
|
77
|
165 |
^ y
|
1
|
166 |
!
|
|
167 |
|
|
168 |
x:newX
|
77
|
169 |
"set the x coordinate to be the argument, aNumber.
|
180
|
170 |
This is destructive (modifies the receiver, not a copy) and
|
|
171 |
should only be used if you know, that you are the exclusive owner
|
|
172 |
of the receiver."
|
1
|
173 |
|
|
174 |
x := newX
|
|
175 |
!
|
|
176 |
|
|
177 |
y:newY
|
77
|
178 |
"set the y coordinate to be the argument, aNumber.
|
180
|
179 |
This is destructive (modifies the receiver, not a copy) and
|
|
180 |
should only be used if you know, that you are the exclusive owner
|
|
181 |
of the receiver."
|
1
|
182 |
|
|
183 |
y := newY
|
|
184 |
!
|
|
185 |
|
|
186 |
x:newX y:newY
|
77
|
187 |
"set both the x and y coordinates.
|
180
|
188 |
This is destructive (modifies the receiver, not a copy) and
|
|
189 |
should only be used if you know, that you are the exclusive owner
|
|
190 |
of the receiver."
|
1
|
191 |
|
|
192 |
x := newX.
|
|
193 |
y := newY
|
|
194 |
! !
|
|
195 |
|
|
196 |
!Point methodsFor:'comparing'!
|
|
197 |
|
|
198 |
hash
|
|
199 |
"return a number for hashing"
|
|
200 |
|
217
|
201 |
x = y ifTrue:[^ x hash].
|
|
202 |
"
|
|
203 |
used to be:
|
1
|
204 |
^ (x hash) bitXor:(y hash)
|
217
|
205 |
the following handles 1@x vs. x@1 better:
|
|
206 |
"
|
|
207 |
^ (x hash) bitXor:(y hash bitShift:12)
|
1
|
208 |
!
|
|
209 |
|
|
210 |
< aPoint
|
|
211 |
"return true if the receiver is above and to the left
|
|
212 |
of the argument, aPoint"
|
|
213 |
|
|
214 |
|p|
|
|
215 |
|
|
216 |
p := aPoint asPoint.
|
|
217 |
x >= (p x) ifTrue:[^ false].
|
|
218 |
y >= (p y) ifTrue:[^ false].
|
|
219 |
^ true
|
|
220 |
!
|
|
221 |
|
|
222 |
> aPoint
|
|
223 |
"return true if the receiver is below and to the right
|
|
224 |
of the argument, aPoint"
|
|
225 |
|
|
226 |
|p|
|
|
227 |
|
|
228 |
p := aPoint asPoint.
|
|
229 |
x <= (p x) ifTrue:[^ false].
|
|
230 |
y <= (p y) ifTrue:[^ false].
|
|
231 |
^ true
|
|
232 |
!
|
|
233 |
|
|
234 |
= aPoint
|
|
235 |
"return true if the receiver represents the same point as
|
|
236 |
the argument, aPoint"
|
|
237 |
|
|
238 |
|p|
|
|
239 |
|
77
|
240 |
(aPoint isMemberOf:Point) ifTrue:[ "this is a hint to STC"
|
217
|
241 |
x ~= (aPoint x) ifTrue:[^ false].
|
|
242 |
y ~= (aPoint y) ifTrue:[^ false].
|
180
|
243 |
^ true
|
1
|
244 |
].
|
|
245 |
aPoint respondsToArithmetic ifFalse:[ ^ false].
|
|
246 |
p := aPoint asPoint.
|
217
|
247 |
x ~= (p x) ifTrue:[^ false].
|
|
248 |
y ~= (p y) ifTrue:[^ false].
|
1
|
249 |
^ true
|
|
250 |
!
|
|
251 |
|
|
252 |
max:aPoint
|
|
253 |
"return the lower right corner of the rectangle uniquely defined by
|
|
254 |
the receiver and the argument, aPoint"
|
|
255 |
|
|
256 |
|p maxX maxY|
|
|
257 |
|
|
258 |
p := aPoint asPoint.
|
|
259 |
maxX := x max:(p x).
|
|
260 |
maxY := y max:(p y).
|
|
261 |
^ maxX @ maxY
|
|
262 |
!
|
|
263 |
|
|
264 |
min:aPoint
|
|
265 |
"return the upper left corner of the rectangle uniquely defined by
|
|
266 |
the receiver and the argument, aPoint"
|
|
267 |
|
|
268 |
|p minX minY|
|
|
269 |
|
|
270 |
p := aPoint asPoint.
|
|
271 |
minX := x min:(p x).
|
|
272 |
minY := y min:(p y).
|
|
273 |
^ minX @ minY
|
|
274 |
! !
|
|
275 |
|
213
|
276 |
!Point methodsFor:'coercing & converting'!
|
1
|
277 |
|
|
278 |
generality
|
40
|
279 |
"return the generality value - see ArithmeticValue>>retry:coercing:"
|
|
280 |
|
1
|
281 |
^ 120
|
|
282 |
!
|
|
283 |
|
|
284 |
coerce:anObject
|
40
|
285 |
"return aNumber converted into receivers type"
|
|
286 |
|
1
|
287 |
^ anObject asPoint
|
|
288 |
!
|
|
289 |
|
|
290 |
asPoint
|
|
291 |
"return the receiver as Point - this is the receiver"
|
|
292 |
|
|
293 |
^ self
|
|
294 |
!
|
|
295 |
|
|
296 |
asRectangle
|
|
297 |
"return a zero-width rectangle consisting of origin
|
57
|
298 |
and corner being the receiver"
|
1
|
299 |
|
|
300 |
^ self corner:self
|
339
|
301 |
!
|
|
302 |
|
|
303 |
asLayout
|
|
304 |
"return a LayoutOrigin from the receiver.
|
|
305 |
If the receiver coordinates are between 0 and 1, take
|
|
306 |
them as fractional parts (relative to superview).
|
|
307 |
Otherwise, treat them as absolute offsets.
|
|
308 |
Notice: in 10.5.x LayoutOrigin is not yet released."
|
|
309 |
|
|
310 |
^ LayoutOrigin fromPoint:self
|
|
311 |
!
|
|
312 |
|
|
313 |
asFractionalLayout
|
|
314 |
"return a LayoutOrigin from the receiver,
|
|
315 |
treating the receiver coordinates as fractional parts
|
|
316 |
(i.e. relative to superview).
|
|
317 |
Notice: in 10.5.x LayoutOrigin is not yet released."
|
|
318 |
|
|
319 |
^ LayoutOrigin fractionalFromPoint:self
|
|
320 |
!
|
|
321 |
|
|
322 |
asOffsetLayout
|
|
323 |
"return a LayoutOrigin from the receiver,
|
|
324 |
treating the receiver coordinates as absolute offsets.
|
|
325 |
Notice: in 10.5.x LayoutOrigin is not yet released."
|
|
326 |
|
|
327 |
^ LayoutOrigin offsetFromPoint:self
|
1
|
328 |
! !
|
|
329 |
|
|
330 |
!Point methodsFor:'creating rectangles'!
|
|
331 |
|
|
332 |
corner:aPoint
|
|
333 |
"return a rectangle whose origin is self and corner is aPoint"
|
|
334 |
|
|
335 |
^ Rectangle origin:self corner:aPoint
|
|
336 |
!
|
|
337 |
|
|
338 |
extent:aPoint
|
|
339 |
"return a rectangle whose origin is self and extent is aPoint"
|
|
340 |
|
|
341 |
^ Rectangle origin:self extent:aPoint
|
|
342 |
! !
|
|
343 |
|
|
344 |
!Point methodsFor:'transformations'!
|
|
345 |
|
|
346 |
+ scale
|
|
347 |
"Return a new Point that is the sum of the
|
|
348 |
receiver and scale (which is a Point or Number)."
|
|
349 |
|
|
350 |
|scalePoint|
|
|
351 |
|
77
|
352 |
"speedup for common cases ..."
|
|
353 |
|
|
354 |
(scale isMemberOf:Point) ifTrue:[
|
180
|
355 |
^ (x + scale x) @ (y + scale y)
|
77
|
356 |
].
|
325
|
357 |
(scale isMemberOf:SmallInteger) ifTrue:[
|
|
358 |
^ (x + scale) @ (y + scale)
|
|
359 |
].
|
77
|
360 |
scale isNumber ifTrue:[
|
180
|
361 |
^ (x + scale) @ (y + scale)
|
77
|
362 |
].
|
|
363 |
|
|
364 |
"this is the general (& clean) code ..."
|
|
365 |
|
1
|
366 |
scalePoint := scale asPoint.
|
|
367 |
^ (x + scalePoint x) @ (y + scalePoint y)
|
|
368 |
!
|
|
369 |
|
|
370 |
- scale
|
|
371 |
"Return a new Point that is the difference of the
|
|
372 |
receiver and scale (which is a Point or Number)."
|
|
373 |
|
|
374 |
|scalePoint|
|
|
375 |
|
77
|
376 |
"speedup for common cases ..."
|
|
377 |
|
|
378 |
(scale isMemberOf:Point) ifTrue:[
|
180
|
379 |
^ (x - scale x) @ (y - scale y)
|
77
|
380 |
].
|
325
|
381 |
(scale isMemberOf:SmallInteger) ifTrue:[
|
|
382 |
^ (x - scale) @ (y - scale)
|
|
383 |
].
|
77
|
384 |
scale isNumber ifTrue:[
|
180
|
385 |
^ (x - scale) @ (y - scale)
|
77
|
386 |
].
|
|
387 |
|
|
388 |
"this is the general (& clean) code ..."
|
|
389 |
|
1
|
390 |
scalePoint := scale asPoint.
|
|
391 |
^ (x - scalePoint x) @ (y - scalePoint y)
|
|
392 |
!
|
|
393 |
|
|
394 |
* scale
|
|
395 |
"Return a new Point that is the product of the
|
|
396 |
receiver and scale (which is a Point or Number)."
|
|
397 |
|
|
398 |
|scalePoint|
|
|
399 |
|
77
|
400 |
"speedup for common cases ..."
|
|
401 |
|
|
402 |
(scale isMemberOf:Point) ifTrue:[
|
180
|
403 |
^ (x * scale x) @ (y * scale y)
|
77
|
404 |
].
|
325
|
405 |
(scale isMemberOf:SmallInteger) ifTrue:[
|
|
406 |
^ (x * scale) @ (y * scale)
|
|
407 |
].
|
77
|
408 |
scale isNumber ifTrue:[
|
180
|
409 |
^ (x * scale) @ (y * scale)
|
77
|
410 |
].
|
|
411 |
|
|
412 |
"this is the general (& clean) code ..."
|
|
413 |
|
1
|
414 |
scalePoint := scale asPoint.
|
|
415 |
^ (x * scalePoint x) @ (y * scalePoint y)
|
|
416 |
!
|
|
417 |
|
|
418 |
/ scale
|
|
419 |
"Return a new Point that is the integer quotient of the
|
|
420 |
receiver and scale (which is a Point or Number)."
|
|
421 |
|
|
422 |
|scalePoint|
|
|
423 |
|
77
|
424 |
"speedup for common cases ..."
|
|
425 |
|
|
426 |
(scale isMemberOf:Point) ifTrue:[
|
180
|
427 |
^ (x / scale x) @ (y / scale y)
|
77
|
428 |
].
|
|
429 |
scale isNumber ifTrue:[
|
180
|
430 |
^ (x / scale) @ (y / scale)
|
77
|
431 |
].
|
|
432 |
|
|
433 |
"this is the general (& clean) code ..."
|
|
434 |
|
1
|
435 |
scalePoint := scale asPoint.
|
|
436 |
^ (x / scalePoint x) @ (y / scalePoint y)
|
|
437 |
!
|
|
438 |
|
|
439 |
// scale
|
|
440 |
"Return a new Point that is the quotient of the
|
|
441 |
receiver and scale (which is a Point or Number)."
|
|
442 |
|
|
443 |
|scalePoint|
|
|
444 |
|
|
445 |
scalePoint := scale asPoint.
|
|
446 |
^ (x // scalePoint x) @ (y // scalePoint y)
|
|
447 |
!
|
|
448 |
|
|
449 |
reciprocal
|
|
450 |
"return a new point where the coordinates are
|
|
451 |
the reciproce of mine"
|
|
452 |
|
|
453 |
^ (1 / x) @ (1 / y)
|
|
454 |
!
|
|
455 |
|
|
456 |
negated
|
77
|
457 |
"return a new point with my coordinates negated
|
|
458 |
i.e. the receiver mirrored at the origin"
|
1
|
459 |
|
|
460 |
^ (x negated) @ (y negated)
|
3
|
461 |
!
|
|
462 |
|
|
463 |
scaledBy:aScale
|
|
464 |
"return a new Point that is the product of the
|
1
|
465 |
receiver and scale (which is a Point or Number)."
|
|
466 |
|
|
467 |
^ self * aScale
|
|
468 |
!
|
|
469 |
|
3
|
470 |
translatedBy:anOffset
|
|
471 |
"return a new Point that is the sum of the
|
1
|
472 |
receiver and scale (which is a Point or Number)."
|
|
473 |
|
|
474 |
^ self + anOffset
|
|
475 |
! !
|
|
476 |
|
77
|
477 |
!Point methodsFor:'destructive transformations'!
|
|
478 |
|
|
479 |
scaleBy:aScale
|
|
480 |
"scale the receiver, by replacing coordinates by the product
|
180
|
481 |
of the receivers coordinates and the scale (a Point or Number).
|
|
482 |
This is destructive (modifies the receiver, not a copy) and
|
|
483 |
should only be used if you know, that you are the exclusive owner
|
|
484 |
of the receiver."
|
77
|
485 |
|
|
486 |
|scalePoint|
|
|
487 |
|
180
|
488 |
(aScale isMemberOf:Point) ifTrue:[ "type hint to stc"
|
|
489 |
x := x * aScale x.
|
|
490 |
y := y * aScale y.
|
|
491 |
^ self
|
77
|
492 |
].
|
|
493 |
aScale isNumber ifTrue:[
|
180
|
494 |
x := x * aScale.
|
|
495 |
y := y * aScale.
|
|
496 |
^ self
|
77
|
497 |
].
|
|
498 |
|
|
499 |
"this is the general (& clean) code ..."
|
|
500 |
|
|
501 |
scalePoint := aScale asPoint.
|
|
502 |
x := x * scalePoint x.
|
|
503 |
y := y * scalePoint y
|
|
504 |
!
|
|
505 |
|
|
506 |
translateBy:anOffset
|
|
507 |
"translate the receiver, by replacing coordinates by the sum
|
180
|
508 |
of the receivers coordinated and the scale (a Point or Number).
|
|
509 |
This is destructive (modifies the receiver, not a copy) and
|
|
510 |
should only be used if you know, that you are the exclusive owner
|
|
511 |
of the receiver."
|
77
|
512 |
|
|
513 |
|offsetPoint|
|
|
514 |
|
180
|
515 |
(anOffset isMemberOf:Point) ifTrue:[ "type hint to stc"
|
|
516 |
x := x + anOffset x.
|
|
517 |
y := y + anOffset y.
|
|
518 |
^ self
|
77
|
519 |
].
|
|
520 |
anOffset isNumber ifTrue:[
|
180
|
521 |
x := x + anOffset.
|
|
522 |
y := y + anOffset.
|
|
523 |
^ self
|
77
|
524 |
].
|
|
525 |
|
|
526 |
"this is the general (& clean) code ..."
|
|
527 |
|
|
528 |
offsetPoint := anOffset asPoint.
|
|
529 |
x := x + anOffset x.
|
|
530 |
y := y + anOffset y
|
|
531 |
! !
|
|
532 |
|
|
533 |
!Point methodsFor:'queries'!
|
|
534 |
|
|
535 |
isPoint
|
|
536 |
"return true, if the receiver is some kind of point"
|
|
537 |
|
|
538 |
^ true
|
|
539 |
! !
|
|
540 |
|
1
|
541 |
!Point methodsFor:'misc'!
|
|
542 |
|
|
543 |
dist:aPoint
|
|
544 |
"return the distance between aPoint and the receiver."
|
|
545 |
|
|
546 |
^ (aPoint - self) r
|
|
547 |
!
|
|
548 |
|
|
549 |
dotProduct:aPoint
|
57
|
550 |
"return a number that is the dot product of the receiver and
|
1
|
551 |
the argument, aPoint. That is, the two points are
|
57
|
552 |
multiplied and the coordinates of the result summed."
|
1
|
553 |
|
|
554 |
|temp|
|
|
555 |
|
|
556 |
temp := self * aPoint.
|
|
557 |
^ temp x abs + temp y abs
|
|
558 |
!
|
|
559 |
|
|
560 |
r
|
217
|
561 |
"return the receiver's radius in a polar coordinate system.
|
|
562 |
(i.e. the length of a vector from 0@0 to the receiver)"
|
1
|
563 |
|
|
564 |
^ (self dotProduct:self) sqrt
|
217
|
565 |
|
|
566 |
"
|
|
567 |
(1@1) r
|
|
568 |
(2@1) r
|
|
569 |
(2@0) r
|
|
570 |
"
|
|
571 |
!
|
|
572 |
|
|
573 |
angle
|
|
574 |
"return the receiver's angle in a polar coordinate system.
|
|
575 |
(i.e. the angle of a vector from 0@0 to the receiver)"
|
|
576 |
|
|
577 |
y < 0 ifTrue:[
|
|
578 |
x < 0 ifTrue:[
|
|
579 |
^ 270 degreesToRadians - (y / x) arcTan
|
|
580 |
].
|
|
581 |
x = 0 ifTrue:[
|
|
582 |
^ 180
|
|
583 |
].
|
|
584 |
^ 360 degreesToRadians - (y abs / x) arcTan
|
|
585 |
].
|
|
586 |
x < 0 ifTrue:[
|
|
587 |
^ 180 degreesToRadians - (y / x abs) arcTan
|
|
588 |
].
|
|
589 |
x = 0 ifTrue:[
|
|
590 |
^ 0
|
|
591 |
].
|
|
592 |
^ (y / x) arcTan
|
|
593 |
|
|
594 |
"
|
|
595 |
(1@1) angle radiansToDegrees
|
|
596 |
(2@1) angle radiansToDegrees
|
|
597 |
"
|
1
|
598 |
!
|
|
599 |
|
|
600 |
abs
|
77
|
601 |
"return a new point with my coordinates taken from the absolute values."
|
1
|
602 |
|
|
603 |
^ (x abs) @ (y abs)
|
|
604 |
!
|
|
605 |
|
|
606 |
truncated
|
57
|
607 |
"return a new point with my coordinates truncated as integer or the
|
|
608 |
receiver, if already truncated."
|
1
|
609 |
|
57
|
610 |
(x isInteger and:[y isInteger]) ifTrue:[^ self].
|
1
|
611 |
^ (x truncated) @ (y truncated)
|
|
612 |
!
|
|
613 |
|
|
614 |
rounded
|
|
615 |
"return a new point with my coordinates rounded to the next integer
|
57
|
616 |
coordinated (use for gridding) or the receiver of already rounded."
|
1
|
617 |
|
57
|
618 |
(x isInteger and:[y isInteger]) ifTrue:[^ self].
|
1
|
619 |
^ (x rounded) @ (y rounded)
|
|
620 |
!
|
|
621 |
|
|
622 |
grid:gridPoint
|
|
623 |
"return a new point with coordinates grided (i.e. rounded to the
|
|
624 |
nearest point on the grid)"
|
|
625 |
|
|
626 |
|newX newY gridX gridY|
|
|
627 |
|
|
628 |
gridX := gridPoint x.
|
|
629 |
(gridX <= 1) ifTrue:[
|
180
|
630 |
newX := x asInteger
|
1
|
631 |
] ifFalse:[
|
180
|
632 |
newX := ((x + (gridX // 2)) // gridX) * gridX
|
1
|
633 |
].
|
|
634 |
gridY := gridPoint y.
|
|
635 |
(gridY <= 1) ifTrue:[
|
180
|
636 |
newY := y asInteger
|
1
|
637 |
] ifFalse:[
|
180
|
638 |
newY := ((y + (gridY // 2)) // gridY) * gridY
|
1
|
639 |
].
|
|
640 |
^ newX @ newY
|
77
|
641 |
!
|
|
642 |
|
|
643 |
quadrantContaining:aPoint
|
|
644 |
"return the number of the quadrant containing aPoint placing
|
|
645 |
the receiver at the origin, where the quadrants are numbered as
|
|
646 |
follows:
|
180
|
647 |
^ 2 | 3
|
|
648 |
Y ------
|
|
649 |
1 | 0
|
77
|
650 |
|
180
|
651 |
X >
|
77
|
652 |
This can be used for polygon operations (see Foley for examples).
|
|
653 |
"
|
|
654 |
|
|
655 |
aPoint x > x ifTrue:[
|
180
|
656 |
aPoint y >= y ifTrue:[^ 3].
|
|
657 |
^ 0
|
77
|
658 |
].
|
|
659 |
aPoint y >= y ifTrue: [^ 2].
|
|
660 |
^ 1
|
|
661 |
|
|
662 |
"
|
|
663 |
(10 @ 10) quadrantContaining:(15 @ 15)
|
|
664 |
(10 @ 10) quadrantContaining:(5 @ 5)
|
|
665 |
(10 @ 10) quadrantContaining:(5 @ 15)
|
|
666 |
(10 @ 10) quadrantContaining:(15 @ 5)
|
|
667 |
"
|
217
|
668 |
!
|
|
669 |
|
|
670 |
quadrant
|
|
671 |
"return the number of the quadrant containing the receiver.
|
|
672 |
quadrants are named as follows:
|
|
673 |
|
|
674 |
^ 2 | 3
|
|
675 |
Y ------
|
|
676 |
1 | 0
|
|
677 |
|
|
678 |
X >
|
|
679 |
"
|
|
680 |
|
|
681 |
^ 0@0 quadrantContaining:self
|
276
|
682 |
!
|
|
683 |
|
|
684 |
nearestIntegerPointOnLineFrom: point1 to: point2
|
|
685 |
"return the closest integer point to the receiver on the line
|
|
686 |
determined by (point1, point2)--much faster than the more
|
|
687 |
accurate version if the receiver and arguments are integer points.
|
|
688 |
This method was found in the Manchester goody library."
|
|
689 |
|
|
690 |
| dX dY newX newY dX2 dY2 intersect scale coeff |
|
|
691 |
|
|
692 |
dX := point2 x - point1 x.
|
|
693 |
dY := point2 y - point1 y.
|
|
694 |
(dX = 0)ifTrue: [
|
|
695 |
(dY = 0) ifTrue: [
|
|
696 |
intersect := point1
|
|
697 |
] ifFalse: [
|
|
698 |
newX := point1 x.
|
|
699 |
scale := (y - point1 y) / dY.
|
|
700 |
newY := scale > 1 ifTrue: [
|
|
701 |
point2 y
|
|
702 |
] ifFalse: [
|
|
703 |
scale < 0 ifTrue: [
|
|
704 |
point1 y
|
|
705 |
] ifFalse: [
|
|
706 |
y
|
|
707 |
]
|
|
708 |
].
|
|
709 |
|
|
710 |
^ (newX @ newY) rounded
|
|
711 |
]
|
|
712 |
] ifFalse: [
|
|
713 |
(dY = 0) ifTrue: [
|
|
714 |
intersect := x @ point1 y
|
|
715 |
] ifFalse:[
|
|
716 |
dX2 := dX * dX.
|
|
717 |
dY2 := dY * dY.
|
|
718 |
coeff := ((dX * (y - point1 y)) -
|
|
719 |
((x - point1 x) * dY)) / (dX2 + dY2).
|
|
720 |
newX := x + (dY * coeff).
|
|
721 |
newY := y - (dX * coeff).
|
|
722 |
intersect := newX @ newY
|
|
723 |
]
|
|
724 |
].
|
|
725 |
|
|
726 |
scale := (intersect x - point1 x) / dX.
|
|
727 |
|
|
728 |
^ (scale > 1 ifTrue: [point2] ifFalse: [
|
|
729 |
scale < 0 ifTrue: [point1] ifFalse: [intersect]]) rounded
|
|
730 |
|
|
731 |
"
|
|
732 |
120@40 nearestIntegerPointOnLineFrom: 30@120 to: 100@120
|
|
733 |
0@0 nearestIntegerPointOnLineFrom: 10@10 to: 100@100
|
|
734 |
"
|
1
|
735 |
! !
|
|
736 |
|
|
737 |
!Point methodsFor:'printing & storing'!
|
|
738 |
|
57
|
739 |
printOn:aStream
|
|
740 |
"append a printed representation of the receiver to aStream"
|
1
|
741 |
|
57
|
742 |
x printOn:aStream.
|
|
743 |
aStream nextPut:$@.
|
|
744 |
y printOn:aStream
|
1
|
745 |
!
|
|
746 |
|
57
|
747 |
storeOn:aStream
|
|
748 |
"append my storeString to aStream"
|
1
|
749 |
|
57
|
750 |
aStream nextPut:$(.
|
|
751 |
x storeOn:aStream.
|
|
752 |
aStream nextPut:$@.
|
|
753 |
y storeOn:aStream.
|
|
754 |
aStream nextPut:$)
|
1
|
755 |
! !
|