Drawing a bridge with Quartz 2D
I'm trying to draw the following image:
Using this objective-c code:
CGContextSetFillColorWithColor(ctx, [[UIColor redColor] CGColor]);
CGContextMoveToPoint(ctx, position.X - (size.width / 2), position.Y - (size.height / 2));
CGContextAddLineToPoint(ctx, position.X - (size.width / 2), position.Y + (size.height / 2));
CGContextAddLineToPoint(ctx, position.X - (size.width / 4), position.Y + (size.height / 2));
CGContextAddArc(ctx, position.X, position.Y + (size.height / 2), (size.width / 4), -M_PI, M_PI, 0);
CGContextAddLineToPoint(ctx, position.X + (size.width / 2), position.Y + (siz开发者_运维问答e.height / 2));
CGContextAddLineToPoint(ctx, position.X + (size.width / 2), position.Y - (size.height / 2));
CGContextFillPath(ctx);
But it doesn't work. I get this image:
The width of the image is size.width and the height is size.height.
The origin is (position.X, position.Y)
. This point is in center, at (size.width /2, size.height / 2).
The first point is the upper left corner, and the second one is the bottom left corner. And then continues to the right.
Here is a better explanation:
Sorry for my English.
Any advice?
I've changed this line:
CGContextAddArc(ctx, position.X, position.Y + (size.height / 2), (size.width / 4), -M_PI, M_PI, 0);
With the following:
CGContextAddArc(ctx, position.X, position.Y + (size.height / 2), (size.width / 4), -M_PI, 0, 0);
And now it works!!! I had an error with the second angle. Instead of M_PI
, the correct is 0.
You stated: The width of the image is size.width and the height is size.height. The origin is (position.X, position.Y), and this point is in center, at (size.width /2, size.height / 2).
First thought is that in general, your center point is actually at (position.X + size.width/2.0, position.Y + size.width/2.0), (wrapping that in floorf if you desire).
If that doesn't work, my advice is to create CGPoint, CGRect, etc, for every intermediate value to make all of your calculations explicit. Then if need be, you can NSLog them, find out exactly where your calculations go wrong.
Does reversing your point draw order fix it?
精彩评论