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