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:''
|
|
17 |
category:'Graphics-Primitives'
|
|
18 |
!
|
|
19 |
|
|
20 |
Point comment:'
|
5
|
21 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
180
|
22 |
All Rights Reserved
|
93
|
23 |
|
217
|
24 |
$Header: /cvs/stx/stx/libbasic/Point.st,v 1.15 1995-02-02 12:25:30 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 |
"
|
217
|
45 |
$Header: /cvs/stx/stx/libbasic/Point.st,v 1.15 1995-02-02 12:25:30 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
|
|
67 |
PointZero := 0 @ 0.
|
|
68 |
PointOne := 1 @ 1
|
|
69 |
! !
|
|
70 |
|
|
71 |
!Point class methodsFor:'constants'!
|
|
72 |
|
|
73 |
zero
|
44
|
74 |
"return the neutral element for addition"
|
|
75 |
|
1
|
76 |
^ PointZero
|
|
77 |
!
|
|
78 |
|
|
79 |
unity
|
44
|
80 |
"return the neutral element for multiplication"
|
|
81 |
|
1
|
82 |
^ PointOne
|
|
83 |
! !
|
|
84 |
|
3
|
85 |
!Point class methodsFor:'queries'!
|
|
86 |
|
|
87 |
isBuiltInClass
|
|
88 |
"this class is known by the run-time-system"
|
|
89 |
|
|
90 |
^ self == Point
|
|
91 |
! !
|
|
92 |
|
1
|
93 |
!Point class methodsFor:'instance creation'!
|
|
94 |
|
|
95 |
x:newX y:newY
|
|
96 |
"create and return a new point with coordinates newX and newY"
|
|
97 |
|
|
98 |
%{ /* NOCONTEXT */
|
|
99 |
|
77
|
100 |
/*
|
|
101 |
* claus: I am no longer certain, if this primitive is worth the effort
|
|
102 |
*/
|
1
|
103 |
if (self == Point) {
|
180
|
104 |
if (_CanDoQuickNew(sizeof(struct point))) {
|
|
105 |
OBJ newPoint;
|
1
|
106 |
|
180
|
107 |
_qCheckedAlignedNew(newPoint, sizeof(struct point));
|
|
108 |
_InstPtr(newPoint)->o_class = Point;
|
|
109 |
_PointInstPtr(newPoint)->p_x = newX;
|
|
110 |
_PointInstPtr(newPoint)->p_y = newY;
|
|
111 |
__STORE(newPoint, newX);
|
|
112 |
__STORE(newPoint, newY);
|
|
113 |
RETURN ( newPoint );
|
|
114 |
}
|
1
|
115 |
}
|
|
116 |
%}
|
|
117 |
.
|
|
118 |
^ (self basicNew) x:newX y:newY
|
|
119 |
!
|
|
120 |
|
202
|
121 |
readFrom:aStream onError:exceptionBlock
|
1
|
122 |
"return the next Point from the (character-)stream aStream;
|
202
|
123 |
skipping all whitespace first; return the value of exceptionBlock,
|
|
124 |
if no point can be read."
|
1
|
125 |
|
|
126 |
|newX newY|
|
|
127 |
|
202
|
128 |
newX := Number readFrom:aStream onError:[^exceptionBlock value].
|
1
|
129 |
newX isNil ifTrue:[^ nil].
|
|
130 |
(aStream skipSeparators ~~ $@) ifTrue:[^nil].
|
|
131 |
aStream next.
|
202
|
132 |
newY := Number readFrom:aStream onError:[^exceptionBlock value].
|
1
|
133 |
newY isNil ifTrue:[^ nil].
|
|
134 |
^ (self basicNew) x:newX y:newY
|
77
|
135 |
|
202
|
136 |
"
|
|
137 |
Point readFrom:('1.234 @ 5.678' readStream)
|
|
138 |
Point readFrom:('fooBar' readStream) onError:[0@0]
|
|
139 |
"
|
1
|
140 |
! !
|
|
141 |
|
|
142 |
!Point methodsFor:'accessing'!
|
|
143 |
|
|
144 |
x
|
|
145 |
"return the x coordinate"
|
|
146 |
|
77
|
147 |
^ x
|
1
|
148 |
!
|
|
149 |
|
|
150 |
y
|
|
151 |
"return the y coordinate"
|
|
152 |
|
77
|
153 |
^ y
|
1
|
154 |
!
|
|
155 |
|
|
156 |
x:newX
|
77
|
157 |
"set the x coordinate to be the argument, aNumber.
|
180
|
158 |
This is destructive (modifies the receiver, not a copy) and
|
|
159 |
should only be used if you know, that you are the exclusive owner
|
|
160 |
of the receiver."
|
1
|
161 |
|
|
162 |
x := newX
|
|
163 |
!
|
|
164 |
|
|
165 |
y:newY
|
77
|
166 |
"set the y coordinate to be the argument, aNumber.
|
180
|
167 |
This is destructive (modifies the receiver, not a copy) and
|
|
168 |
should only be used if you know, that you are the exclusive owner
|
|
169 |
of the receiver."
|
1
|
170 |
|
|
171 |
y := newY
|
|
172 |
!
|
|
173 |
|
|
174 |
x:newX y:newY
|
77
|
175 |
"set both the x and y coordinates.
|
180
|
176 |
This is destructive (modifies the receiver, not a copy) and
|
|
177 |
should only be used if you know, that you are the exclusive owner
|
|
178 |
of the receiver."
|
1
|
179 |
|
|
180 |
x := newX.
|
|
181 |
y := newY
|
|
182 |
! !
|
|
183 |
|
|
184 |
!Point methodsFor:'comparing'!
|
|
185 |
|
|
186 |
hash
|
|
187 |
"return a number for hashing"
|
|
188 |
|
217
|
189 |
x = y ifTrue:[^ x hash].
|
|
190 |
"
|
|
191 |
used to be:
|
1
|
192 |
^ (x hash) bitXor:(y hash)
|
217
|
193 |
the following handles 1@x vs. x@1 better:
|
|
194 |
"
|
|
195 |
^ (x hash) bitXor:(y hash bitShift:12)
|
1
|
196 |
!
|
|
197 |
|
|
198 |
< aPoint
|
|
199 |
"return true if the receiver is above and to the left
|
|
200 |
of the argument, aPoint"
|
|
201 |
|
|
202 |
|p|
|
|
203 |
|
|
204 |
p := aPoint asPoint.
|
|
205 |
x >= (p x) ifTrue:[^ false].
|
|
206 |
y >= (p y) ifTrue:[^ false].
|
|
207 |
^ true
|
|
208 |
!
|
|
209 |
|
|
210 |
> aPoint
|
|
211 |
"return true if the receiver is below and to the right
|
|
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 represents the same point as
|
|
224 |
the argument, aPoint"
|
|
225 |
|
|
226 |
|p|
|
|
227 |
|
77
|
228 |
(aPoint isMemberOf:Point) ifTrue:[ "this is a hint to STC"
|
217
|
229 |
x ~= (aPoint x) ifTrue:[^ false].
|
|
230 |
y ~= (aPoint y) ifTrue:[^ false].
|
180
|
231 |
^ true
|
1
|
232 |
].
|
|
233 |
aPoint respondsToArithmetic ifFalse:[ ^ false].
|
|
234 |
p := aPoint asPoint.
|
217
|
235 |
x ~= (p x) ifTrue:[^ false].
|
|
236 |
y ~= (p y) ifTrue:[^ false].
|
1
|
237 |
^ true
|
|
238 |
!
|
|
239 |
|
|
240 |
max:aPoint
|
|
241 |
"return the lower right corner of the rectangle uniquely defined by
|
|
242 |
the receiver and the argument, aPoint"
|
|
243 |
|
|
244 |
|p maxX maxY|
|
|
245 |
|
|
246 |
p := aPoint asPoint.
|
|
247 |
maxX := x max:(p x).
|
|
248 |
maxY := y max:(p y).
|
|
249 |
^ maxX @ maxY
|
|
250 |
!
|
|
251 |
|
|
252 |
min:aPoint
|
|
253 |
"return the upper left corner of the rectangle uniquely defined by
|
|
254 |
the receiver and the argument, aPoint"
|
|
255 |
|
|
256 |
|p minX minY|
|
|
257 |
|
|
258 |
p := aPoint asPoint.
|
|
259 |
minX := x min:(p x).
|
|
260 |
minY := y min:(p y).
|
|
261 |
^ minX @ minY
|
|
262 |
! !
|
|
263 |
|
213
|
264 |
!Point methodsFor:'coercing & converting'!
|
1
|
265 |
|
|
266 |
generality
|
40
|
267 |
"return the generality value - see ArithmeticValue>>retry:coercing:"
|
|
268 |
|
1
|
269 |
^ 120
|
|
270 |
!
|
|
271 |
|
|
272 |
coerce:anObject
|
40
|
273 |
"return aNumber converted into receivers type"
|
|
274 |
|
1
|
275 |
^ anObject asPoint
|
|
276 |
!
|
|
277 |
|
|
278 |
asPoint
|
|
279 |
"return the receiver as Point - this is the receiver"
|
|
280 |
|
|
281 |
^ self
|
|
282 |
!
|
|
283 |
|
|
284 |
asRectangle
|
|
285 |
"return a zero-width rectangle consisting of origin
|
57
|
286 |
and corner being the receiver"
|
1
|
287 |
|
|
288 |
^ self corner:self
|
|
289 |
! !
|
|
290 |
|
|
291 |
!Point methodsFor:'creating rectangles'!
|
|
292 |
|
|
293 |
corner:aPoint
|
|
294 |
"return a rectangle whose origin is self and corner is aPoint"
|
|
295 |
|
|
296 |
^ Rectangle origin:self corner:aPoint
|
|
297 |
!
|
|
298 |
|
|
299 |
extent:aPoint
|
|
300 |
"return a rectangle whose origin is self and extent is aPoint"
|
|
301 |
|
|
302 |
^ Rectangle origin:self extent:aPoint
|
|
303 |
! !
|
|
304 |
|
|
305 |
!Point methodsFor:'transformations'!
|
|
306 |
|
|
307 |
+ scale
|
|
308 |
"Return a new Point that is the sum of the
|
|
309 |
receiver and scale (which is a Point or Number)."
|
|
310 |
|
|
311 |
|scalePoint|
|
|
312 |
|
77
|
313 |
"speedup for common cases ..."
|
|
314 |
|
|
315 |
(scale isMemberOf:Point) ifTrue:[
|
180
|
316 |
^ (x + scale x) @ (y + scale y)
|
77
|
317 |
].
|
|
318 |
scale isNumber ifTrue:[
|
180
|
319 |
^ (x + scale) @ (y + scale)
|
77
|
320 |
].
|
|
321 |
|
|
322 |
"this is the general (& clean) code ..."
|
|
323 |
|
1
|
324 |
scalePoint := scale asPoint.
|
|
325 |
^ (x + scalePoint x) @ (y + scalePoint y)
|
|
326 |
!
|
|
327 |
|
|
328 |
- scale
|
|
329 |
"Return a new Point that is the difference of the
|
|
330 |
receiver and scale (which is a Point or Number)."
|
|
331 |
|
|
332 |
|scalePoint|
|
|
333 |
|
77
|
334 |
"speedup for common cases ..."
|
|
335 |
|
|
336 |
(scale isMemberOf:Point) ifTrue:[
|
180
|
337 |
^ (x - scale x) @ (y - scale y)
|
77
|
338 |
].
|
|
339 |
scale isNumber ifTrue:[
|
180
|
340 |
^ (x - scale) @ (y - scale)
|
77
|
341 |
].
|
|
342 |
|
|
343 |
"this is the general (& clean) code ..."
|
|
344 |
|
1
|
345 |
scalePoint := scale asPoint.
|
|
346 |
^ (x - scalePoint x) @ (y - scalePoint y)
|
|
347 |
!
|
|
348 |
|
|
349 |
* scale
|
|
350 |
"Return a new Point that is the product of the
|
|
351 |
receiver and scale (which is a Point or Number)."
|
|
352 |
|
|
353 |
|scalePoint|
|
|
354 |
|
77
|
355 |
"speedup for common cases ..."
|
|
356 |
|
|
357 |
(scale isMemberOf:Point) ifTrue:[
|
180
|
358 |
^ (x * scale x) @ (y * scale y)
|
77
|
359 |
].
|
|
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 integer quotient 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 |
].
|
|
381 |
scale isNumber ifTrue:[
|
180
|
382 |
^ (x / scale) @ (y / scale)
|
77
|
383 |
].
|
|
384 |
|
|
385 |
"this is the general (& clean) code ..."
|
|
386 |
|
1
|
387 |
scalePoint := scale asPoint.
|
|
388 |
^ (x / scalePoint x) @ (y / scalePoint y)
|
|
389 |
!
|
|
390 |
|
|
391 |
// scale
|
|
392 |
"Return a new Point that is the quotient of the
|
|
393 |
receiver and scale (which is a Point or Number)."
|
|
394 |
|
|
395 |
|scalePoint|
|
|
396 |
|
|
397 |
scalePoint := scale asPoint.
|
|
398 |
^ (x // scalePoint x) @ (y // scalePoint y)
|
|
399 |
!
|
|
400 |
|
|
401 |
reciprocal
|
|
402 |
"return a new point where the coordinates are
|
|
403 |
the reciproce of mine"
|
|
404 |
|
|
405 |
^ (1 / x) @ (1 / y)
|
|
406 |
!
|
|
407 |
|
|
408 |
negated
|
77
|
409 |
"return a new point with my coordinates negated
|
|
410 |
i.e. the receiver mirrored at the origin"
|
1
|
411 |
|
|
412 |
^ (x negated) @ (y negated)
|
3
|
413 |
!
|
|
414 |
|
|
415 |
scaledBy:aScale
|
|
416 |
"return a new Point that is the product of the
|
1
|
417 |
receiver and scale (which is a Point or Number)."
|
|
418 |
|
|
419 |
^ self * aScale
|
|
420 |
!
|
|
421 |
|
3
|
422 |
translatedBy:anOffset
|
|
423 |
"return a new Point that is the sum of the
|
1
|
424 |
receiver and scale (which is a Point or Number)."
|
|
425 |
|
|
426 |
^ self + anOffset
|
|
427 |
! !
|
|
428 |
|
77
|
429 |
!Point methodsFor:'destructive transformations'!
|
|
430 |
|
|
431 |
scaleBy:aScale
|
|
432 |
"scale the receiver, by replacing coordinates by the product
|
180
|
433 |
of the receivers coordinates and the scale (a Point or Number).
|
|
434 |
This is destructive (modifies the receiver, not a copy) and
|
|
435 |
should only be used if you know, that you are the exclusive owner
|
|
436 |
of the receiver."
|
77
|
437 |
|
|
438 |
|scalePoint|
|
|
439 |
|
180
|
440 |
(aScale isMemberOf:Point) ifTrue:[ "type hint to stc"
|
|
441 |
x := x * aScale x.
|
|
442 |
y := y * aScale y.
|
|
443 |
^ self
|
77
|
444 |
].
|
|
445 |
aScale isNumber ifTrue:[
|
180
|
446 |
x := x * aScale.
|
|
447 |
y := y * aScale.
|
|
448 |
^ self
|
77
|
449 |
].
|
|
450 |
|
|
451 |
"this is the general (& clean) code ..."
|
|
452 |
|
|
453 |
scalePoint := aScale asPoint.
|
|
454 |
x := x * scalePoint x.
|
|
455 |
y := y * scalePoint y
|
|
456 |
!
|
|
457 |
|
|
458 |
translateBy:anOffset
|
|
459 |
"translate the receiver, by replacing coordinates by the sum
|
180
|
460 |
of the receivers coordinated and the scale (a Point or Number).
|
|
461 |
This is destructive (modifies the receiver, not a copy) and
|
|
462 |
should only be used if you know, that you are the exclusive owner
|
|
463 |
of the receiver."
|
77
|
464 |
|
|
465 |
|offsetPoint|
|
|
466 |
|
180
|
467 |
(anOffset isMemberOf:Point) ifTrue:[ "type hint to stc"
|
|
468 |
x := x + anOffset x.
|
|
469 |
y := y + anOffset y.
|
|
470 |
^ self
|
77
|
471 |
].
|
|
472 |
anOffset isNumber ifTrue:[
|
180
|
473 |
x := x + anOffset.
|
|
474 |
y := y + anOffset.
|
|
475 |
^ self
|
77
|
476 |
].
|
|
477 |
|
|
478 |
"this is the general (& clean) code ..."
|
|
479 |
|
|
480 |
offsetPoint := anOffset asPoint.
|
|
481 |
x := x + anOffset x.
|
|
482 |
y := y + anOffset y
|
|
483 |
! !
|
|
484 |
|
|
485 |
!Point methodsFor:'queries'!
|
|
486 |
|
|
487 |
isPoint
|
|
488 |
"return true, if the receiver is some kind of point"
|
|
489 |
|
|
490 |
^ true
|
|
491 |
! !
|
|
492 |
|
1
|
493 |
!Point methodsFor:'misc'!
|
|
494 |
|
|
495 |
dist:aPoint
|
|
496 |
"return the distance between aPoint and the receiver."
|
|
497 |
|
|
498 |
^ (aPoint - self) r
|
|
499 |
!
|
|
500 |
|
|
501 |
dotProduct:aPoint
|
57
|
502 |
"return a number that is the dot product of the receiver and
|
1
|
503 |
the argument, aPoint. That is, the two points are
|
57
|
504 |
multiplied and the coordinates of the result summed."
|
1
|
505 |
|
|
506 |
|temp|
|
|
507 |
|
|
508 |
temp := self * aPoint.
|
|
509 |
^ temp x abs + temp y abs
|
|
510 |
!
|
|
511 |
|
|
512 |
r
|
217
|
513 |
"return the receiver's radius in a polar coordinate system.
|
|
514 |
(i.e. the length of a vector from 0@0 to the receiver)"
|
1
|
515 |
|
|
516 |
^ (self dotProduct:self) sqrt
|
217
|
517 |
|
|
518 |
"
|
|
519 |
(1@1) r
|
|
520 |
(2@1) r
|
|
521 |
(2@0) r
|
|
522 |
"
|
|
523 |
!
|
|
524 |
|
|
525 |
angle
|
|
526 |
"return the receiver's angle in a polar coordinate system.
|
|
527 |
(i.e. the angle of a vector from 0@0 to the receiver)"
|
|
528 |
|
|
529 |
y < 0 ifTrue:[
|
|
530 |
x < 0 ifTrue:[
|
|
531 |
^ 270 degreesToRadians - (y / x) arcTan
|
|
532 |
].
|
|
533 |
x = 0 ifTrue:[
|
|
534 |
^ 180
|
|
535 |
].
|
|
536 |
^ 360 degreesToRadians - (y abs / x) arcTan
|
|
537 |
].
|
|
538 |
x < 0 ifTrue:[
|
|
539 |
^ 180 degreesToRadians - (y / x abs) arcTan
|
|
540 |
].
|
|
541 |
x = 0 ifTrue:[
|
|
542 |
^ 0
|
|
543 |
].
|
|
544 |
^ (y / x) arcTan
|
|
545 |
|
|
546 |
"
|
|
547 |
(1@1) angle radiansToDegrees
|
|
548 |
(2@1) angle radiansToDegrees
|
|
549 |
"
|
1
|
550 |
!
|
|
551 |
|
|
552 |
abs
|
77
|
553 |
"return a new point with my coordinates taken from the absolute values."
|
1
|
554 |
|
|
555 |
^ (x abs) @ (y abs)
|
|
556 |
!
|
|
557 |
|
|
558 |
truncated
|
57
|
559 |
"return a new point with my coordinates truncated as integer or the
|
|
560 |
receiver, if already truncated."
|
1
|
561 |
|
57
|
562 |
(x isInteger and:[y isInteger]) ifTrue:[^ self].
|
1
|
563 |
^ (x truncated) @ (y truncated)
|
|
564 |
!
|
|
565 |
|
|
566 |
rounded
|
|
567 |
"return a new point with my coordinates rounded to the next integer
|
57
|
568 |
coordinated (use for gridding) or the receiver of already rounded."
|
1
|
569 |
|
57
|
570 |
(x isInteger and:[y isInteger]) ifTrue:[^ self].
|
1
|
571 |
^ (x rounded) @ (y rounded)
|
|
572 |
!
|
|
573 |
|
|
574 |
grid:gridPoint
|
|
575 |
"return a new point with coordinates grided (i.e. rounded to the
|
|
576 |
nearest point on the grid)"
|
|
577 |
|
|
578 |
|newX newY gridX gridY|
|
|
579 |
|
|
580 |
gridX := gridPoint x.
|
|
581 |
(gridX <= 1) ifTrue:[
|
180
|
582 |
newX := x asInteger
|
1
|
583 |
] ifFalse:[
|
180
|
584 |
newX := ((x + (gridX // 2)) // gridX) * gridX
|
1
|
585 |
].
|
|
586 |
gridY := gridPoint y.
|
|
587 |
(gridY <= 1) ifTrue:[
|
180
|
588 |
newY := y asInteger
|
1
|
589 |
] ifFalse:[
|
180
|
590 |
newY := ((y + (gridY // 2)) // gridY) * gridY
|
1
|
591 |
].
|
|
592 |
^ newX @ newY
|
77
|
593 |
!
|
|
594 |
|
|
595 |
quadrantContaining:aPoint
|
|
596 |
"return the number of the quadrant containing aPoint placing
|
|
597 |
the receiver at the origin, where the quadrants are numbered as
|
|
598 |
follows:
|
180
|
599 |
^ 2 | 3
|
|
600 |
Y ------
|
|
601 |
1 | 0
|
77
|
602 |
|
180
|
603 |
X >
|
77
|
604 |
This can be used for polygon operations (see Foley for examples).
|
|
605 |
"
|
|
606 |
|
|
607 |
aPoint x > x ifTrue:[
|
180
|
608 |
aPoint y >= y ifTrue:[^ 3].
|
|
609 |
^ 0
|
77
|
610 |
].
|
|
611 |
aPoint y >= y ifTrue: [^ 2].
|
|
612 |
^ 1
|
|
613 |
|
|
614 |
"
|
|
615 |
(10 @ 10) quadrantContaining:(15 @ 15)
|
|
616 |
(10 @ 10) quadrantContaining:(5 @ 5)
|
|
617 |
(10 @ 10) quadrantContaining:(5 @ 15)
|
|
618 |
(10 @ 10) quadrantContaining:(15 @ 5)
|
|
619 |
"
|
217
|
620 |
!
|
|
621 |
|
|
622 |
quadrant
|
|
623 |
"return the number of the quadrant containing the receiver.
|
|
624 |
quadrants are named as follows:
|
|
625 |
|
|
626 |
^ 2 | 3
|
|
627 |
Y ------
|
|
628 |
1 | 0
|
|
629 |
|
|
630 |
X >
|
|
631 |
"
|
|
632 |
|
|
633 |
^ 0@0 quadrantContaining:self
|
1
|
634 |
! !
|
|
635 |
|
|
636 |
!Point methodsFor:'printing & storing'!
|
|
637 |
|
57
|
638 |
printOn:aStream
|
|
639 |
"append a printed representation of the receiver to aStream"
|
1
|
640 |
|
57
|
641 |
x printOn:aStream.
|
|
642 |
aStream nextPut:$@.
|
|
643 |
y printOn:aStream
|
1
|
644 |
!
|
|
645 |
|
57
|
646 |
storeOn:aStream
|
|
647 |
"append my storeString to aStream"
|
1
|
648 |
|
57
|
649 |
aStream nextPut:$(.
|
|
650 |
x storeOn:aStream.
|
|
651 |
aStream nextPut:$@.
|
|
652 |
y storeOn:aStream.
|
|
653 |
aStream nextPut:$)
|
1
|
654 |
! !
|