author | Stefan Vogel <sv@exept.de> |
Fri, 19 Jan 2018 10:40:38 +0100 | |
changeset 22458 | fc3246d004fa |
parent 22291 | ff5f835ac482 |
child 22635 | f9320ed5c7b8 |
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 |
22291 | 374 |
"return true if the receiver is below and to the right |
637 | 375 |
of the argument, aPoint" |
376 |
||
377 |
|p| |
|
356 | 378 |
|
637 | 379 |
p := aPoint asPoint. |
1339 | 380 |
(p x) < x ifTrue:[ |
22291 | 381 |
(p y) < y ifTrue:[^ true]. |
1339 | 382 |
]. |
383 |
^ false |
|
384 |
||
22291 | 385 |
"Modified: / 07-05-1996 / 12:11:15 / cg" |
386 |
"Modified (comment): / 27-09-2017 / 15:55:14 / mawalch" |
|
637 | 387 |
! |
356 | 388 |
|
637 | 389 |
hash |
390 |
"return a number for hashing" |
|
356 | 391 |
|
637 | 392 |
x = y ifTrue:[^ x hash]. |
393 |
" |
|
394 |
used to be: |
|
395 |
^ (x hash) bitXor:(y hash) |
|
396 |
the following handles 1@x vs. x@1 better: |
|
397 |
" |
|
4857 | 398 |
^ (x hash) bitXor:(y hash bitShift:16) |
399 |
||
400 |
||
401 |
||
402 |
||
403 |
||
637 | 404 |
! |
356 | 405 |
|
10882 | 406 |
isLeftOrAbove:aPoint |
407 |
"return true if the receiver is above or to the left |
|
408 |
of the argument, aPoint. |
|
409 |
When sorting this enumerates points from left to right and top to bottom" |
|
410 |
||
411 |
|p| |
|
412 |
||
413 |
p := aPoint asPoint. |
|
414 |
^ (y < p y) or:[(y = p y) and:[x < p x]] |
|
415 |
! |
|
416 |
||
637 | 417 |
max:aPoint |
418 |
"return the lower right corner of the rectangle uniquely defined by |
|
419 |
the receiver and the argument, aPoint" |
|
420 |
||
421 |
|p maxX maxY| |
|
356 | 422 |
|
637 | 423 |
p := aPoint asPoint. |
424 |
maxX := x max:(p x). |
|
425 |
maxY := y max:(p y). |
|
426 |
^ maxX @ maxY |
|
427 |
! |
|
428 |
||
429 |
min:aPoint |
|
430 |
"return the upper left corner of the rectangle uniquely defined by |
|
431 |
the receiver and the argument, aPoint" |
|
432 |
||
433 |
|p minX minY| |
|
434 |
||
435 |
p := aPoint asPoint. |
|
436 |
minX := x min:(p x). |
|
437 |
minY := y min:(p y). |
|
438 |
^ minX @ minY |
|
356 | 439 |
! ! |
440 |
||
441 |
!Point methodsFor:'converting'! |
|
442 |
||
3636 | 443 |
asComplex |
444 |
"Return a complex number whose real and imaginary components are the x and y |
|
445 |
coordinates of the receiver." |
|
446 |
||
7360 | 447 |
^ Complex real:x imaginary:y |
3636 | 448 |
|
449 |
"Modified: / 9.7.1998 / 10:21:10 / cg" |
|
450 |
! |
|
451 |
||
637 | 452 |
asFractionalLayout |
453 |
"return a LayoutOrigin from the receiver, |
|
20308 | 454 |
treating the receiver coordinates as fractional parts |
637 | 455 |
(i.e. relative to superview). |
456 |
Notice: in 10.5.x LayoutOrigin is not yet officially released." |
|
457 |
||
458 |
^ LayoutOrigin fractionalFromPoint:self |
|
356 | 459 |
|
637 | 460 |
" |
20308 | 461 |
(0@0.5) asFractionalLayout |
462 |
(0@0.5) asLayout |
|
463 |
(0@10) asLayout |
|
464 |
(0@10) asOffsetLayout |
|
637 | 465 |
" |
356 | 466 |
|
467 |
! |
|
1 | 468 |
|
4865 | 469 |
asIntegerPoint |
470 |
(x isInteger and:[y isInteger]) ifTrue:[ ^ self]. |
|
471 |
^ x asInteger @ y asInteger |
|
472 |
! |
|
473 |
||
356 | 474 |
asLayout |
475 |
"return a LayoutOrigin from the receiver. |
|
476 |
If the receiver coordinates are between 0 and 1, take |
|
477 |
them as fractional parts (relative to superview). |
|
478 |
Otherwise, treat them as absolute offsets. |
|
479 |
Notice: in 10.5.x LayoutOrigin is not yet released." |
|
480 |
||
481 |
^ LayoutOrigin fromPoint:self |
|
482 |
||
483 |
" |
|
20308 | 484 |
(0@0.5) asFractionalLayout |
485 |
(0@0.5) asLayout |
|
486 |
(0@10) asLayout |
|
487 |
(0@10) asOffsetLayout |
|
356 | 488 |
" |
489 |
||
490 |
! |
|
491 |
||
492 |
asOffsetLayout |
|
493 |
"return a LayoutOrigin from the receiver, |
|
20308 | 494 |
treating the receiver coordinates as absolute offsets. |
356 | 495 |
Notice: in 10.5.x LayoutOrigin is not yet released." |
496 |
||
497 |
^ LayoutOrigin offsetFromPoint:self |
|
498 |
||
499 |
" |
|
20308 | 500 |
(0@0.5) asFractionalLayout |
501 |
(0@0.5) asLayout |
|
502 |
(0@10) asLayout |
|
503 |
(0@10) asOffsetLayout |
|
356 | 504 |
" |
505 |
||
1 | 506 |
! |
507 |
||
637 | 508 |
asPoint |
509 |
"return the receiver as Point - this is the receiver" |
|
356 | 510 |
|
637 | 511 |
^ self |
356 | 512 |
! |
513 |
||
514 |
asRectangle |
|
20308 | 515 |
"return a zero-width rectangle consisting of origin |
356 | 516 |
and corner being the receiver" |
517 |
||
518 |
^ self corner:self |
|
519 |
||
520 |
" |
|
20308 | 521 |
(0@10) asRectangle |
356 | 522 |
" |
523 |
! |
|
524 |
||
637 | 525 |
corner:aPoint |
526 |
"return a rectangle whose origin is self and corner is aPoint" |
|
527 |
||
528 |
^ Rectangle origin:self corner:aPoint |
|
529 |
! |
|
530 |
||
531 |
extent:aPoint |
|
532 |
"return a rectangle whose origin is self and extent is aPoint" |
|
356 | 533 |
|
637 | 534 |
^ Rectangle origin:self extent:aPoint |
535 |
! |
|
536 |
||
537 |
fromLiteralArrayEncoding:encoding |
|
538 |
"read my values from an encoding. |
|
539 |
The encoding is supposed to be of the form: (Point xValue yValue)" |
|
540 |
||
541 |
x := encoding at:2. |
|
542 |
y := encoding at:3. |
|
356 | 543 |
|
544 |
" |
|
637 | 545 |
Point new fromLiteralArrayEncoding:#(Point 10 20) |
356 | 546 |
" |
379 | 547 |
! |
548 |
||
421 | 549 |
literalArrayEncoding |
1245 | 550 |
"encode myself as an array, from which a copy of the receiver |
551 |
can be reconstructed with #decodeAsLiteralArray. |
|
421 | 552 |
The encoding is: (Point xValue yValue)" |
553 |
||
554 |
^ Array |
|
20308 | 555 |
with:#Point |
556 |
with:x |
|
557 |
with:y |
|
421 | 558 |
|
559 |
||
560 |
" |
|
20308 | 561 |
Point new fromLiteralArrayEncoding:#(Point 10 20) |
562 |
(10@20) literalArrayEncoding |
|
421 | 563 |
" |
564 |
||
565 |
"Modified: 1.9.1995 / 02:18:29 / claus" |
|
1245 | 566 |
"Modified: 22.4.1996 / 13:00:32 / cg" |
421 | 567 |
! |
568 |
||
3488
6e1feb9ead68
prefRect may now be a valueHolder or block
Claus Gittinger <cg@exept.de>
parents:
3238
diff
changeset
|
569 |
rectangleRelativeTo:aRectangle preferred:prefRectHolder |
637 | 570 |
"compute a displayRectangle, treating the receiver like a |
571 |
layoutorigin. This allows point to be used interchangable with |
|
572 |
LayoutOrigins." |
|
379 | 573 |
|
3488
6e1feb9ead68
prefRect may now be a valueHolder or block
Claus Gittinger <cg@exept.de>
parents:
3238
diff
changeset
|
574 |
^ (self asLayout) rectangleRelativeTo:aRectangle preferred:prefRectHolder |
379 | 575 |
|
576 |
" |
|
637 | 577 |
consider the case, where a view has a preferred extent of 50@50 |
578 |
and is to be positioned in its superview which has size 100@100. |
|
579 |
For absolute origin: |
|
20308 | 580 |
(10@20) rectangleRelativeTo:(0@0 corner:100@100) preferred:(0@0 corner:50@50) |
1 | 581 |
|
637 | 582 |
for relative origin: |
20308 | 583 |
(0.5@0.5) rectangleRelativeTo:(0@0 corner:100@100) preferred:(0@0 corner:50@50) |
637 | 584 |
" |
3488
6e1feb9ead68
prefRect may now be a valueHolder or block
Claus Gittinger <cg@exept.de>
parents:
3238
diff
changeset
|
585 |
|
6e1feb9ead68
prefRect may now be a valueHolder or block
Claus Gittinger <cg@exept.de>
parents:
3238
diff
changeset
|
586 |
"Modified: / 27.5.1998 / 10:20:13 / cg" |
1 | 587 |
! ! |
588 |
||
77 | 589 |
!Point methodsFor:'destructive transformations'! |
590 |
||
591 |
scaleBy:aScale |
|
592 |
"scale the receiver, by replacing coordinates by the product |
|
19470 | 593 |
of the receiver's coordinates and the scale (a Point or Number). |
20308 | 594 |
This is destructive (modifies the receiver, not a copy) and |
595 |
should only be used if you know, that you are the exclusive owner |
|
180 | 596 |
of the receiver." |
77 | 597 |
|
598 |
|scalePoint| |
|
599 |
||
20308 | 600 |
(aScale isMemberOf:Point) ifTrue:[ "type hint to stc" |
601 |
x := x * aScale x. |
|
602 |
y := y * aScale y. |
|
603 |
^ self |
|
77 | 604 |
]. |
605 |
aScale isNumber ifTrue:[ |
|
20308 | 606 |
x := x * aScale. |
607 |
y := y * aScale. |
|
608 |
^ self |
|
77 | 609 |
]. |
610 |
||
611 |
"this is the general (& clean) code ..." |
|
612 |
||
613 |
scalePoint := aScale asPoint. |
|
614 |
x := x * scalePoint x. |
|
615 |
y := y * scalePoint y |
|
616 |
! |
|
617 |
||
618 |
translateBy:anOffset |
|
619 |
"translate the receiver, by replacing coordinates by the sum |
|
19470 | 620 |
of the receiver's coordinated and the scale (a Point or Number). |
20308 | 621 |
This is destructive (modifies the receiver, not a copy) and |
622 |
should only be used if you know, that you are the exclusive owner |
|
180 | 623 |
of the receiver." |
77 | 624 |
|
625 |
|offsetPoint| |
|
626 |
||
20308 | 627 |
(anOffset isMemberOf:Point) ifTrue:[ "type hint to stc" |
628 |
x := x + anOffset x. |
|
629 |
y := y + anOffset y. |
|
630 |
^ self |
|
77 | 631 |
]. |
632 |
anOffset isNumber ifTrue:[ |
|
20308 | 633 |
x := x + anOffset. |
634 |
y := y + anOffset. |
|
635 |
^ self |
|
77 | 636 |
]. |
637 |
||
638 |
"this is the general (& clean) code ..." |
|
639 |
||
640 |
offsetPoint := anOffset asPoint. |
|
641 |
x := x + anOffset x. |
|
642 |
y := y + anOffset y |
|
643 |
! ! |
|
644 |
||
18840 | 645 |
|
5361
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
646 |
!Point methodsFor:'interpolating'! |
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 |
interpolateTo: end at: amountDone |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
649 |
"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
|
650 |
|
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
651 |
^ self + ((end - self) * amountDone). |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
652 |
|
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
653 |
" |
20308 | 654 |
(10@10) interpolateTo:(20@20) at:0.5 |
655 |
(10@10) interpolateTo:(20@20) at:0.3 |
|
656 |
(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
|
657 |
" |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
658 |
! ! |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
659 |
|
637 | 660 |
!Point methodsFor:'misc'! |
661 |
||
662 |
abs |
|
663 |
"return a new point with my coordinates taken from the absolute values." |
|
664 |
||
18891 | 665 |
^ self class x:(x abs) y:(y abs) |
637 | 666 |
! |
667 |
||
3562
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
668 |
ceiling |
5361
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
669 |
"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
|
670 |
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
|
671 |
|
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
672 |
(x isInteger and:[y isInteger]) ifTrue: [ |
21430 | 673 |
^ self |
5361
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
674 |
]. |
3562
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
675 |
|
18891 | 676 |
^ self class x:x ceiling y:y ceiling. |
21430 | 677 |
|
678 |
"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
|
679 |
! |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
680 |
|
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
681 |
floor |
5361
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
682 |
"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
|
683 |
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
|
684 |
|
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
685 |
(x isInteger and:[y isInteger]) ifTrue: [ |
20308 | 686 |
^ self |
5361
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
687 |
]. |
3562
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
688 |
|
18891 | 689 |
^ self class x:(x floor) y:(y floor) |
3562
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
690 |
! |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
691 |
|
637 | 692 |
quadrant |
693 |
"return the number of the quadrant containing the receiver. |
|
694 |
quadrants are named as follows: |
|
356 | 695 |
|
20308 | 696 |
^ 2 | 3 |
697 |
Y ------ |
|
698 |
1 | 0 |
|
637 | 699 |
|
20308 | 700 |
X > |
637 | 701 |
|
702 |
Q: what is to be returned if any coordinate is 0 ? |
|
703 |
" |
|
77 | 704 |
|
637 | 705 |
^ 0@0 quadrantContaining:self |
706 |
||
707 |
" |
|
20308 | 708 |
(1@1) quadrant |
709 |
(-1@1) quadrant |
|
710 |
(-1@-1) quadrant |
|
711 |
(1@-1) quadrant |
|
712 |
(0@0) quadrant |
|
637 | 713 |
" |
356 | 714 |
! |
77 | 715 |
|
637 | 716 |
quadrantContaining:aPoint |
20308 | 717 |
"return the number of the quadrant containing aPoint placing |
718 |
the receiver at the origin, where the quadrants are numbered as |
|
637 | 719 |
follows: |
720 |
^ 2 | 3 |
|
721 |
Y ------ |
|
722 |
1 | 0 |
|
723 |
||
724 |
X > |
|
725 |
This can be used for polygon operations (see Foley for examples). |
|
726 |
" |
|
727 |
||
728 |
aPoint x > x ifTrue:[ |
|
729 |
aPoint y >= y ifTrue:[^ 3]. |
|
730 |
^ 0 |
|
731 |
]. |
|
20308 | 732 |
aPoint y >= y ifTrue: [^ 2]. |
637 | 733 |
^ 1 |
356 | 734 |
|
637 | 735 |
" |
736 |
(10 @ 10) quadrantContaining:(15 @ 15) |
|
20308 | 737 |
(10 @ 10) quadrantContaining:(5 @ 5) |
738 |
(10 @ 10) quadrantContaining:(5 @ 15) |
|
739 |
(10 @ 10) quadrantContaining:(15 @ 5) |
|
637 | 740 |
" |
741 |
! |
|
742 |
||
3636 | 743 |
rounded |
5361
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
744 |
"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
|
745 |
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
|
746 |
|
5361
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
747 |
(x isInteger and:[y isInteger]) ifTrue: [ |
20308 | 748 |
^ self |
5361
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
749 |
]. |
18891 | 750 |
^ 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
|
751 |
|
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
752 |
" |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
753 |
(1.5 @ 2.6) rounded |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
754 |
(1 @ 2) rounded |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
755 |
" |
3562
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
756 |
! |
637 | 757 |
|
3562
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
758 |
truncateTo:aNumber |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
759 |
"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
|
760 |
multiple of aNumber." |
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
761 |
|
813c3f1e99d7
more point truncation-type messages.
Claus Gittinger <cg@exept.de>
parents:
3488
diff
changeset
|
762 |
^ (self quo:aNumber) rounded * aNumber |
3636 | 763 |
! |
764 |
||
765 |
truncated |
|
5361
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
766 |
"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
|
767 |
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
|
768 |
|
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
769 |
(x isInteger and:[y isInteger]) ifTrue: [ |
20308 | 770 |
^ self |
5361
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
771 |
]. |
3636 | 772 |
|
18891 | 773 |
^ self class x:(x truncated) y:(y truncated) |
77 | 774 |
! ! |
775 |
||
356 | 776 |
!Point methodsFor:'point functions'! |
1 | 777 |
|
20308 | 778 |
crossProduct: aPoint |
779 |
"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
|
780 |
argument, aPoint." |
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 |
^ (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
|
783 |
|
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
784 |
|
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
785 |
! |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
786 |
|
20308 | 787 |
dist:aPoint |
1 | 788 |
"return the distance between aPoint and the receiver." |
789 |
||
790 |
^ (aPoint - self) r |
|
791 |
! |
|
792 |
||
20308 | 793 |
dotProduct:aPoint |
57 | 794 |
"return a number that is the dot product of the receiver and |
1 | 795 |
the argument, aPoint. That is, the two points are |
57 | 796 |
multiplied and the coordinates of the result summed." |
1 | 797 |
|
8451 | 798 |
^ (x * aPoint x) + (y * aPoint y) |
1 | 799 |
! |
800 |
||
8452 | 801 |
fourNeighbors |
20308 | 802 |
^ Array |
22040 | 803 |
with: self + (1 @ 0) |
804 |
with: self + (0 @ 1) |
|
805 |
with: self + (-1 @ 0) |
|
806 |
with: self + (0 @ -1) |
|
807 |
||
808 |
"Modified (format): / 17-07-2017 / 14:20:40 / cg" |
|
8452 | 809 |
! |
810 |
||
1 | 811 |
grid:gridPoint |
812 |
"return a new point with coordinates grided (i.e. rounded to the |
|
813 |
nearest point on the grid)" |
|
814 |
||
815 |
|newX newY gridX gridY| |
|
816 |
||
817 |
gridX := gridPoint x. |
|
818 |
(gridX <= 1) ifTrue:[ |
|
180 | 819 |
newX := x asInteger |
1 | 820 |
] ifFalse:[ |
180 | 821 |
newX := ((x + (gridX // 2)) // gridX) * gridX |
1 | 822 |
]. |
823 |
gridY := gridPoint y. |
|
824 |
(gridY <= 1) ifTrue:[ |
|
180 | 825 |
newY := y asInteger |
1 | 826 |
] ifFalse:[ |
180 | 827 |
newY := ((y + (gridY // 2)) // gridY) * gridY |
1 | 828 |
]. |
829 |
^ newX @ newY |
|
77 | 830 |
! |
831 |
||
20308 | 832 |
nearestIntegerPointOnLineFrom: point1 to: point2 |
833 |
"return the closest integer point to the receiver on the line |
|
834 |
determined by (point1, point2)--much faster than the more |
|
276 | 835 |
accurate version if the receiver and arguments are integer points. |
836 |
This method was found in the Manchester goody library." |
|
837 |
||
838 |
| dX dY newX newY dX2 dY2 intersect scale coeff | |
|
839 |
||
840 |
dX := point2 x - point1 x. |
|
841 |
dY := point2 y - point1 y. |
|
842 |
(dX = 0)ifTrue: [ |
|
843 |
(dY = 0) ifTrue: [ |
|
844 |
intersect := point1 |
|
845 |
] ifFalse: [ |
|
846 |
newX := point1 x. |
|
847 |
scale := (y - point1 y) / dY. |
|
359 | 848 |
scale > 1 ifTrue:[ |
849 |
newY := point2 y |
|
850 |
] ifFalse: [ |
|
851 |
scale < 0 ifTrue: [ |
|
852 |
newY := point1 y |
|
853 |
] ifFalse: [ |
|
854 |
newY := y |
|
855 |
] |
|
856 |
]. |
|
276 | 857 |
|
858 |
^ (newX @ newY) rounded |
|
859 |
] |
|
860 |
] ifFalse: [ |
|
861 |
(dY = 0) ifTrue: [ |
|
862 |
intersect := x @ point1 y |
|
863 |
] ifFalse:[ |
|
864 |
dX2 := dX * dX. |
|
865 |
dY2 := dY * dY. |
|
20308 | 866 |
coeff := ((dX * (y - point1 y)) - |
276 | 867 |
((x - point1 x) * dY)) / (dX2 + dY2). |
868 |
newX := x + (dY * coeff). |
|
869 |
newY := y - (dX * coeff). |
|
870 |
intersect := newX @ newY |
|
871 |
] |
|
872 |
]. |
|
873 |
||
874 |
scale := (intersect x - point1 x) / dX. |
|
875 |
||
876 |
^ (scale > 1 ifTrue: [point2] ifFalse: [ |
|
877 |
scale < 0 ifTrue: [point1] ifFalse: [intersect]]) rounded |
|
878 |
||
879 |
" |
|
20308 | 880 |
120@40 nearestIntegerPointOnLineFrom: 30@120 to: 100@120 |
881 |
0@0 nearestIntegerPointOnLineFrom: 10@10 to: 100@100 |
|
276 | 882 |
" |
5361
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
883 |
! |
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 |
normalized |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
886 |
"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
|
887 |
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
|
888 |
(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
|
889 |
|
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
890 |
^ self / self r |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
891 |
|
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
892 |
" |
20308 | 893 |
(10 @ 10) normalized |
894 |
(1 @ 1) normalized |
|
895 |
(10 @ 0) normalized |
|
896 |
(0 @ 10) normalized |
|
897 |
(-10 @ 0) normalized |
|
898 |
(0 @ -10) normalized |
|
899 |
(0 @ 0) normalized |
|
5361
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 |
! |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
902 |
|
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
903 |
transposed |
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
904 |
"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
|
905 |
|
dbf17a65f6bd
more point functions; better #rounded, #truncated, #floor and #ceiling
Claus Gittinger <cg@exept.de>
parents:
5322
diff
changeset
|
906 |
^ y@x |
1 | 907 |
! ! |
908 |
||
356 | 909 |
!Point methodsFor:'polar coordinates'! |
910 |
||
20308 | 911 |
angle |
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
912 |
"return the receiver's angle (in degrees) in a polar coordinate system. |
637 | 913 |
(i.e. the angle of a vector from 0@0 to the receiver). |
914 |
OBSOLETE ST/X interface; use theta for ST-80 compatibility." |
|
915 |
||
8198
c5f599f796ab
Use #theta inseat of obsolete #angle
Stefan Vogel <sv@exept.de>
parents:
8076
diff
changeset
|
916 |
<resource:#obsolete> |
c5f599f796ab
Use #theta inseat of obsolete #angle
Stefan Vogel <sv@exept.de>
parents:
8076
diff
changeset
|
917 |
|
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
918 |
^ self theta radiansToDegrees |
637 | 919 |
|
920 |
" |
|
20308 | 921 |
(1@1) angle |
922 |
(1@0) angle |
|
923 |
(2@1) angle |
|
637 | 924 |
" |
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
925 |
|
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
926 |
"Modified: 2.4.1997 / 00:02:17 / cg" |
637 | 927 |
! |
928 |
||
4891 | 929 |
degrees |
930 |
"return the receiver's angle (in degrees) in a polar coordinate system. |
|
931 |
(i.e. the angle of a vector from 0@0 to the receiver). |
|
11404 | 932 |
The angle is counted counter-clock-wise, starting with 0 for a horizontal |
933 |
line (i.e. 0@0 -> 100@0 has an angle of 0 and 0@0 -> 0@100 has an angle of 90). |
|
934 |
Added for Squeak compatibility." |
|
4891 | 935 |
|
936 |
^ self theta radiansToDegrees |
|
937 |
||
938 |
" |
|
20308 | 939 |
(1@1) degrees |
940 |
(2@1) degrees |
|
4891 | 941 |
" |
942 |
! |
|
943 |
||
356 | 944 |
r |
945 |
"return the receiver's radius in a polar coordinate system. |
|
946 |
(i.e. the length of a vector from 0@0 to the receiver)" |
|
947 |
||
2578 | 948 |
^ ((x*x) + (y*y)) sqrt |
1 | 949 |
|
356 | 950 |
" |
20308 | 951 |
(1@1) r |
952 |
(2@1) r |
|
953 |
(2@0) r |
|
954 |
(0@2) r |
|
955 |
(-2@-2) r |
|
956 |
(2@2) r |
|
356 | 957 |
" |
1 | 958 |
! |
959 |
||
20308 | 960 |
theta |
356 | 961 |
"return the receiver's angle (in radians) in a polar coordinate system. |
962 |
(i.e. the angle of a vector from 0@0 to the receiver)" |
|
1 | 963 |
|
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
964 |
|theta t| |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
965 |
|
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
966 |
x = 0 ifTrue:[ |
20308 | 967 |
y >= 0 ifTrue:[ |
968 |
^ Float pi * 0.5 |
|
969 |
]. |
|
970 |
^ Float pi * 1.5. |
|
356 | 971 |
]. |
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
972 |
|
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
973 |
t := y asFloat / x asFloat. |
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
974 |
theta := t arcTan. |
356 | 975 |
x < 0 ifTrue:[ |
20308 | 976 |
^ theta + Float pi |
356 | 977 |
]. |
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
978 |
theta < 0 ifTrue:[ |
20308 | 979 |
^ theta + (Float pi * 2.0) |
356 | 980 |
]. |
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
981 |
^ theta. |
356 | 982 |
" |
20308 | 983 |
(1@1) theta |
984 |
(2@1) theta |
|
985 |
(-2@1) theta |
|
986 |
(-2@-1) theta |
|
987 |
(0@-1) theta |
|
356 | 988 |
" |
2517
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
989 |
|
e8d65844587e
fixed radians/degrees confusion in r:angle: / r:theta
Claus Gittinger <cg@exept.de>
parents:
2273
diff
changeset
|
990 |
"Modified: 2.4.1997 / 00:15:12 / cg" |
1 | 991 |
! ! |
356 | 992 |
|
637 | 993 |
!Point methodsFor:'printing & storing'! |
994 |
||
995 |
printOn:aStream |
|
996 |
"append a printed representation of the receiver to aStream" |
|
997 |
||
998 |
x printOn:aStream. |
|
999 |
aStream nextPut:$@. |
|
1000 |
y printOn:aStream |
|
1001 |
! |
|
1002 |
||
1003 |
storeOn:aStream |
|
1004 |
"append my storeString to aStream" |
|
1005 |
||
1006 |
aStream nextPut:$(. |
|
1007 |
x storeOn:aStream. |
|
1008 |
aStream nextPut:$@. |
|
1009 |
y storeOn:aStream. |
|
1010 |
aStream nextPut:$) |
|
1011 |
! ! |
|
1012 |
||
1013 |
!Point methodsFor:'queries'! |
|
1014 |
||
1015 |
isPoint |
|
1016 |
"return true, if the receiver is some kind of point" |
|
1017 |
||
1018 |
^ true |
|
1019 |
! ! |
|
1020 |
||
7450 | 1021 |
!Point methodsFor:'testing'! |
1022 |
||
1023 |
isFinite |
|
1024 |
^x isFinite and: [y isFinite] |
|
1025 |
! |
|
1026 |
||
1027 |
isInfinite |
|
1028 |
^x isInfinite or: [y isInfinite] |
|
1029 |
! ! |
|
1030 |
||
637 | 1031 |
!Point methodsFor:'transformations'! |
1032 |
||
20308 | 1033 |
* scale |
1034 |
"Return a new Point that is the product of the |
|
637 | 1035 |
receiver and scale (which is a Point or Number)." |
1036 |
||
1037 |
|scalePoint| |
|
1038 |
||
1039 |
"speedup for common cases ..." |
|
1040 |
||
20308 | 1041 |
(scale isMemberOf:Point) ifTrue:[ |
1042 |
^ self class x:(x * scale x) y:(y * scale y) |
|
637 | 1043 |
]. |
1044 |
(scale isMemberOf:SmallInteger) ifTrue:[ |
|
20308 | 1045 |
^ self class x:(x * scale) y:(y * scale) |
637 | 1046 |
]. |
1047 |
scale isNumber ifTrue:[ |
|
20308 | 1048 |
^ self class x:(x * scale) y:(y * scale) |
637 | 1049 |
]. |
1050 |
||
1051 |
"this is the general (& clean) code ..." |
|
1052 |
||
1053 |
scalePoint := scale asPoint. |
|
18891 | 1054 |
^ self class x:(x * scalePoint x) y:(y * scalePoint y) |
2273 | 1055 |
|
1056 |
"Modified: 25.1.1997 / 17:28:11 / cg" |
|
637 | 1057 |
! |
1058 |
||
20308 | 1059 |
+ translation |
1060 |
"Return a new Point that is the sum of the |
|
18891 | 1061 |
receiver and translation (which is a Point or Number)." |
637 | 1062 |
|
18891 | 1063 |
|translationPoint| |
637 | 1064 |
|
1065 |
"speedup for common cases ..." |
|
1066 |
||
20308 | 1067 |
(translation isMemberOf:Point) ifTrue:[ |
1068 |
^ self class x:(x + translation x) y:(y + translation y) |
|
637 | 1069 |
]. |
18891 | 1070 |
(translation isMemberOf:SmallInteger) ifTrue:[ |
20308 | 1071 |
"/ same as below, but stc can do better here |
1072 |
^ self class x:(x + translation) y:(y + translation) |
|
637 | 1073 |
]. |
18891 | 1074 |
translation isNumber ifTrue:[ |
20308 | 1075 |
^ self class x:(x + translation) y:(y + translation) |
637 | 1076 |
]. |
1077 |
||
1078 |
"this is the general (& clean) code ..." |
|
1079 |
||
18891 | 1080 |
translationPoint := translation asPoint. |
1081 |
^ self class x:(x + translationPoint x) y:(y + translationPoint y). |
|
2273 | 1082 |
|
1083 |
"Modified: 25.1.1997 / 17:27:46 / cg" |
|
637 | 1084 |
! |
1085 |
||
20308 | 1086 |
- translation |
1087 |
"Return a new Point that is the difference of the |
|
18891 | 1088 |
receiver and translation (which is a Point or Number)." |
637 | 1089 |
|
18891 | 1090 |
|translationPoint| |
637 | 1091 |
|
1092 |
"speedup for common cases ..." |
|
1093 |
||
20308 | 1094 |
(translation isMemberOf:Point) ifTrue:[ |
1095 |
^ self class x:(x - translation x) y:(y - translation y) |
|
637 | 1096 |
]. |
18891 | 1097 |
(translation isMemberOf:SmallInteger) ifTrue:[ |
20308 | 1098 |
"/ same as below, but stc can do better here |
1099 |
^ self class x:(x - translation) y:(y - translation) |
|
637 | 1100 |
]. |
18891 | 1101 |
translation isNumber ifTrue:[ |
20308 | 1102 |
^ self class x:(x - translation) y:(y - translation) |
637 | 1103 |
]. |
1104 |
||
1105 |
"this is the general (& clean) code ..." |
|
1106 |
||
18891 | 1107 |
translationPoint := translation asPoint. |
1108 |
^ self class x:(x - translationPoint x) y:(y - translationPoint y). |
|
2273 | 1109 |
|
18891 | 1110 |
"Modified: 25.1.1997 / 17:27:46 / cg" |
637 | 1111 |
! |
1112 |
||
20308 | 1113 |
/ scale |
1114 |
"Return a new Point that is the integer quotient of the |
|
637 | 1115 |
receiver and scale (which is a Point or Number)." |
1116 |
||
1117 |
|scalePoint| |
|
1118 |
||
1119 |
"speedup for common cases ..." |
|
1120 |
||
20308 | 1121 |
(scale isMemberOf:Point) ifTrue:[ |
1122 |
self class x:(x / scale x) y:(y / scale y) |
|
637 | 1123 |
]. |
1124 |
scale isNumber ifTrue:[ |
|
20308 | 1125 |
^ self class x:(x / scale) y:(y / scale) |
637 | 1126 |
]. |
1127 |
||
1128 |
"this is the general (& clean) code ..." |
|
1129 |
||
1130 |
scalePoint := scale asPoint. |
|
18891 | 1131 |
^ self class x:(x / scalePoint x) y:(y / scalePoint y) |
637 | 1132 |
! |
1133 |
||
20308 | 1134 |
// scale |
1135 |
"Return a new Point that is the quotient of the |
|
637 | 1136 |
receiver and scale (which is a Point or Number)." |
1137 |
||
1138 |
|scalePoint| |
|
1139 |
||
1140 |
scalePoint := scale asPoint. |
|
18891 | 1141 |
^ self class x:(x // scalePoint x) y:(y // scalePoint y) |
637 | 1142 |
! |
1143 |
||
1144 |
negated |
|
20308 | 1145 |
"return a new point with my coordinates negated |
637 | 1146 |
i.e. the receiver mirrored at the origin" |
1147 |
||
18891 | 1148 |
^ self class x:x negated y:y negated |
1149 |
||
1150 |
" |
|
20308 | 1151 |
(1 @ 1) negated |
18891 | 1152 |
" |
637 | 1153 |
! |
1154 |
||
1155 |
reciprocal |
|
1156 |
"return a new point where the coordinates are |
|
1157 |
the reciproce of mine" |
|
1158 |
||
18891 | 1159 |
^ self class x:(1 / x) y:(1 / y) |
637 | 1160 |
! |
1161 |
||
4893 | 1162 |
rotateBy:angle about:center |
1163 |
"Return a new point, generated by rotating the receiver |
|
1164 |
counterClockWise by some angle in radians around the given center point. |
|
20308 | 1165 |
Even though Point.theta is measured CW, |
4893 | 1166 |
this rotates with the more conventional CCW interpretateion of angle." |
1167 |
||
1168 |
|p r theta| |
|
1169 |
||
1170 |
p := self - center. |
|
1171 |
r := p r. |
|
1172 |
theta := angle asFloat - p theta. |
|
18891 | 1173 |
^ self class x:(center x asFloat + (r * theta cos)) |
20308 | 1174 |
y:(center y asFloat - (r * theta sin)) |
4893 | 1175 |
|
1176 |
" |
|
20308 | 1177 |
(10@10) rotateBy:Float pi about:0@0 |
1178 |
(10@0) rotateBy:Float pi about:0@0 |
|
4893 | 1179 |
" |
1180 |
! |
|
1181 |
||
637 | 1182 |
scaledBy:aScale |
20308 | 1183 |
"return a new Point that is the product of the |
637 | 1184 |
receiver and scale (which is a Point or Number)." |
1185 |
||
1186 |
^ self * aScale |
|
1187 |
! |
|
1188 |
||
1189 |
translatedBy:anOffset |
|
20308 | 1190 |
"return a new Point that is the sum of the |
637 | 1191 |
receiver and scale (which is a Point or Number)." |
1192 |
||
1193 |
^ self + anOffset |
|
1194 |
! ! |
|
1195 |
||
2273 | 1196 |
!Point class methodsFor:'documentation'! |
637 | 1197 |
|
1198 |
version |
|
18840 | 1199 |
^ '$Header$' |
637 | 1200 |
! ! |
7261 | 1201 |
|
18840 | 1202 |
|
356 | 1203 |
Point initialize! |