0
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1992 by Claus Gittinger
|
|
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 |
Object subclass: #WindowingTransformation
|
|
14 |
instanceVariableNames: 'scale translation'
|
|
15 |
classVariableNames: ''
|
|
16 |
poolDictionaries: ''
|
|
17 |
category: 'Graphics-Support'!
|
|
18 |
|
|
19 |
WindowingTransformation comment:'
|
|
20 |
COPYRIGHT (c) 1992 by Claus Gittinger
|
|
21 |
All Rights Reserved
|
54
|
22 |
|
|
23 |
$Header: /cvs/stx/stx/libview/WindowingTransformation.st,v 1.5 1994-08-05 01:16:22 claus Exp $
|
46
|
24 |
'!
|
0
|
25 |
|
46
|
26 |
!WindowingTransformation class methodsFor: 'documentation'!
|
|
27 |
|
|
28 |
copyright
|
|
29 |
"
|
|
30 |
COPYRIGHT (c) 1992 by Claus Gittinger
|
|
31 |
All Rights Reserved
|
0
|
32 |
|
46
|
33 |
This software is furnished under a license and may be used
|
|
34 |
only in accordance with the terms of that license and with the
|
|
35 |
inclusion of the above copyright notice. This software may not
|
|
36 |
be provided or otherwise made available to, or used by, any
|
|
37 |
other person. No title to or ownership of the software is
|
|
38 |
hereby transferred.
|
|
39 |
"
|
|
40 |
!
|
0
|
41 |
|
46
|
42 |
version
|
|
43 |
"
|
54
|
44 |
$Header: /cvs/stx/stx/libview/WindowingTransformation.st,v 1.5 1994-08-05 01:16:22 claus Exp $
|
46
|
45 |
"
|
|
46 |
!
|
|
47 |
|
|
48 |
documentation
|
|
49 |
"
|
|
50 |
I represent the ability to perform transformations in 2-D space.
|
0
|
51 |
|
46
|
52 |
Instance variables are:
|
|
53 |
scale <Number> or <Point> representing a linear scaling factor.
|
|
54 |
translation <Number> or <Point> representing a translation in 2-D.
|
0
|
55 |
|
46
|
56 |
All 2-D objects are supposed to be able to be transformed using
|
|
57 |
instances of me. Instances of me can also be combined to form a
|
|
58 |
single composite transformation.
|
|
59 |
"
|
|
60 |
! !
|
0
|
61 |
|
|
62 |
!WindowingTransformation methodsFor: 'accessing'!
|
|
63 |
|
|
64 |
scale
|
|
65 |
"return a copy of the Point that represents the
|
|
66 |
current scale of the receiver."
|
|
67 |
|
|
68 |
scale == nil ifTrue:[
|
|
69 |
^ Point x:1 y:1
|
|
70 |
].
|
|
71 |
^ scale copy
|
|
72 |
!
|
|
73 |
|
|
74 |
scaleOfOne
|
|
75 |
"Set the scale of the receiver to the identity scale"
|
|
76 |
|
|
77 |
scale := nil
|
|
78 |
!
|
|
79 |
|
|
80 |
translation
|
|
81 |
"return a copy of the receiver's translation."
|
|
82 |
|
|
83 |
^ translation copy
|
|
84 |
!
|
|
85 |
|
|
86 |
translation: aValue
|
|
87 |
"Set the receiver's translation to aValue."
|
|
88 |
|
|
89 |
translation := aValue
|
|
90 |
! !
|
|
91 |
|
|
92 |
!WindowingTransformation methodsFor: 'testing'!
|
|
93 |
|
|
94 |
noScale
|
|
95 |
"return true if the identity scale is in effect;
|
|
96 |
answer false, otherwise."
|
|
97 |
|
|
98 |
^ scale == nil
|
|
99 |
! !
|
|
100 |
|
|
101 |
!WindowingTransformation methodsFor: 'applying transform'!
|
|
102 |
|
|
103 |
applyInverseTo:anObject
|
|
104 |
"Apply the inverse of the receiver to anObject
|
|
105 |
and answer the result."
|
|
106 |
|
|
107 |
|transformedObject|
|
|
108 |
|
2
|
109 |
transformedObject := anObject translatedBy:(self inverseTranslation).
|
0
|
110 |
scale == nil ifFalse:[
|
2
|
111 |
transformedObject scaleBy:(self inverseScale)
|
0
|
112 |
].
|
|
113 |
^ transformedObject
|
|
114 |
!
|
|
115 |
|
|
116 |
applyTo:anObject
|
|
117 |
"Apply the receiver to anObject and answer the result."
|
|
118 |
|
|
119 |
|transformedObject|
|
|
120 |
|
|
121 |
scale == nil ifTrue:[
|
2
|
122 |
^ anObject translateBy:translation.
|
0
|
123 |
].
|
2
|
124 |
transformedObject := anObject scaledBy:scale
|
|
125 |
transformedObject translateBy:translation.
|
0
|
126 |
^ transformedObject
|
|
127 |
!
|
|
128 |
|
|
129 |
compose:aTransformation
|
|
130 |
"return a new WindowingTransformation that is the
|
|
131 |
composition of the receiver and aTransformation.
|
|
132 |
The effect of applying the resulting WindowingTransformation
|
|
133 |
to an object is the same as that of first applying
|
|
134 |
aTransformation to the object and then applying the
|
|
135 |
receiver to its result."
|
|
136 |
|
|
137 |
|aTransformationScale newScale newTranslation|
|
|
138 |
|
|
139 |
aTransformationScale := aTransformation scale.
|
|
140 |
scale == nil ifTrue:[
|
|
141 |
aTransformation noScale ifTrue:[
|
|
142 |
newScale := nil
|
|
143 |
] ifFalse:[
|
|
144 |
newScale := aTransformationScale
|
|
145 |
].
|
|
146 |
newTranslation := translation + aTransformation translation
|
|
147 |
] ifFalse:[
|
|
148 |
aTransformation noScale ifTrue:[
|
|
149 |
newScale := scale
|
|
150 |
] ifFalse:[
|
|
151 |
newScale := scale * aTransformationScale
|
|
152 |
].
|
|
153 |
newTranslation := translation
|
|
154 |
+ (scale * aTransformation translation)
|
|
155 |
].
|
|
156 |
^ WindowingTransformation scale:newScale
|
|
157 |
translation:newTranslation
|
|
158 |
! !
|
|
159 |
|
|
160 |
!WindowingTransformation methodsFor: 'transforming'!
|
|
161 |
|
2
|
162 |
scaledBy:aScale
|
0
|
163 |
"return a new WindowingTransformation with the scale and translation of
|
|
164 |
the receiver both scaled by aScale."
|
|
165 |
|
|
166 |
|checkedScale newScale newTranslation|
|
|
167 |
|
|
168 |
aScale == nil ifTrue:[
|
|
169 |
newScale := scale.
|
|
170 |
newTranslation := translation
|
|
171 |
] ifFalse:[
|
|
172 |
checkedScale := self checkScale:aScale.
|
|
173 |
scale == nil ifTrue:[
|
|
174 |
newScale := checkedScale
|
|
175 |
] ifFalse:[
|
|
176 |
newScale := scale * checkedScale
|
|
177 |
].
|
|
178 |
newTranslation := checkedScale * translation
|
|
179 |
].
|
|
180 |
^ WindowingTransformation scale:newScale
|
|
181 |
translation:newTranslation
|
|
182 |
!
|
|
183 |
|
2
|
184 |
translatedBy:aPoint
|
0
|
185 |
"return a new WindowingTransformation with the same scale and
|
|
186 |
rotations as the receiver and with a translation of the current
|
|
187 |
translation plus aPoint."
|
|
188 |
|
|
189 |
^ WindowingTransformation scale:scale
|
|
190 |
translation:(translation + aPoint)
|
|
191 |
! !
|
|
192 |
|
|
193 |
!WindowingTransformation methodsFor: 'printing'!
|
|
194 |
|
|
195 |
printString
|
|
196 |
^ (self class name, ' scale: ', scale printString,
|
|
197 |
' translation: ', translation printString)
|
|
198 |
! !
|
|
199 |
|
|
200 |
!WindowingTransformation methodsFor: 'private'!
|
|
201 |
|
|
202 |
checkScale:aScale
|
|
203 |
"Converts aScale to the internal format of a floating-point Point."
|
|
204 |
|
|
205 |
|checkedScale|
|
|
206 |
|
|
207 |
checkedScale := aScale asPoint.
|
|
208 |
^ Point x:checkedScale x asFloat
|
|
209 |
y:checkedScale y asFloat
|
|
210 |
!
|
|
211 |
|
|
212 |
inverseScale
|
|
213 |
"return with a Point representing the inverse of my
|
|
214 |
scale."
|
|
215 |
|
|
216 |
|newScale|
|
|
217 |
|
|
218 |
newScale := self checkScale:scale.
|
|
219 |
^ Point x:(1.0 / newScale x)
|
|
220 |
y:(1.0 / newScale y)
|
|
221 |
!
|
|
222 |
|
|
223 |
inverseTranslation
|
|
224 |
"return with a Point representing the inverse of my
|
|
225 |
translation."
|
|
226 |
|
|
227 |
|trans|
|
|
228 |
|
|
229 |
trans := translation asPoint.
|
|
230 |
^ Point x:trans x negated
|
|
231 |
y:trans y negated
|
|
232 |
!
|
|
233 |
|
|
234 |
setScale:aScale translation:aTranslation
|
|
235 |
"Sets the scale to aScale and the translation to aTranslation."
|
|
236 |
|
|
237 |
scale := aScale.
|
|
238 |
translation := aTranslation
|
|
239 |
! !
|
|
240 |
|
|
241 |
!WindowingTransformation class methodsFor: 'instance creation'!
|
|
242 |
|
|
243 |
identity
|
|
244 |
"returns a windowing transformation with no scaling (nil)
|
|
245 |
and no translation (0@0)."
|
|
246 |
|
2
|
247 |
^ self new setScale:nil translation:(Point x:0.0 y:0.0)
|
0
|
248 |
!
|
|
249 |
|
|
250 |
scale:aScale translation:aTranslation
|
|
251 |
"returns a windowing transformation with a scale factor of
|
|
252 |
aScale and a translation offset of aTranslation."
|
|
253 |
|
2
|
254 |
^ self new setScale:aScale translation:aTranslation
|
0
|
255 |
!
|
|
256 |
|
|
257 |
window:sourceRectangle viewport:destinationRectangle
|
|
258 |
"returns a windowing transformation with a scale and
|
|
259 |
translation computed from sourceRectangle and destinationRectangle.
|
|
260 |
The scale and transformation are computed such that sourceRectangle
|
|
261 |
is transformed to destinationRectangle."
|
|
262 |
|
|
263 |
|sX sY tX tY newScale|
|
|
264 |
sX := destinationRectangle width / sourceRectangle width.
|
|
265 |
sY := destinationRectangle height / sourceRectangle height.
|
|
266 |
tX := destinationRectangle left - sourceRectangle left.
|
|
267 |
tY := destinationRectangle top - sourceRectangle top.
|
|
268 |
((sX = 1.0) and:[sY = 1.0]) ifTrue:[
|
|
269 |
newScale := nil
|
|
270 |
] ifFalse:[
|
|
271 |
newScale := Point x:sX y:sY
|
|
272 |
].
|
2
|
273 |
^ self new setScale:newScale translation:(Point x:tX y:tY)
|
0
|
274 |
! !
|