Code0987
Code0987

Reputation: 2618

object not rotating properly after translate transform in wpf?

I have canvas with several cutom controls inherited from panel class, dynamically added to it at runtime with rendertransform=(.5,.5). But when apply translate transform (50,50) and rotate it by 100 degrees, it does not rotate on its place, it rotates in radius of 50, why? Am I doing wrong something ?

Upvotes: 1

Views: 2638

Answers (2)

Gabe
Gabe

Reputation: 86868

If it rotates with a radius of 50, it's because your origin is wrong.

You just need to change the origin of your RotateTransform by setting the CenterX and CenterY properties both to 50 in this case.

Upvotes: 0

brunnerh
brunnerh

Reputation: 185589

Transformations are not commutative, you should apply the rotation before applying the translation.

Often you have a TransformGroup, then you can just change the order of its children, if this is somehow not an option because some transform is "inherited" from a parent you can nullify prior transforms using their inverse (in the case of a translation that should move the target back to the origin), then you can rotate it in place, and apply the original transform again.


The documentation is your friend, here is what can be found for TransformGroups:

In a composite transformation, the order of individual transformations is important. For example, if you first rotate, then scale, then translate, you get a different result than if you first translate, then rotate, then scale. One reason order is significant is that transformations like rotation and scaling are done with respect to the origin of the coordinate system. Scaling an object that is centered at the origin produces a different result than scaling an object that has been moved away from the origin. Similarly, rotating an object that is centered at the origin produces a different result than rotating an object that has been moved away from the origin.

Upvotes: 6

Related Questions