Search code examples
javamatrixawtaffinetransform

When would you use AffineTransform getScaleX()?


Is there a legitimate use case for AffineTransform.getScaleX() and family?

It returns the m00 or the top left corner element of the transform matrix and that is pretty useless for determining the scaling the matrix does. Consider for example a trivial 90 degree rotation matrix and both getScaleX() and getScaleY() return 0.

I would never use this call as it is confusing to the code reader who may not be familiar with the fact that it does not return scaling in a meaningful way, much better to getMatrix(m) and then access the m[0], because most people likely to read code that uses transformations are familiar with matrix math.

There must be a use case for this, but I just don't get it.


Solution

  • As you wrote, the notion of the x scaling factor is quite meaningless in the general case. It is only relevant for the special case of scaling matrixes. i.E. those for which getType returns TYPE_GENERAL_SCALE or TYPE_UNIFORM_SCALE. (A translation could be added, too) In other cases, getScaleX is "correct" in the sense that it does what the docs say, but usesless and misleading regarding its name. Analogous reasoning applies to getShearX, which only makes senses in context of shearing matrixes. getTranslateX is a bit different, since one could argue that this is where the origin gets translated to, regardless of all other transformations the matrix implies.