ckc
ckc

Reputation: 373

Possible error in CGAffineTransform documentation

In CGAffineTransform documentation https://developer.apple.com/documentation/coregraphics/cgaffinetransform the equations that transform point (x, y) to point (x’,y’) are as follows: x'=ax+cy+t_x, y'=bx+dy+t_y. Consider the case where we have scaling on a view. Then the equations become x'=ax, y'=dy. This means that according to the equations the point that remains at the same position after the scaling is (0,0). However, the point that remains at the same position is the center of the view. This can be seen in practice, as well as here: https://developer.apple.com/documentation/quartzcore/calayer/1410817-anchorpoint.

My question is: Are the equations provided in CGAffineTransform documentation, incorrect indeed?

Upvotes: 0

Views: 91

Answers (3)

Rob Napier
Rob Napier

Reputation: 299355

This means that according to the equations the point that remains at the same position after the scaling is (0,0).

Exactly. But (0,0) is the anchor point, not the "view bounds origin" which I believe you're expecting.

However, the point that remains at the same position is the center of the view.

Exactly. Because that's where the anchor point is.

You even link to the documentation that explains this. Transforms are applied to a coordinate space based on the anchor point. The default anchor point is in the center of the view. This is independent of the coordinate space of bounds.

CALayer works in a coordinate space from (0,0) to (1,1), with (0.5, 0.5) in its center. The default anchor point is the center, but can be moved.

The docs are very clear about that. They're not misleading or incorrect:

This property [transform] is set to the identity transform by default. Any transformations you apply to the layer occur relative to the layer’s anchor point.

To make the transform "relative to the layer's anchor point," scaling and translation may be applied. That's fine; that's how affine transforms work. They can be freely combined to moved between coordinate spaces. If you want the coordinate space to be anchored a the upper-left corner, move the anchor point.

Upvotes: 1

matt
matt

Reputation: 535201

You are basically equivocating on the terms x and y. The formulae given are the correct linear algebra formulae for what an affine transform is — in the abstract. But nothing is said about how that applies to a particular view.

Actually, the "point that remains at the same position" is arbitrary. You can set it to any point of the view. It is the bounds midpoint by default, but you can change that (by changing the anchor point of the view's underlying layer).

So you would simply have to understand that for purposes of the transform, the "origin" is wherever you say it is. By default, it's the bounds midpoint.

To see this more clearly, use the applying method of CGPoint, which actually performs the transform for you:

let p = CGPoint.zero
let pprime = p.applying(CGAffineTransform(scaleX: 2, y: 2))
print(pprime)

The result is .zero. So, sure enough, whatever point we consider to be the origin for purposes of the transform, does not move as a result of the transform. But there is nothing here about what point of a particular view is the origin for purposes for purposes of the transform. Devoid of a context, it is just pure math — and so are the equations you started with.

Upvotes: 2

impression7vx
impression7vx

Reputation: 1863

I'm not sure where you got This means that according to the equations the point that remains at the same position after the scaling is (0,0). from.

If you are only SCALING a view, the center point itself does not change, therefore t_x and t_y does not change, only the scaling at which you see, which is the coefficients ax and dy change; therefore, WHEN SCALING x' = ax and y' = dy.

For example, if you scale on an image, let's say your current centerpoint is the CENTER of the image. If you scale, you will scale upon the CENTER of the image. If the user slides the image, that is no longer a ONLY scaling on a view.

Additionally, the point (0,0) is NOT the same; the only point that stays the same is the center. Let's say you can see the full image of (0,0,400,400) - with the point (0,0) at the top left and (200,200) at the center. If you zoom in by DOUBLE, you will now have (100,100) at the top left and (200,200) at the center. Note: (0,0) did not stay the same, but the center did.

Answer: No

Upvotes: 3

Related Questions