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