Is it a bug in NSAffineTransform?
Is it a bug in NSAffineTransform?
- Subject: Is it a bug in NSAffineTransform?
- From: "Bill So" <email@hidden>
- Date: Tue, 20 Jun 2006 13:20:25 +0800
Dear All,
I m not an expert in computer graphics. I m trying to use
NSAffineTransform as documented in Cocoa Drawing Guide -> Coordinate
Systems and Transforms -> Using Transforms in Your Code -> Undoing a
Transformation.
http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaDrawingGuide/Transforms/chapter_4_section_4.html#//apple_ref/doc/uid/TP40003290-CH204-BCIJGDAA
I found a strange behaviour in NSAffineTransform which doesn't align
with what's described in the documentation.
NSAffineTransform* xform = [NSAffineTransform transform];
// Draw item 1
[xform translateXBy:10.0 yBy:10.0];
[xform concat];
[item1 draw];
//Draw item 2
[xform translateXBy:5.0 yBy:0.0]; // Translate relative to the
previous element.
[xform concat];
[item2 draw];
As specified in the guide, the SECOND transform should be relative to
the previous element. My understanding is that the frist transform
translated the origin to (10, 10). And the second transform
translated the origin to (15, 10).
But I don't find it correct when I implement the example in my testing
application. I implemented a custom NSView class "TransformView" and
draw this view in NSWindows.
The "drawRect" method of my TransformView class is as follow:
- (void)drawRect:(NSRect)rect
{
NSPoint testPoint = { 0.0, 0.0 };
[[NSColor whiteColor] setFill];
NSRectFill(rect);
NSColor *myColor = [NSColor grayColor];
NSAffineTransform* xform = [NSAffineTransform transform];
[xform translateXBy:10.0 yBy:10.0];
[xform concat];
[myColor setFill];
NSRect myRect = NSMakeRect(0.0, 0.0, 4.0, 4.0);
// fill 1st square
NSRectFill(myRect);
[xform translateXBy:5.0 yBy:0.0];
[xform concat];
// test point
testPoint = [xform transformPoint:testPoint];
// fill 2nd square
NSRectFill(myRect);
}
I found that the 2nd square is not drawn at (15, 10 ). Instead, it is
drawing at (25, 20)
i.e. let 1st transformation matrix be T1 and 2nd transformation matrix be T2.
The transformation matrix when drawing the 2nd square is T1 x (T1 x
T2) instead of T1 x T2
There's one strange behaviour. For the sake of debugging, I put an
NSPoint variable "testPoint" and initialize it to (0, 0). The result
of [xform transformPoint:testPoint] is correct, i.e. (15, 10), not
(25, 20).
Is there anything wrong in my code or analysis?
Is it a bug in NSAffineTransform?
Please kindly advise.
Best Regards,
Bill
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden