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