1
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
1
|
3 |
All Rights Reserved
|
|
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 |
|
|
13 |
ArithmeticValue subclass:#Point
|
|
14 |
instanceVariableNames:'x y'
|
|
15 |
classVariableNames:'PointZero PointOne'
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Graphics-Primitives'
|
|
18 |
!
|
|
19 |
|
|
20 |
Point comment:'
|
|
21 |
|
5
|
22 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
1
|
23 |
All Rights Reserved
|
|
24 |
|
|
25 |
I represent a point in 2D space. Or I can be used to represent
|
|
26 |
an extent (of a rectangle for example), in which case my x-coordinate
|
|
27 |
represents the width, and y-coordinate the height of something.
|
|
28 |
|
|
29 |
The x and y coordinate are usually numbers.
|
|
30 |
|
|
31 |
Instance variables:
|
|
32 |
|
|
33 |
x <Number> the x-coordinate of myself
|
|
34 |
y <Number> the y-coordinate of myself
|
|
35 |
|
5
|
36 |
$Header: /cvs/stx/stx/libbasic/Point.st,v 1.4 1993-10-13 02:13:08 claus Exp $
|
1
|
37 |
written 89 by claus
|
|
38 |
'!
|
|
39 |
|
|
40 |
!Point class methodsFor:'initialization'!
|
|
41 |
|
|
42 |
initialize
|
|
43 |
PointZero := 0 @ 0.
|
|
44 |
PointOne := 1 @ 1
|
|
45 |
! !
|
|
46 |
|
|
47 |
!Point class methodsFor:'constants'!
|
|
48 |
|
|
49 |
zero
|
|
50 |
^ PointZero
|
|
51 |
!
|
|
52 |
|
|
53 |
unity
|
|
54 |
^ PointOne
|
|
55 |
! !
|
|
56 |
|
3
|
57 |
!Point class methodsFor:'queries'!
|
|
58 |
|
|
59 |
isBuiltInClass
|
|
60 |
"this class is known by the run-time-system"
|
|
61 |
|
|
62 |
^ self == Point
|
|
63 |
! !
|
|
64 |
|
1
|
65 |
!Point class methodsFor:'instance creation'!
|
|
66 |
|
|
67 |
x:newX y:newY
|
|
68 |
"create and return a new point with coordinates newX and newY"
|
|
69 |
|
|
70 |
%{ /* NOCONTEXT */
|
|
71 |
extern char *newNextPtr, *newEndPtr;
|
|
72 |
|
|
73 |
if (self == Point) {
|
|
74 |
if (_CanDoQuickNew(sizeof(struct point))) {
|
|
75 |
OBJ newPoint;
|
|
76 |
|
|
77 |
_qCheckedAlignedNew(newPoint, sizeof(struct point), __context);
|
|
78 |
_InstPtr(newPoint)->o_class = Point;
|
|
79 |
_PointInstPtr(newPoint)->p_x = newX;
|
|
80 |
_PointInstPtr(newPoint)->p_y = newY;
|
3
|
81 |
__STORE(newPoint, newX);
|
|
82 |
__STORE(newPoint, newY);
|
1
|
83 |
RETURN ( newPoint );
|
|
84 |
}
|
|
85 |
}
|
|
86 |
%}
|
|
87 |
.
|
|
88 |
^ (self basicNew) x:newX y:newY
|
|
89 |
!
|
|
90 |
|
|
91 |
readFrom:aStream
|
|
92 |
"return the next Point from the (character-)stream aStream;
|
|
93 |
skipping all whitespace first; return nil if no point"
|
|
94 |
|
|
95 |
|newX newY|
|
|
96 |
|
|
97 |
newX := Number readFrom:aStream.
|
|
98 |
newX isNil ifTrue:[^ nil].
|
|
99 |
(aStream skipSeparators ~~ $@) ifTrue:[^nil].
|
|
100 |
aStream next.
|
|
101 |
newY := Number readFrom:aStream.
|
|
102 |
newY isNil ifTrue:[^ nil].
|
|
103 |
^ (self basicNew) x:newX y:newY
|
|
104 |
! !
|
|
105 |
|
|
106 |
!Point methodsFor:'accessing'!
|
|
107 |
|
|
108 |
x
|
|
109 |
"return the x coordinate"
|
|
110 |
|
|
111 |
^x
|
|
112 |
!
|
|
113 |
|
|
114 |
y
|
|
115 |
"return the y coordinate"
|
|
116 |
|
|
117 |
^y
|
|
118 |
!
|
|
119 |
|
|
120 |
x:newX
|
|
121 |
"set the x coordinate to be the argument, aNumber"
|
|
122 |
|
|
123 |
x := newX
|
|
124 |
!
|
|
125 |
|
|
126 |
y:newY
|
|
127 |
"set the y coordinate to be the argument, aNumber"
|
|
128 |
|
|
129 |
y := newY
|
|
130 |
!
|
|
131 |
|
|
132 |
x:newX y:newY
|
|
133 |
"set both the x and y coordinates"
|
|
134 |
|
|
135 |
x := newX.
|
|
136 |
y := newY
|
|
137 |
! !
|
|
138 |
|
|
139 |
!Point methodsFor:'comparing'!
|
|
140 |
|
|
141 |
hash
|
|
142 |
"return a number for hashing"
|
|
143 |
|
|
144 |
^ (x hash) bitXor:(y hash)
|
|
145 |
!
|
|
146 |
|
|
147 |
< aPoint
|
|
148 |
"return true if the receiver is above and to the left
|
|
149 |
of the argument, aPoint"
|
|
150 |
|
|
151 |
|p|
|
|
152 |
|
|
153 |
p := aPoint asPoint.
|
|
154 |
x >= (p x) ifTrue:[^ false].
|
|
155 |
y >= (p y) ifTrue:[^ false].
|
|
156 |
^ true
|
|
157 |
!
|
|
158 |
|
|
159 |
> aPoint
|
|
160 |
"return true if the receiver is below and to the right
|
|
161 |
of the argument, aPoint"
|
|
162 |
|
|
163 |
|p|
|
|
164 |
|
|
165 |
p := aPoint asPoint.
|
|
166 |
x <= (p x) ifTrue:[^ false].
|
|
167 |
y <= (p y) ifTrue:[^ false].
|
|
168 |
^ true
|
|
169 |
!
|
|
170 |
|
|
171 |
= aPoint
|
|
172 |
"return true if the receiver represents the same point as
|
|
173 |
the argument, aPoint"
|
|
174 |
|
|
175 |
|p|
|
|
176 |
|
|
177 |
(aPoint isMemberOf:Point) ifTrue:[
|
3
|
178 |
x ~~ (aPoint x) ifTrue:[^ false].
|
|
179 |
y ~~ (aPoint y) ifTrue:[^ false].
|
|
180 |
^ true
|
1
|
181 |
].
|
|
182 |
aPoint respondsToArithmetic ifFalse:[ ^ false].
|
|
183 |
p := aPoint asPoint.
|
|
184 |
x ~~ (p x) ifTrue:[^ false].
|
|
185 |
y ~~ (p y) ifTrue:[^ false].
|
|
186 |
^ true
|
|
187 |
!
|
|
188 |
|
|
189 |
max:aPoint
|
|
190 |
"return the lower right corner of the rectangle uniquely defined by
|
|
191 |
the receiver and the argument, aPoint"
|
|
192 |
|
|
193 |
|p maxX maxY|
|
|
194 |
|
|
195 |
p := aPoint asPoint.
|
|
196 |
maxX := x max:(p x).
|
|
197 |
maxY := y max:(p y).
|
|
198 |
^ maxX @ maxY
|
|
199 |
!
|
|
200 |
|
|
201 |
min:aPoint
|
|
202 |
"return the upper left corner of the rectangle uniquely defined by
|
|
203 |
the receiver and the argument, aPoint"
|
|
204 |
|
|
205 |
|p minX minY|
|
|
206 |
|
|
207 |
p := aPoint asPoint.
|
|
208 |
minX := x min:(p x).
|
|
209 |
minY := y min:(p y).
|
|
210 |
^ minX @ minY
|
|
211 |
! !
|
|
212 |
|
|
213 |
!Point methodsFor:'converting'!
|
|
214 |
|
|
215 |
generality
|
|
216 |
^ 120
|
|
217 |
!
|
|
218 |
|
|
219 |
coerce:anObject
|
|
220 |
^ anObject asPoint
|
|
221 |
!
|
|
222 |
|
|
223 |
asPoint
|
|
224 |
"return the receiver as Point - this is the receiver"
|
|
225 |
|
|
226 |
^ self
|
|
227 |
!
|
|
228 |
|
|
229 |
asRectangle
|
|
230 |
"return a zero-width rectangle consisting of origin
|
|
231 |
and corner beeing the receiver"
|
|
232 |
|
|
233 |
^ self corner:self
|
|
234 |
! !
|
|
235 |
|
|
236 |
!Point methodsFor:'creating rectangles'!
|
|
237 |
|
|
238 |
corner:aPoint
|
|
239 |
"return a rectangle whose origin is self and corner is aPoint"
|
|
240 |
|
|
241 |
^ Rectangle origin:self corner:aPoint
|
|
242 |
!
|
|
243 |
|
|
244 |
extent:aPoint
|
|
245 |
"return a rectangle whose origin is self and extent is aPoint"
|
|
246 |
|
|
247 |
^ Rectangle origin:self extent:aPoint
|
|
248 |
! !
|
|
249 |
|
|
250 |
!Point methodsFor:'transformations'!
|
|
251 |
|
|
252 |
+ scale
|
|
253 |
"Return a new Point that is the sum of the
|
|
254 |
receiver and scale (which is a Point or Number)."
|
|
255 |
|
|
256 |
|scalePoint|
|
|
257 |
|
|
258 |
scalePoint := scale asPoint.
|
|
259 |
^ (x + scalePoint x) @ (y + scalePoint y)
|
|
260 |
!
|
|
261 |
|
|
262 |
- scale
|
|
263 |
"Return a new Point that is the difference of the
|
|
264 |
receiver and scale (which is a Point or Number)."
|
|
265 |
|
|
266 |
|scalePoint|
|
|
267 |
|
|
268 |
scalePoint := scale asPoint.
|
|
269 |
^ (x - scalePoint x) @ (y - scalePoint y)
|
|
270 |
!
|
|
271 |
|
|
272 |
* scale
|
|
273 |
"Return a new Point that is the product of the
|
|
274 |
receiver and scale (which is a Point or Number)."
|
|
275 |
|
|
276 |
|scalePoint|
|
|
277 |
|
|
278 |
scalePoint := scale asPoint.
|
|
279 |
^ (x * scalePoint x) @ (y * scalePoint y)
|
|
280 |
!
|
|
281 |
|
|
282 |
/ scale
|
|
283 |
"Return a new Point that is the integer quotient of the
|
|
284 |
receiver and scale (which is a Point or Number)."
|
|
285 |
|
|
286 |
|scalePoint|
|
|
287 |
|
|
288 |
scalePoint := scale asPoint.
|
|
289 |
^ (x / scalePoint x) @ (y / scalePoint y)
|
|
290 |
!
|
|
291 |
|
|
292 |
// scale
|
|
293 |
"Return a new Point that is the quotient of the
|
|
294 |
receiver and scale (which is a Point or Number)."
|
|
295 |
|
|
296 |
|scalePoint|
|
|
297 |
|
|
298 |
scalePoint := scale asPoint.
|
|
299 |
^ (x // scalePoint x) @ (y // scalePoint y)
|
|
300 |
!
|
|
301 |
|
|
302 |
reciprocal
|
|
303 |
"return a new point where the coordinates are
|
|
304 |
the reciproce of mine"
|
|
305 |
|
|
306 |
^ (1 / x) @ (1 / y)
|
|
307 |
!
|
|
308 |
|
|
309 |
negated
|
|
310 |
"return a new point with my coordinates negated i.e.
|
|
311 |
the receiver mirrored at the origin"
|
|
312 |
|
|
313 |
^ (x negated) @ (y negated)
|
|
314 |
!
|
|
315 |
|
|
316 |
scaleBy:aScale
|
3
|
317 |
"scale the receiver, by replacing coordinates by the product
|
|
318 |
of the receivers coordinated and the scale
|
|
319 |
(which is a Point or Number)."
|
|
320 |
|
|
321 |
(aScale isKindOf:Point) ifFalse:[
|
|
322 |
x := x * aScale.
|
|
323 |
y := y * aScale
|
|
324 |
] ifTrue:[
|
|
325 |
x := x * aScale x.
|
|
326 |
y := y * aScale y
|
|
327 |
]
|
|
328 |
!
|
|
329 |
|
|
330 |
translateBy:anOffset
|
|
331 |
"translate the receiver, by replacing coordinates by the sum
|
|
332 |
of the receivers coordinated and the scale
|
|
333 |
(which is a Point or Number)."
|
|
334 |
|
|
335 |
(anOffset isKindOf:Point) ifFalse:[
|
|
336 |
x := x + anOffset.
|
|
337 |
y := y + anOffset
|
|
338 |
] ifTrue:[
|
|
339 |
x := x + anOffset x.
|
|
340 |
y := y + anOffset y
|
|
341 |
]
|
|
342 |
!
|
|
343 |
|
|
344 |
scaledBy:aScale
|
|
345 |
"return a new Point that is the product of the
|
1
|
346 |
receiver and scale (which is a Point or Number)."
|
|
347 |
|
|
348 |
^ self * aScale
|
|
349 |
!
|
|
350 |
|
3
|
351 |
translatedBy:anOffset
|
|
352 |
"return a new Point that is the sum of the
|
1
|
353 |
receiver and scale (which is a Point or Number)."
|
|
354 |
|
|
355 |
^ self + anOffset
|
|
356 |
! !
|
|
357 |
|
|
358 |
!Point methodsFor:'misc'!
|
|
359 |
|
|
360 |
dist:aPoint
|
|
361 |
"return the distance between aPoint and the receiver."
|
|
362 |
|
|
363 |
^ (aPoint - self) r
|
|
364 |
!
|
|
365 |
|
|
366 |
dotProduct:aPoint
|
|
367 |
"return a Number that is the dot product of the receiver and
|
|
368 |
the argument, aPoint. That is, the two points are
|
|
369 |
multipled and the coordinates of the result summed."
|
|
370 |
|
|
371 |
|temp|
|
|
372 |
|
|
373 |
temp := self * aPoint.
|
|
374 |
^ temp x abs + temp y abs
|
|
375 |
!
|
|
376 |
|
|
377 |
r
|
|
378 |
"return the receiver's radius in polar coordinate system."
|
|
379 |
|
|
380 |
^ (self dotProduct:self) sqrt
|
|
381 |
!
|
|
382 |
|
|
383 |
abs
|
|
384 |
"return a new point with my coordinates taken from the absolute values"
|
|
385 |
|
|
386 |
^ (x abs) @ (y abs)
|
|
387 |
!
|
|
388 |
|
|
389 |
truncated
|
|
390 |
"return a new point with my coordinates truncated as integer"
|
|
391 |
|
|
392 |
^ (x truncated) @ (y truncated)
|
|
393 |
!
|
|
394 |
|
|
395 |
rounded
|
|
396 |
"return a new point with my coordinates rounded to the next integer
|
|
397 |
coordinated (use for gridding)"
|
|
398 |
|
|
399 |
^ (x rounded) @ (y rounded)
|
|
400 |
!
|
|
401 |
|
|
402 |
grid:gridPoint
|
|
403 |
"return a new point with coordinates grided (i.e. rounded to the
|
|
404 |
nearest point on the grid)"
|
|
405 |
|
|
406 |
|newX newY gridX gridY|
|
|
407 |
|
|
408 |
gridX := gridPoint x.
|
|
409 |
(gridX <= 1) ifTrue:[
|
|
410 |
newX := x asInteger
|
|
411 |
] ifFalse:[
|
|
412 |
newX := ((x + (gridX // 2)) // gridX) * gridX
|
|
413 |
].
|
|
414 |
gridY := gridPoint y.
|
|
415 |
(gridY <= 1) ifTrue:[
|
|
416 |
newY := y asInteger
|
|
417 |
] ifFalse:[
|
|
418 |
newY := ((y + (gridY // 2)) // gridY) * gridY
|
|
419 |
].
|
|
420 |
^ newX @ newY
|
|
421 |
! !
|
|
422 |
|
|
423 |
!Point methodsFor:'printing & storing'!
|
|
424 |
|
|
425 |
printString
|
|
426 |
"return my printString"
|
|
427 |
|
|
428 |
^ x printString , '@' , y printString
|
|
429 |
!
|
|
430 |
|
|
431 |
storeString
|
|
432 |
"return my storeString"
|
|
433 |
|
|
434 |
^ self printString
|
|
435 |
! !
|