Re: Coordinate conversions in CALayer
Re: Coordinate conversions in CALayer
- Subject: Re: Coordinate conversions in CALayer
- From: Quincey Morris <email@hidden>
- Date: Wed, 23 Jan 2013 17:42:16 -0800
On Jan 23, 2013, at 17:01 , Graham Cox <email@hidden> wrote:
> The above works correctly, but below, which I thought should do the same, does not, if the layer.transform property is not the identity matrix:
>
>
> CGPoint anch = layer.anchorPoint;
> CGRect br = layer.bounds;
> CGPoint pos = layer.position;
>
>
> CGAffineTransform tfm = CGAffineTransformMakeTranslation( pos.x, pos.y );
> tfm = CGAffineTransformConcat( tfm, layer.transform );
> tfm = CGAffineTransformTranslate( tfm, -(br.origin.x + anch.x * br.size.width), -(br.origin.y + anch.y * br.size.height));
>
> CGContextConcatCTM( ctx, tfm );
>
> In the second case I've tried every combination of ordering the different statements that build the transform, and the sign of the translations, but nothing I have tried makes the second code work identically to the first. I'm missing something obvious but I'm not grokking it. I need the second case to work because it is needed in other situations where I have no context - e.g. just performing transformation calculations between a layer and its superlayer.
>
> Please someone help, this is driving me bats.
This sort of thing drives me nuts too. You have to simplify the problem.
1. In the scenario where bounds.origin is (0,0) and the anchor point is (0,0) -- which should be be a very ordinary scenario to set up, even if you have to abuse your app to make it so -- the third transformation has no effect. If -- another ordinary scenario (I'd hope) -- the layer transform is the identity, the second transformation has no effect. So start with those scenarios in effect. Can you make it work with *just* the first translation doing something? That will incidentally settle the sign question once and for all.
2. Once you have that working, start using a non-identity layer transform, preferably something very easy like a 90 degree rotation, or a 50% scale, or a reflection about a vertical axis. If you don't get the right answer, it *should* be obvious why not. Then try it with a 30 degree rotation to check you didn't just solve it for a special case.
(If you're depending on seeing what draws to find out if you got it right, try something like a 95% or a 105% scale. One or other of those would likely still draw something you can recognize, and tell you whether the error is a mis-translation or a mis-scale, and in which direction.)
3. After you get that far, I can't imagine it would be difficult to get the last transformation right. Even so, do it in 2 parts, first with a non-zero bounds origin, then second with a different anchor point.
As to why the methodology isn't precisely parallel in the 2 cases, I dunno, but I suspect it's because in one case you're drawing untransformed geometry into a transformed drawing space; in the other case, you're drawing transformed geometry into an untransformed drawing space. Those two things might be as equivalent as you expect.
But you've probably thought this all through already ...
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden