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