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