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