@@ -110,7 +110,7 @@ public SimilarityTransformation2D Scale(double scale, bool isXScaleNegative, boo
110
110
{
111
111
Scaling = Scaling * scale . CheckPositive ( ) ,
112
112
IsYScaleNegative = IsYScaleNegative ^ isXScaleNegative ^ isYScaleNegative ,
113
- Rotation = Rotation + AngularMeasure . Pi * ( isXScaleNegative ? 1 : 0 ) ,
113
+ Rotation = Rotation + ( isXScaleNegative ? AngularMeasure . Pi : AngularMeasure . Zero ) ,
114
114
} ;
115
115
}
116
116
@@ -137,16 +137,32 @@ public SimilarityTransformation2D Translate(Vector2D translation)
137
137
/// <summary>
138
138
/// 将点进行相似变换。
139
139
/// </summary>
140
- /// <param name="point"></param>
141
- /// <returns></returns>
140
+ /// <param name="point">要变换的点。 </param>
141
+ /// <returns>变换后的点。 </returns>
142
142
public Point2D Transform ( Point2D point )
143
+ {
144
+ return ( Point2D ) ( Transform ( point . ToVector ( ) ) + Translation ) ;
145
+ }
146
+
147
+ /// <summary>
148
+ /// 将向量进行相似变换。与点的变换不同的是,向量的变换不会计算 <see cref="Translation"/>。
149
+ /// </summary>
150
+ /// <param name="vector">要变换的向量。</param>
151
+ /// <returns>变换后的向量。</returns>
152
+ public Vector2D Transform ( Vector2D vector )
143
153
{
144
154
var sin = Rotation . Sin ( ) ;
145
155
var cos = Rotation . Cos ( ) ;
146
- return new Point2D (
147
- Scaling * ( cos * point . X - sin * point . Y ) + Translation . X ,
148
- ( IsYScaleNegative ? - 1 : 1 ) * Scaling * ( sin * point . X + cos * point . Y ) + Translation . Y
149
- ) ;
156
+
157
+ return IsYScaleNegative
158
+ ? new Vector2D (
159
+ Scaling * ( cos * vector . X + sin * vector . Y ) ,
160
+ Scaling * ( sin * vector . X - cos * vector . Y )
161
+ )
162
+ : new Vector2D (
163
+ Scaling * ( cos * vector . X - sin * vector . Y ) ,
164
+ Scaling * ( sin * vector . X + cos * vector . Y )
165
+ ) ;
150
166
}
151
167
152
168
/// <summary>
@@ -169,16 +185,38 @@ public SimilarityTransformation2D Inverse()
169
185
{
170
186
var sin = Rotation . Sin ( ) ;
171
187
var cos = Rotation . Cos ( ) ;
188
+ var yScaleFactor = IsYScaleNegative ? - 1 : 1 ;
172
189
return this with
173
190
{
174
191
Scaling = 1 / Scaling ,
175
- Rotation = ( - Rotation ) . Normalized ,
192
+ Rotation = ( yScaleFactor * - Rotation ) . Normalized ,
193
+ IsYScaleNegative = IsYScaleNegative ,
176
194
Translation = new Vector2D (
177
195
- ( cos * Translation . X + sin * Translation . Y ) / Scaling ,
178
- ( IsYScaleNegative ? - 1 : 1 ) * ( sin * Translation . X - cos * Translation . Y ) / Scaling ) ,
196
+ yScaleFactor * ( sin * Translation . X - cos * Translation . Y ) / Scaling ) ,
179
197
} ;
180
198
}
181
199
200
+ /// <summary>
201
+ /// 应用另一个相似变换到当前相似变换上。
202
+ /// </summary>
203
+ /// <remarks>
204
+ /// 使用应用后的结果对目标进行变换时,相当于使用当前变换对目标进行变换,然后再使用 <paramref name="transformation"/> 对变换后的目标进行变换。
205
+ /// </remarks>
206
+ /// <param name="transformation">要应用的相似变换。</param>
207
+ /// <returns>应用后的相似变换。</returns>
208
+ public SimilarityTransformation2D Apply ( SimilarityTransformation2D transformation )
209
+ {
210
+ ArgumentNullException . ThrowIfNull ( transformation ) ;
211
+
212
+ var yScaleFactor = transformation . IsYScaleNegative ? - 1 : 1 ;
213
+ return new SimilarityTransformation2D (
214
+ Scaling * transformation . Scaling ,
215
+ IsYScaleNegative ^ transformation . IsYScaleNegative ,
216
+ yScaleFactor * Rotation + transformation . Rotation ,
217
+ transformation . Transform ( Translation ) + transformation . Translation ) ;
218
+ }
219
+
182
220
#endregion
183
221
}
184
222
0 commit comments