I am trying to scale a shape that I have in Java around a certain point.
When I use the AffineTransform.scale method, it scales based on the top left corner. Is there anyway to scale anchored at a point (say the center of the window for this case).
Thanks,
Ty
Usually, to scale about a particular point you first translate the object so that the point is at the origin. You then perform the scaling and then the inverse of the original translation to move the fixed point back to its original position.
Examples of affine transformations include translation, scaling, homothety, similarity, reflection, rotation, shear mapping, and compositions of them in any combination and sequence.
The affine transforms scale, rotate and shear are actually linear transforms and can be represented by a matrix multiplication of a point represented as a vector, [x y ] = [ax + by dx + ey ] = [a b d e ][x y ] , or x = Mx, where M is the matrix.
This sequence of operations can be combined into a single affine transform matrix by combining the transform matrices in the correct mathematical order: The affine transform resulting from a X translation, then a Y translation and then a Z rotation sequence.
I agree with Hovercraft Full of Eels that the proper way to do this is to translate the center to the top left corner, scale, then translate the top left corner back to the center.
However, if you want it to perform it in fewer than three steps, the transform is:
x ⟼ S(x – c) + c = Sx + (c – Sc),
where S is the scaling transformation, and c is the center in coordinates relative to the top left.
So, you need to do your scaling, then translate by c – Sc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With