author | Stefan Vogel <sv@exept.de> |
Wed, 30 Aug 2000 14:35:40 +0200 | |
changeset 5567 | 2cc43ea7394f |
parent 5361 | dbf17a65f6bd |
child 6624 | 886265eff14c |
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 |
||
5361
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
13 |
"{ Package: 'stx:libbasic' }" |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
14 |
|
1 | 15 |
ArithmeticValue subclass:#Point |
1245 | 16 |
instanceVariableNames:'x y' |
17 |
classVariableNames:'PointZero PointOne' |
|
18 |
poolDictionaries:'' |
|
19 |
category:'Graphics-Geometry' |
|
1 | 20 |
! |
21 |
||
2273 | 22 |
!Point class methodsFor:'documentation'! |
77 | 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 |
||
77 | 38 |
documentation |
39 |
" |
|
40 |
I represent a point in 2D space. Or I can be used to represent |
|
41 |
an extent (of a rectangle, for example), in which case my x-coordinate |
|
42 |
represents the width, and y-coordinate the height of something. |
|
43 |
||
44 |
The x and y coordinate are usually numbers. |
|
45 |
||
1290 | 46 |
[Instance variables:] |
77 | 47 |
|
5322 | 48 |
x <Number> the x-coordinate of myself |
49 |
y <Number> the y-coordinate of myself |
|
1290 | 50 |
|
51 |
[author:] |
|
5322 | 52 |
Claus Gittinger |
1314 | 53 |
|
54 |
[see also:] |
|
5322 | 55 |
Rectangle Polygon |
56 |
LayoutOrigin LayoutFrame AlignmentOrigin Layout |
|
57 |
View GraphicsContext |
|
77 | 58 |
" |
59 |
! ! |
|
60 |
||
2273 | 61 |
!Point class methodsFor:'initialization'! |
1 | 62 |
|
63 |
initialize |
|
302 | 64 |
PointZero isNil ifTrue:[ |
65 |
PointZero := 0 @ 0. |
|
66 |
PointOne := 1 @ 1 |
|
67 |
] |
|
1 | 68 |
! ! |
69 |
||
2273 | 70 |
!Point class methodsFor:'instance creation'! |
1 | 71 |
|
3238
f96eaf89d55d
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
72 |
decodeFromLiteralArray:anArray |
f96eaf89d55d
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
73 |
"create & return a new instance from information encoded in anArray. |
f96eaf89d55d
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
74 |
Redefined for faster creation." |
f96eaf89d55d
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
75 |
|
f96eaf89d55d
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
76 |
^ self x:(anArray at:2) y:(anArray at:3) |
f96eaf89d55d
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
77 |
|
f96eaf89d55d
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
78 |
" |
f96eaf89d55d
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
79 |
Point |
5322 | 80 |
decodeFromLiteralArray:#(Point 10 10) |
3238
f96eaf89d55d
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
81 |
" |
f96eaf89d55d
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
82 |
|
f96eaf89d55d
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
83 |
"Created: / 28.1.1998 / 17:44:08 / cg" |
f96eaf89d55d
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
84 |
! |
f96eaf89d55d
faster decodeFromLiteralEncoding
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
85 |
|
1354 | 86 |
r:distance angle:angle |
87 |
"create and return a new point given polar coordinates. |
|
88 |
The angle is given in degrees" |
|
89 |
||
90 |
|x y rad| |
|
91 |
||
92 |
rad := angle degreesToRadians. |
|
93 |
x := distance * rad cos. |
|
94 |
y := distance * rad sin. |
|
95 |
^ x @ y |
|
96 |
||
97 |
" |
|
98 |
Point r:100 angle:0 |
|
99 |
Point r:100 angle:90 |
|
100 |
Point r:100 angle:45 |
|
101 |
Point r:100 angle:180 |
|
102 |
" |
|
103 |
||
104 |
"Modified: 8.5.1996 / 20:01:50 / cg" |
|
105 |
! |
|
106 |
||
4893 | 107 |
r:distance degrees:angle |
108 |
"create and return a new point given polar coordinates. |
|
109 |
The angle is given in degrees. |
|
110 |
Added for Squeak compatibility" |
|
111 |
||
112 |
^ self r:distance angle:angle |
|
113 |
||
114 |
" |
|
115 |
Point r:100 degrees:90 |
|
116 |
" |
|
117 |
||
118 |
"Modified: 8.5.1996 / 20:01:50 / cg" |
|
119 |
! |
|
120 |
||
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
121 |
r:distance theta:angleInRadians |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
122 |
"create and return a new point given polar coordinates. |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
123 |
The angle is given in radians" |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
124 |
|
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
125 |
|x y| |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
126 |
|
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
127 |
x := distance * angleInRadians cos. |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
128 |
y := distance * angleInRadians sin. |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
129 |
^ x @ y |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
130 |
|
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
131 |
" |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
132 |
Point r:100 theta:0 |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
133 |
" |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
134 |
|
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
135 |
"Modified: 2.4.1997 / 00:01:40 / cg" |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
136 |
! |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
137 |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
138 |
readFrom:aStringOrStream onError:exceptionBlock |
1 | 139 |
"return the next Point from the (character-)stream aStream; |
202 | 140 |
skipping all whitespace first; return the value of exceptionBlock, |
141 |
if no point can be read." |
|
1 | 142 |
|
1701
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1354
diff
changeset
|
143 |
ErrorSignal handle:[:ex | |
5322 | 144 |
^ exceptionBlock value |
1701
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1354
diff
changeset
|
145 |
] do:[ |
5322 | 146 |
|str newX newY| |
1 | 147 |
|
5322 | 148 |
str := aStringOrStream readStream. |
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
149 |
|
5322 | 150 |
newX := Number readFrom:str onError:nil. |
151 |
newX notNil ifTrue:[ |
|
152 |
(str skipSeparators == $@) ifTrue:[ |
|
153 |
str next. |
|
154 |
newY := Number readFrom:str onError:nil. |
|
155 |
newY notNil ifTrue:[ |
|
156 |
^ self x:newX y:newY |
|
157 |
] |
|
158 |
] |
|
159 |
]. |
|
345 | 160 |
]. |
161 |
^ exceptionBlock value |
|
77 | 162 |
|
202 | 163 |
" |
164 |
Point readFrom:('1.234 @ 5.678' readStream) |
|
345 | 165 |
Point readFrom:('1' readStream) |
166 |
Point readFrom:('1' readStream) onError:[1@1] |
|
202 | 167 |
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
|
168 |
" |
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
169 |
|
1701
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1354
diff
changeset
|
170 |
"Modified: 8.10.1996 / 19:31:39 / cg" |
637 | 171 |
! |
172 |
||
173 |
x:newX y:newY |
|
174 |
"create and return a new point with coordinates newX and newY" |
|
175 |
||
176 |
%{ /* NOCONTEXT */ |
|
177 |
||
178 |
/* |
|
179 |
* claus: I am no longer certain, if this primitive is worth the effort |
|
180 |
*/ |
|
5322 | 181 |
if (__CanDoQuickNew(sizeof(struct __Point))) { /* OBJECT ALLOCATION */ |
1133 | 182 |
if (self == @global(Point)) { |
637 | 183 |
OBJ newPoint; |
184 |
int spc; |
|
185 |
||
5322 | 186 |
__qCheckedAlignedNew(newPoint, sizeof(struct __Point)); |
1133 | 187 |
__InstPtr(newPoint)->o_class = self; |
188 |
__PointInstPtr(newPoint)->p_x = newX; |
|
189 |
__PointInstPtr(newPoint)->p_y = newY; |
|
637 | 190 |
if (! __bothSmallInteger(newX, newY)) { |
191 |
spc = __qSpace(newPoint); |
|
192 |
__STORE_SPC(newPoint, newX, spc); |
|
193 |
__STORE_SPC(newPoint, newY, spc); |
|
194 |
} |
|
195 |
RETURN ( newPoint ); |
|
196 |
} |
|
197 |
} |
|
1133 | 198 |
%}. |
637 | 199 |
^ (self basicNew) x:newX y:newY |
1 | 200 |
! ! |
201 |
||
2273 | 202 |
!Point class methodsFor:'constants'! |
356 | 203 |
|
637 | 204 |
unity |
205 |
"return the neutral element for multiplication" |
|
206 |
||
207 |
^ PointOne |
|
208 |
! |
|
209 |
||
356 | 210 |
zero |
211 |
"return the neutral element for addition" |
|
212 |
||
213 |
^ PointZero |
|
214 |
! ! |
|
215 |
||
2273 | 216 |
!Point class methodsFor:'queries'! |
356 | 217 |
|
218 |
isBuiltInClass |
|
1264 | 219 |
"return true if this class is known by the run-time-system. |
220 |
Here, true is returned for myself, false for subclasses." |
|
356 | 221 |
|
222 |
^ self == Point |
|
1264 | 223 |
|
224 |
"Modified: 23.4.1996 / 16:00:18 / cg" |
|
356 | 225 |
! ! |
226 |
||
4984 | 227 |
!Point methodsFor:'Compatibility - Squeak'! |
228 |
||
5006 | 229 |
adhereTo: aRectangle |
230 |
"If the receiver lies outside aRectangle, return the nearest point on the boundary of the rectangle, otherwise return self." |
|
231 |
||
232 |
(aRectangle containsPoint: self) ifTrue: [^ self]. |
|
233 |
^ ((x max: aRectangle left) min: aRectangle right) |
|
234 |
@ |
|
235 |
((y max: aRectangle top) min: aRectangle bottom) |
|
236 |
! |
|
237 |
||
4984 | 238 |
asFloatPoint |
239 |
"Squeak mimicri return the receiver as Point - this is the receiver" |
|
240 |
||
241 |
^ self |
|
242 |
! ! |
|
243 |
||
637 | 244 |
!Point methodsFor:'accessing'! |
245 |
||
246 |
x |
|
247 |
"return the x coordinate" |
|
248 |
||
249 |
^ x |
|
250 |
! |
|
356 | 251 |
|
637 | 252 |
x:newX |
253 |
"set the x coordinate to be the argument, aNumber. |
|
254 |
This is destructive (modifies the receiver, not a copy) and |
|
255 |
should only be used if you know, that you are the exclusive owner |
|
256 |
of the receiver." |
|
356 | 257 |
|
637 | 258 |
x := newX |
356 | 259 |
! |
260 |
||
637 | 261 |
x:newX y:newY |
262 |
"set both the x and y coordinates. |
|
263 |
This is destructive (modifies the receiver, not a copy) and |
|
264 |
should only be used if you know, that you are the exclusive owner |
|
265 |
of the receiver." |
|
1 | 266 |
|
637 | 267 |
x := newX. |
268 |
y := newY |
|
269 |
! |
|
270 |
||
271 |
y |
|
272 |
"return the y coordinate" |
|
273 |
||
274 |
^ y |
|
356 | 275 |
! |
276 |
||
637 | 277 |
y:newY |
278 |
"set the y coordinate to be the argument, aNumber. |
|
279 |
This is destructive (modifies the receiver, not a copy) and |
|
280 |
should only be used if you know, that you are the exclusive owner |
|
281 |
of the receiver." |
|
356 | 282 |
|
637 | 283 |
y := newY |
284 |
! ! |
|
285 |
||
5567 | 286 |
!Point methodsFor:'coercing & converting'! |
637 | 287 |
|
288 |
coerce:anObject |
|
289 |
"return aNumber converted into receivers type" |
|
290 |
||
291 |
^ anObject asPoint |
|
356 | 292 |
! |
1 | 293 |
|
637 | 294 |
generality |
295 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
296 |
||
297 |
^ 120 |
|
298 |
! ! |
|
356 | 299 |
|
637 | 300 |
!Point methodsFor:'comparing'! |
301 |
||
302 |
< aPoint |
|
303 |
"return true if the receiver is above and to the left |
|
304 |
of the argument, aPoint" |
|
305 |
||
306 |
|p| |
|
356 | 307 |
|
637 | 308 |
p := aPoint asPoint. |
1339 | 309 |
x < (p x) ifTrue:[ |
5322 | 310 |
y < (p y) ifTrue:[^ true]. |
1339 | 311 |
]. |
312 |
^ false |
|
313 |
||
314 |
"notice the funny result if one coordinate has the same value ... |
|
315 |
||
316 |
(3@4) < (4@4) |
|
317 |
(3@4) <= (4@4) |
|
318 |
(3@4) > (4@4) |
|
319 |
(4@4) >= (3@4) |
|
320 |
(4@4) > (3@4) |
|
321 |
" |
|
322 |
||
323 |
"Modified: 7.5.1996 / 12:14:25 / cg" |
|
637 | 324 |
! |
325 |
||
326 |
= aPoint |
|
327 |
"return true if the receiver represents the same point as |
|
328 |
the argument, aPoint" |
|
356 | 329 |
|
637 | 330 |
|p| |
331 |
||
332 |
(aPoint isMemberOf:Point) ifTrue:[ "this is a hint to STC" |
|
333 |
x ~= (aPoint x) ifTrue:[^ false]. |
|
334 |
y ~= (aPoint y) ifTrue:[^ false]. |
|
335 |
^ true |
|
336 |
]. |
|
337 |
aPoint respondsToArithmetic ifFalse:[ ^ false]. |
|
338 |
p := aPoint asPoint. |
|
339 |
x ~= (p x) ifTrue:[^ false]. |
|
340 |
y ~= (p y) ifTrue:[^ false]. |
|
341 |
^ true |
|
1 | 342 |
! |
343 |
||
637 | 344 |
> aPoint |
345 |
"return true if the receiver is below and to the right |
|
346 |
of the argument, aPoint" |
|
347 |
||
348 |
|p| |
|
356 | 349 |
|
637 | 350 |
p := aPoint asPoint. |
1339 | 351 |
(p x) < x ifTrue:[ |
5322 | 352 |
(p y) < y ifTrue:[^ true]. |
1339 | 353 |
]. |
354 |
^ false |
|
355 |
||
356 |
"Modified: 7.5.1996 / 12:11:15 / cg" |
|
637 | 357 |
! |
356 | 358 |
|
637 | 359 |
hash |
360 |
"return a number for hashing" |
|
356 | 361 |
|
637 | 362 |
x = y ifTrue:[^ x hash]. |
363 |
" |
|
364 |
used to be: |
|
365 |
^ (x hash) bitXor:(y hash) |
|
366 |
the following handles 1@x vs. x@1 better: |
|
367 |
" |
|
4857 | 368 |
^ (x hash) bitXor:(y hash bitShift:16) |
369 |
||
370 |
||
371 |
||
372 |
||
373 |
||
637 | 374 |
! |
356 | 375 |
|
637 | 376 |
max:aPoint |
377 |
"return the lower right corner of the rectangle uniquely defined by |
|
378 |
the receiver and the argument, aPoint" |
|
379 |
||
380 |
|p maxX maxY| |
|
356 | 381 |
|
637 | 382 |
p := aPoint asPoint. |
383 |
maxX := x max:(p x). |
|
384 |
maxY := y max:(p y). |
|
385 |
^ maxX @ maxY |
|
386 |
! |
|
387 |
||
388 |
min:aPoint |
|
389 |
"return the upper left corner of the rectangle uniquely defined by |
|
390 |
the receiver and the argument, aPoint" |
|
391 |
||
392 |
|p minX minY| |
|
393 |
||
394 |
p := aPoint asPoint. |
|
395 |
minX := x min:(p x). |
|
396 |
minY := y min:(p y). |
|
397 |
^ minX @ minY |
|
356 | 398 |
! ! |
399 |
||
400 |
!Point methodsFor:'converting'! |
|
401 |
||
3636 | 402 |
asComplex |
403 |
"Return a complex number whose real and imaginary components are the x and y |
|
404 |
coordinates of the receiver." |
|
405 |
||
406 |
^ x % y |
|
407 |
||
408 |
"Modified: / 9.7.1998 / 10:21:10 / cg" |
|
409 |
! |
|
410 |
||
637 | 411 |
asFractionalLayout |
412 |
"return a LayoutOrigin from the receiver, |
|
413 |
treating the receiver coordinates as fractional parts |
|
414 |
(i.e. relative to superview). |
|
415 |
Notice: in 10.5.x LayoutOrigin is not yet officially released." |
|
416 |
||
417 |
^ LayoutOrigin fractionalFromPoint:self |
|
356 | 418 |
|
637 | 419 |
" |
420 |
(0@0.5) asFractionalLayout |
|
421 |
(0@0.5) asLayout |
|
422 |
(0@10) asLayout |
|
423 |
(0@10) asOffsetLayout |
|
424 |
" |
|
356 | 425 |
|
426 |
! |
|
1 | 427 |
|
4865 | 428 |
asIntegerPoint |
429 |
(x isInteger and:[y isInteger]) ifTrue:[ ^ self]. |
|
430 |
^ x asInteger @ y asInteger |
|
431 |
! |
|
432 |
||
356 | 433 |
asLayout |
434 |
"return a LayoutOrigin from the receiver. |
|
435 |
If the receiver coordinates are between 0 and 1, take |
|
436 |
them as fractional parts (relative to superview). |
|
437 |
Otherwise, treat them as absolute offsets. |
|
438 |
Notice: in 10.5.x LayoutOrigin is not yet released." |
|
439 |
||
440 |
^ LayoutOrigin fromPoint:self |
|
441 |
||
442 |
" |
|
443 |
(0@0.5) asFractionalLayout |
|
444 |
(0@0.5) asLayout |
|
445 |
(0@10) asLayout |
|
446 |
(0@10) asOffsetLayout |
|
447 |
" |
|
448 |
||
449 |
! |
|
450 |
||
451 |
asOffsetLayout |
|
452 |
"return a LayoutOrigin from the receiver, |
|
453 |
treating the receiver coordinates as absolute offsets. |
|
454 |
Notice: in 10.5.x LayoutOrigin is not yet released." |
|
455 |
||
456 |
^ LayoutOrigin offsetFromPoint:self |
|
457 |
||
458 |
" |
|
459 |
(0@0.5) asFractionalLayout |
|
460 |
(0@0.5) asLayout |
|
461 |
(0@10) asLayout |
|
462 |
(0@10) asOffsetLayout |
|
463 |
" |
|
464 |
||
1 | 465 |
! |
466 |
||
637 | 467 |
asPoint |
468 |
"return the receiver as Point - this is the receiver" |
|
356 | 469 |
|
637 | 470 |
^ self |
356 | 471 |
! |
472 |
||
473 |
asRectangle |
|
474 |
"return a zero-width rectangle consisting of origin |
|
475 |
and corner being the receiver" |
|
476 |
||
477 |
^ self corner:self |
|
478 |
||
479 |
" |
|
480 |
(0@10) asRectangle |
|
481 |
" |
|
482 |
! |
|
483 |
||
637 | 484 |
corner:aPoint |
485 |
"return a rectangle whose origin is self and corner is aPoint" |
|
486 |
||
487 |
^ Rectangle origin:self corner:aPoint |
|
488 |
! |
|
489 |
||
490 |
extent:aPoint |
|
491 |
"return a rectangle whose origin is self and extent is aPoint" |
|
356 | 492 |
|
637 | 493 |
^ Rectangle origin:self extent:aPoint |
494 |
! |
|
495 |
||
496 |
fromLiteralArrayEncoding:encoding |
|
497 |
"read my values from an encoding. |
|
498 |
The encoding is supposed to be of the form: (Point xValue yValue)" |
|
499 |
||
500 |
x := encoding at:2. |
|
501 |
y := encoding at:3. |
|
356 | 502 |
|
503 |
" |
|
637 | 504 |
Point new fromLiteralArrayEncoding:#(Point 10 20) |
356 | 505 |
" |
379 | 506 |
! |
507 |
||
421 | 508 |
literalArrayEncoding |
1245 | 509 |
"encode myself as an array, from which a copy of the receiver |
510 |
can be reconstructed with #decodeAsLiteralArray. |
|
421 | 511 |
The encoding is: (Point xValue yValue)" |
512 |
||
513 |
^ Array |
|
5322 | 514 |
with:#Point |
515 |
with:x |
|
516 |
with:y |
|
421 | 517 |
|
518 |
||
519 |
" |
|
520 |
Point new fromLiteralArrayEncoding:#(Point 10 20) |
|
521 |
(10@20) literalArrayEncoding |
|
522 |
" |
|
523 |
||
524 |
"Modified: 1.9.1995 / 02:18:29 / claus" |
|
1245 | 525 |
"Modified: 22.4.1996 / 13:00:32 / cg" |
421 | 526 |
! |
527 |
||
3488
6e1feb9ead68
prefRect may now be a valueHolder or block
Claus Gittinger <cg@exept.de>
parents:
3238
diff
changeset
|
528 |
rectangleRelativeTo:aRectangle preferred:prefRectHolder |
637 | 529 |
"compute a displayRectangle, treating the receiver like a |
530 |
layoutorigin. This allows point to be used interchangable with |
|
531 |
LayoutOrigins." |
|
379 | 532 |
|
3488
6e1feb9ead68
prefRect may now be a valueHolder or block
Claus Gittinger <cg@exept.de>
parents:
3238
diff
changeset
|
533 |
^ (self asLayout) rectangleRelativeTo:aRectangle preferred:prefRectHolder |
379 | 534 |
|
535 |
" |
|
637 | 536 |
consider the case, where a view has a preferred extent of 50@50 |
537 |
and is to be positioned in its superview which has size 100@100. |
|
538 |
For absolute origin: |
|
5322 | 539 |
(10@20) rectangleRelativeTo:(0@0 corner:100@100) preferred:(0@0 corner:50@50) |
1 | 540 |
|
637 | 541 |
for relative origin: |
5322 | 542 |
(0.5@0.5) rectangleRelativeTo:(0@0 corner:100@100) preferred:(0@0 corner:50@50) |
637 | 543 |
" |
3488
6e1feb9ead68
prefRect may now be a valueHolder or block
Claus Gittinger <cg@exept.de>
parents:
3238
diff
changeset
|
544 |
|
6e1feb9ead68
prefRect may now be a valueHolder or block
Claus Gittinger <cg@exept.de>
parents:
3238
diff
changeset
|
545 |
"Modified: / 27.5.1998 / 10:20:13 / cg" |
1 | 546 |
! ! |
547 |
||
77 | 548 |
!Point methodsFor:'destructive transformations'! |
549 |
||
550 |
scaleBy:aScale |
|
551 |
"scale the receiver, by replacing coordinates by the product |
|
180 | 552 |
of the receivers coordinates and the scale (a Point or Number). |
553 |
This is destructive (modifies the receiver, not a copy) and |
|
554 |
should only be used if you know, that you are the exclusive owner |
|
555 |
of the receiver." |
|
77 | 556 |
|
557 |
|scalePoint| |
|
558 |
||
180 | 559 |
(aScale isMemberOf:Point) ifTrue:[ "type hint to stc" |
560 |
x := x * aScale x. |
|
561 |
y := y * aScale y. |
|
562 |
^ self |
|
77 | 563 |
]. |
564 |
aScale isNumber ifTrue:[ |
|
180 | 565 |
x := x * aScale. |
566 |
y := y * aScale. |
|
567 |
^ self |
|
77 | 568 |
]. |
569 |
||
570 |
"this is the general (& clean) code ..." |
|
571 |
||
572 |
scalePoint := aScale asPoint. |
|
573 |
x := x * scalePoint x. |
|
574 |
y := y * scalePoint y |
|
575 |
! |
|
576 |
||
577 |
translateBy:anOffset |
|
578 |
"translate the receiver, by replacing coordinates by the sum |
|
180 | 579 |
of the receivers coordinated and the scale (a Point or Number). |
580 |
This is destructive (modifies the receiver, not a copy) and |
|
581 |
should only be used if you know, that you are the exclusive owner |
|
582 |
of the receiver." |
|
77 | 583 |
|
584 |
|offsetPoint| |
|
585 |
||
180 | 586 |
(anOffset isMemberOf:Point) ifTrue:[ "type hint to stc" |
587 |
x := x + anOffset x. |
|
588 |
y := y + anOffset y. |
|
589 |
^ self |
|
77 | 590 |
]. |
591 |
anOffset isNumber ifTrue:[ |
|
180 | 592 |
x := x + anOffset. |
593 |
y := y + anOffset. |
|
594 |
^ self |
|
77 | 595 |
]. |
596 |
||
597 |
"this is the general (& clean) code ..." |
|
598 |
||
599 |
offsetPoint := anOffset asPoint. |
|
600 |
x := x + anOffset x. |
|
601 |
y := y + anOffset y |
|
602 |
! ! |
|
603 |
||
5361
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
604 |
!Point methodsFor:'interpolating'! |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
605 |
|
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
606 |
interpolateTo: end at: amountDone |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
607 |
"Interpolate between the instance and end after the specified amount has been done (0 - 1)." |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
608 |
|
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
609 |
^ self + ((end - self) * amountDone). |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
610 |
|
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
611 |
" |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
612 |
(10@10) interpolateTo:(20@20) at:0.5 |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
613 |
(10@10) interpolateTo:(20@20) at:0.3 |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
614 |
(0@0) interpolateTo:(0@20) at:0.5 |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
615 |
" |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
616 |
! ! |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
617 |
|
637 | 618 |
!Point methodsFor:'misc'! |
619 |
||
620 |
abs |
|
621 |
"return a new point with my coordinates taken from the absolute values." |
|
622 |
||
623 |
^ (x abs) @ (y abs) |
|
624 |
! |
|
625 |
||
3562
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
626 |
ceiling |
5361
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
627 |
"return a new point with my coordinates truncated towards positive infinity. |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
628 |
Return the receiver if its coordinates are already integral." |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
629 |
|
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
630 |
(x isInteger and:[y isInteger]) ifTrue: [ |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
631 |
^ self |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
632 |
]. |
3562
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
633 |
|
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
634 |
^ (x ceiling) @ (y ceiling) |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
635 |
! |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
636 |
|
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
637 |
floor |
5361
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
638 |
"return a new point with my coordinates truncated towards negative infinity. |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
639 |
Return the receiver if its coordinates are already integral." |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
640 |
|
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
641 |
(x isInteger and:[y isInteger]) ifTrue: [ |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
642 |
^ self |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
643 |
]. |
3562
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
644 |
|
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
645 |
^ (x floor) @ (y floor) |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
646 |
! |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
647 |
|
637 | 648 |
quadrant |
649 |
"return the number of the quadrant containing the receiver. |
|
650 |
quadrants are named as follows: |
|
356 | 651 |
|
637 | 652 |
^ 2 | 3 |
653 |
Y ------ |
|
654 |
1 | 0 |
|
655 |
||
656 |
X > |
|
657 |
||
658 |
Q: what is to be returned if any coordinate is 0 ? |
|
659 |
" |
|
77 | 660 |
|
637 | 661 |
^ 0@0 quadrantContaining:self |
662 |
||
663 |
" |
|
664 |
(0@0) quadrant |
|
665 |
(1@1) quadrant |
|
666 |
(-1@1) quadrant |
|
667 |
(-1@-1) quadrant |
|
668 |
(1@-1) quadrant |
|
669 |
" |
|
356 | 670 |
! |
77 | 671 |
|
637 | 672 |
quadrantContaining:aPoint |
673 |
"return the number of the quadrant containing aPoint placing |
|
674 |
the receiver at the origin, where the quadrants are numbered as |
|
675 |
follows: |
|
676 |
^ 2 | 3 |
|
677 |
Y ------ |
|
678 |
1 | 0 |
|
679 |
||
680 |
X > |
|
681 |
This can be used for polygon operations (see Foley for examples). |
|
682 |
" |
|
683 |
||
684 |
aPoint x > x ifTrue:[ |
|
685 |
aPoint y >= y ifTrue:[^ 3]. |
|
686 |
^ 0 |
|
687 |
]. |
|
688 |
aPoint y >= y ifTrue: [^ 2]. |
|
689 |
^ 1 |
|
356 | 690 |
|
637 | 691 |
" |
692 |
(10 @ 10) quadrantContaining:(15 @ 15) |
|
693 |
(10 @ 10) quadrantContaining:(5 @ 5) |
|
694 |
(10 @ 10) quadrantContaining:(5 @ 15) |
|
695 |
(10 @ 10) quadrantContaining:(15 @ 5) |
|
696 |
" |
|
697 |
! |
|
698 |
||
3562
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
699 |
roundTo:aNumber |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
700 |
"return a new point with my coordinates rounded towards the next |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
701 |
multiple of aNumber." |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
702 |
|
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
703 |
^ (self / aNumber) rounded * aNumber |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
704 |
! |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
705 |
|
3636 | 706 |
rounded |
5361
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
707 |
"return a new point with my coordinates rounded to the next integer. |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
708 |
Return the receiver if its coordinates are already integral." |
3562
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
709 |
|
5361
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
710 |
(x isInteger and:[y isInteger]) ifTrue: [ |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
711 |
^ self |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
712 |
]. |
3636 | 713 |
^ (x rounded) @ (y rounded) |
5361
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
714 |
|
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
715 |
" |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
716 |
(1.5 @ 2.6) rounded |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
717 |
(1 @ 2) rounded |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
718 |
" |
3562
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
719 |
! |
637 | 720 |
|
3562
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
721 |
truncateTo:aNumber |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
722 |
"return a new point with my coordinates truncated towards zero to the next |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
723 |
multiple of aNumber." |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
724 |
|
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
725 |
^ (self quo:aNumber) rounded * aNumber |
3636 | 726 |
! |
727 |
||
728 |
truncated |
|
5361
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
729 |
"return a new point with my coordinates truncated as integer. |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
730 |
Return the receiver if its coordinates are already integral." |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
731 |
|
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
732 |
(x isInteger and:[y isInteger]) ifTrue: [ |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
733 |
^ self |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
734 |
]. |
3636 | 735 |
|
736 |
^ (x truncated) @ (y truncated) |
|
77 | 737 |
! ! |
738 |
||
356 | 739 |
!Point methodsFor:'point functions'! |
1 | 740 |
|
5361
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
741 |
crossProduct: aPoint |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
742 |
"Return a number that is the cross product of the receiver and the |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
743 |
argument, aPoint." |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
744 |
|
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
745 |
^ (x * aPoint y) - (y * aPoint x) |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
746 |
|
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
747 |
|
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
748 |
! |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
749 |
|
1 | 750 |
dist:aPoint |
751 |
"return the distance between aPoint and the receiver." |
|
752 |
||
753 |
^ (aPoint - self) r |
|
754 |
! |
|
755 |
||
756 |
dotProduct:aPoint |
|
57 | 757 |
"return a number that is the dot product of the receiver and |
1 | 758 |
the argument, aPoint. That is, the two points are |
57 | 759 |
multiplied and the coordinates of the result summed." |
1 | 760 |
|
761 |
|temp| |
|
762 |
||
763 |
temp := self * aPoint. |
|
764 |
^ temp x abs + temp y abs |
|
765 |
! |
|
766 |
||
767 |
grid:gridPoint |
|
768 |
"return a new point with coordinates grided (i.e. rounded to the |
|
769 |
nearest point on the grid)" |
|
770 |
||
771 |
|newX newY gridX gridY| |
|
772 |
||
773 |
gridX := gridPoint x. |
|
774 |
(gridX <= 1) ifTrue:[ |
|
180 | 775 |
newX := x asInteger |
1 | 776 |
] ifFalse:[ |
180 | 777 |
newX := ((x + (gridX // 2)) // gridX) * gridX |
1 | 778 |
]. |
779 |
gridY := gridPoint y. |
|
780 |
(gridY <= 1) ifTrue:[ |
|
180 | 781 |
newY := y asInteger |
1 | 782 |
] ifFalse:[ |
180 | 783 |
newY := ((y + (gridY // 2)) // gridY) * gridY |
1 | 784 |
]. |
785 |
^ newX @ newY |
|
77 | 786 |
! |
787 |
||
276 | 788 |
nearestIntegerPointOnLineFrom: point1 to: point2 |
789 |
"return the closest integer point to the receiver on the line |
|
790 |
determined by (point1, point2)--much faster than the more |
|
791 |
accurate version if the receiver and arguments are integer points. |
|
792 |
This method was found in the Manchester goody library." |
|
793 |
||
794 |
| dX dY newX newY dX2 dY2 intersect scale coeff | |
|
795 |
||
796 |
dX := point2 x - point1 x. |
|
797 |
dY := point2 y - point1 y. |
|
798 |
(dX = 0)ifTrue: [ |
|
799 |
(dY = 0) ifTrue: [ |
|
800 |
intersect := point1 |
|
801 |
] ifFalse: [ |
|
802 |
newX := point1 x. |
|
803 |
scale := (y - point1 y) / dY. |
|
359 | 804 |
scale > 1 ifTrue:[ |
805 |
newY := point2 y |
|
806 |
] ifFalse: [ |
|
807 |
scale < 0 ifTrue: [ |
|
808 |
newY := point1 y |
|
809 |
] ifFalse: [ |
|
810 |
newY := y |
|
811 |
] |
|
812 |
]. |
|
276 | 813 |
|
814 |
^ (newX @ newY) rounded |
|
815 |
] |
|
816 |
] ifFalse: [ |
|
817 |
(dY = 0) ifTrue: [ |
|
818 |
intersect := x @ point1 y |
|
819 |
] ifFalse:[ |
|
820 |
dX2 := dX * dX. |
|
821 |
dY2 := dY * dY. |
|
822 |
coeff := ((dX * (y - point1 y)) - |
|
823 |
((x - point1 x) * dY)) / (dX2 + dY2). |
|
824 |
newX := x + (dY * coeff). |
|
825 |
newY := y - (dX * coeff). |
|
826 |
intersect := newX @ newY |
|
827 |
] |
|
828 |
]. |
|
829 |
||
830 |
scale := (intersect x - point1 x) / dX. |
|
831 |
||
832 |
^ (scale > 1 ifTrue: [point2] ifFalse: [ |
|
833 |
scale < 0 ifTrue: [point1] ifFalse: [intersect]]) rounded |
|
834 |
||
835 |
" |
|
836 |
120@40 nearestIntegerPointOnLineFrom: 30@120 to: 100@120 |
|
837 |
0@0 nearestIntegerPointOnLineFrom: 10@10 to: 100@100 |
|
838 |
" |
|
5361
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
839 |
! |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
840 |
|
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
841 |
normalized |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
842 |
"interpreting myself as the endPoint of a 0@0 based vector, |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
843 |
return the endPoint of the corresponding normalized vector. |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
844 |
(that is the endPoint of a vector with the same direction but length 1)" |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
845 |
|
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
846 |
^ self / self r |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
847 |
|
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
848 |
" |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
849 |
(10 @ 10) normalized |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
850 |
(1 @ 1) normalized |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
851 |
(10 @ 0) normalized |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
852 |
(0 @ 10) normalized |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
853 |
(-10 @ 0) normalized |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
854 |
(0 @ -10) normalized |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
855 |
(0 @ 0) normalized |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
856 |
" |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
857 |
! |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
858 |
|
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
859 |
transposed |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
860 |
"return a new point with x and y coordinates exchanged" |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
861 |
|
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
862 |
^ y@x |
1 | 863 |
! ! |
864 |
||
356 | 865 |
!Point methodsFor:'polar coordinates'! |
866 |
||
637 | 867 |
angle |
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
868 |
"return the receiver's angle (in degrees) in a polar coordinate system. |
637 | 869 |
(i.e. the angle of a vector from 0@0 to the receiver). |
870 |
OBSOLETE ST/X interface; use theta for ST-80 compatibility." |
|
871 |
||
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
872 |
^ self theta radiansToDegrees |
637 | 873 |
|
874 |
" |
|
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
875 |
(1@1) angle |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
876 |
(2@1) angle |
637 | 877 |
" |
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
878 |
|
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
879 |
"Modified: 2.4.1997 / 00:02:17 / cg" |
637 | 880 |
! |
881 |
||
4891 | 882 |
degrees |
883 |
"return the receiver's angle (in degrees) in a polar coordinate system. |
|
884 |
(i.e. the angle of a vector from 0@0 to the receiver). |
|
885 |
Added for Squeak compatibility." |
|
886 |
||
887 |
^ self theta radiansToDegrees |
|
888 |
||
889 |
" |
|
890 |
(1@1) degrees |
|
891 |
(2@1) degrees |
|
892 |
" |
|
893 |
||
894 |
! |
|
895 |
||
356 | 896 |
r |
897 |
"return the receiver's radius in a polar coordinate system. |
|
898 |
(i.e. the length of a vector from 0@0 to the receiver)" |
|
899 |
||
2578 | 900 |
^ ((x*x) + (y*y)) sqrt |
1 | 901 |
|
356 | 902 |
" |
2578 | 903 |
(1@1) r |
904 |
(2@1) r |
|
905 |
(2@0) r |
|
906 |
(0@2) r |
|
907 |
(-2@-2) r |
|
908 |
(2@2) r |
|
356 | 909 |
" |
1 | 910 |
! |
911 |
||
356 | 912 |
theta |
913 |
"return the receiver's angle (in radians) in a polar coordinate system. |
|
914 |
(i.e. the angle of a vector from 0@0 to the receiver)" |
|
1 | 915 |
|
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
916 |
|theta t| |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
917 |
|
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
918 |
x = 0 ifTrue:[ |
5322 | 919 |
y >= 0 ifTrue:[ |
920 |
^ Float pi * 0.5 |
|
921 |
]. |
|
922 |
^ Float pi * 1.5. |
|
356 | 923 |
]. |
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
924 |
|
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
925 |
t := y asFloat / x asFloat. |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
926 |
theta := t arcTan. |
356 | 927 |
x < 0 ifTrue:[ |
5322 | 928 |
^ theta +Float pi |
356 | 929 |
]. |
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
930 |
theta < 0 ifTrue:[ |
5322 | 931 |
^ theta + (Float pi * 2.0) |
356 | 932 |
]. |
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
933 |
^ theta. |
356 | 934 |
" |
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
935 |
(1@1) theta |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
936 |
(2@1) theta |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
937 |
(-2@1) theta |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
938 |
(-2@-1) theta |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
939 |
(0@-1) theta |
356 | 940 |
" |
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
941 |
|
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
942 |
"Modified: 2.4.1997 / 00:15:12 / cg" |
1 | 943 |
! ! |
356 | 944 |
|
637 | 945 |
!Point methodsFor:'printing & storing'! |
946 |
||
947 |
printOn:aStream |
|
948 |
"append a printed representation of the receiver to aStream" |
|
949 |
||
950 |
x printOn:aStream. |
|
951 |
aStream nextPut:$@. |
|
952 |
y printOn:aStream |
|
953 |
! |
|
954 |
||
955 |
storeOn:aStream |
|
956 |
"append my storeString to aStream" |
|
957 |
||
958 |
aStream nextPut:$(. |
|
959 |
x storeOn:aStream. |
|
960 |
aStream nextPut:$@. |
|
961 |
y storeOn:aStream. |
|
962 |
aStream nextPut:$) |
|
963 |
! ! |
|
964 |
||
965 |
!Point methodsFor:'queries'! |
|
966 |
||
967 |
isPoint |
|
968 |
"return true, if the receiver is some kind of point" |
|
969 |
||
970 |
^ true |
|
971 |
! ! |
|
972 |
||
973 |
!Point methodsFor:'transformations'! |
|
974 |
||
975 |
* scale |
|
976 |
"Return a new Point that is the product of the |
|
977 |
receiver and scale (which is a Point or Number)." |
|
978 |
||
979 |
|scalePoint| |
|
980 |
||
981 |
"speedup for common cases ..." |
|
982 |
||
983 |
(scale isMemberOf:Point) ifTrue:[ |
|
5322 | 984 |
^ Point x:(x * scale x) y:(y * scale y) |
637 | 985 |
]. |
986 |
(scale isMemberOf:SmallInteger) ifTrue:[ |
|
5322 | 987 |
^ Point x:(x * scale) y:(y * scale) |
637 | 988 |
]. |
989 |
scale isNumber ifTrue:[ |
|
5322 | 990 |
^ Point x:(x * scale) y:(y * scale) |
637 | 991 |
]. |
992 |
||
993 |
"this is the general (& clean) code ..." |
|
994 |
||
995 |
scalePoint := scale asPoint. |
|
996 |
^ (x * scalePoint x) @ (y * scalePoint y) |
|
2273 | 997 |
|
998 |
"Modified: 25.1.1997 / 17:28:11 / cg" |
|
637 | 999 |
! |
1000 |
||
1001 |
+ scale |
|
1002 |
"Return a new Point that is the sum of the |
|
1003 |
receiver and scale (which is a Point or Number)." |
|
1004 |
||
1005 |
|scalePoint| |
|
1006 |
||
1007 |
"speedup for common cases ..." |
|
1008 |
||
1009 |
(scale isMemberOf:Point) ifTrue:[ |
|
5322 | 1010 |
^ Point x:(x + scale x) y:(y + scale y) |
637 | 1011 |
]. |
1012 |
(scale isMemberOf:SmallInteger) ifTrue:[ |
|
5322 | 1013 |
"/ same as below, but stc can do better here |
1014 |
^ Point x:(x + scale) y:(y + scale) |
|
637 | 1015 |
]. |
1016 |
scale isNumber ifTrue:[ |
|
5322 | 1017 |
^ Point x:(x + scale) y:(y + scale) |
637 | 1018 |
]. |
1019 |
||
1020 |
"this is the general (& clean) code ..." |
|
1021 |
||
1022 |
scalePoint := scale asPoint. |
|
1023 |
^ (x + scalePoint x) @ (y + scalePoint y) |
|
2273 | 1024 |
|
1025 |
"Modified: 25.1.1997 / 17:27:46 / cg" |
|
637 | 1026 |
! |
1027 |
||
1028 |
- scale |
|
1029 |
"Return a new Point that is the difference of the |
|
1030 |
receiver and scale (which is a Point or Number)." |
|
1031 |
||
1032 |
|scalePoint| |
|
1033 |
||
1034 |
"speedup for common cases ..." |
|
1035 |
||
1036 |
(scale isMemberOf:Point) ifTrue:[ |
|
5322 | 1037 |
^ Point x:(x - scale x) y:(y - scale y) |
637 | 1038 |
]. |
1039 |
(scale isMemberOf:SmallInteger) ifTrue:[ |
|
5322 | 1040 |
"/ same as below, but stc can do better here |
1041 |
^ Point x:(x - scale) y:(y - scale) |
|
637 | 1042 |
]. |
1043 |
scale isNumber ifTrue:[ |
|
5322 | 1044 |
^ Point x:(x - scale) y:(y - scale) |
637 | 1045 |
]. |
1046 |
||
1047 |
"this is the general (& clean) code ..." |
|
1048 |
||
1049 |
scalePoint := scale asPoint. |
|
1050 |
^ (x - scalePoint x) @ (y - scalePoint y) |
|
2273 | 1051 |
|
1052 |
"Modified: 25.1.1997 / 17:27:40 / cg" |
|
637 | 1053 |
! |
1054 |
||
1055 |
/ scale |
|
1056 |
"Return a new Point that is the integer quotient of the |
|
1057 |
receiver and scale (which is a Point or Number)." |
|
1058 |
||
1059 |
|scalePoint| |
|
1060 |
||
1061 |
"speedup for common cases ..." |
|
1062 |
||
1063 |
(scale isMemberOf:Point) ifTrue:[ |
|
1064 |
^ (x / scale x) @ (y / scale y) |
|
1065 |
]. |
|
1066 |
scale isNumber ifTrue:[ |
|
1067 |
^ (x / scale) @ (y / scale) |
|
1068 |
]. |
|
1069 |
||
1070 |
"this is the general (& clean) code ..." |
|
1071 |
||
1072 |
scalePoint := scale asPoint. |
|
1073 |
^ (x / scalePoint x) @ (y / scalePoint y) |
|
1074 |
! |
|
1075 |
||
1076 |
// scale |
|
1077 |
"Return a new Point that is the quotient of the |
|
1078 |
receiver and scale (which is a Point or Number)." |
|
1079 |
||
1080 |
|scalePoint| |
|
1081 |
||
1082 |
scalePoint := scale asPoint. |
|
1083 |
^ (x // scalePoint x) @ (y // scalePoint y) |
|
1084 |
! |
|
1085 |
||
1086 |
negated |
|
1087 |
"return a new point with my coordinates negated |
|
1088 |
i.e. the receiver mirrored at the origin" |
|
1089 |
||
1090 |
^ (x negated) @ (y negated) |
|
1091 |
! |
|
1092 |
||
1093 |
reciprocal |
|
1094 |
"return a new point where the coordinates are |
|
1095 |
the reciproce of mine" |
|
1096 |
||
1097 |
^ (1 / x) @ (1 / y) |
|
1098 |
! |
|
1099 |
||
4893 | 1100 |
rotateBy:angle about:center |
1101 |
"Return a new point, generated by rotating the receiver |
|
1102 |
counterClockWise by some angle in radians around the given center point. |
|
1103 |
Even though Point.theta is measured CW, |
|
1104 |
this rotates with the more conventional CCW interpretateion of angle." |
|
1105 |
||
1106 |
|p r theta| |
|
1107 |
||
1108 |
p := self - center. |
|
1109 |
r := p r. |
|
1110 |
theta := angle asFloat - p theta. |
|
1111 |
^ (center x asFloat + (r * theta cos)) @ |
|
1112 |
(center y asFloat - (r * theta sin)) |
|
1113 |
||
1114 |
" |
|
1115 |
(10@10) rotateBy:Float pi about:0@0 |
|
1116 |
(10@0) rotateBy:Float pi about:0@0 |
|
1117 |
" |
|
1118 |
! |
|
1119 |
||
637 | 1120 |
scaledBy:aScale |
1121 |
"return a new Point that is the product of the |
|
1122 |
receiver and scale (which is a Point or Number)." |
|
1123 |
||
1124 |
^ self * aScale |
|
1125 |
! |
|
1126 |
||
1127 |
translatedBy:anOffset |
|
1128 |
"return a new Point that is the sum of the |
|
1129 |
receiver and scale (which is a Point or Number)." |
|
1130 |
||
1131 |
^ self + anOffset |
|
1132 |
! ! |
|
1133 |
||
2273 | 1134 |
!Point class methodsFor:'documentation'! |
637 | 1135 |
|
1136 |
version |
|
5567 | 1137 |
^ '$Header: /cvs/stx/stx/libbasic/Point.st,v 1.56 2000-08-30 12:35:40 stefan Exp $' |
637 | 1138 |
! ! |
356 | 1139 |
Point initialize! |