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