Suzan Cioc
Suzan Cioc

Reputation: 30127

How to scale about point?

I find the following code inside PAffineTransform:

/**
     * Scales the transform about the given point by the given scale.
     * 
     * @param scale to transform the transform by
     * @param x x coordinate around which the scale should take place
     * @param y y coordinate around which the scale should take place
     */
    public void scaleAboutPoint(final double scale, final double x, final double y) {

        //TODO strange order

        translate(x, y);
        scale(scale, scale);
        translate(-x, -y);
    }

Wouldn't it correct to do reverse:

        translate(-x, -y);
        scale(scale, scale);
        translate(x, y);

All used methods are the same as in AffineTransform.

UPDATE

My mistake.

Sequential transform modification means matrix multiplication from the right. So, the last applied modification works first while transforming, because transforming is matrix multiplication from the left.

Upvotes: 2

Views: 2196

Answers (1)

tenorsax
tenorsax

Reputation: 21233

The order in PAffineTransform is related to the fact that each node in Piccol2D has an affine transform that defines a local coordinate system for that node.

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.

PNode has its own local coordinate system with origin at (0, 0). So when scaleAboutPoint() is performed on a node, the order defined in PAffineTransform makes sense. First, translate to the point so it becomes the new origin, then scale, then inverse the translation.

Upvotes: 1

Related Questions