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