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