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