3

I'm trying to draw the following image:

enter image description here

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 + (size.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:

enter image description here

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: enter image description here

Sorry for my English.

Any advice?

VansFannel
  • 45,055
  • 107
  • 359
  • 626

3 Answers3

2

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.

VansFannel
  • 45,055
  • 107
  • 359
  • 626
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.

Kurt Spindler
  • 1,311
  • 1
  • 14
  • 22
0

Does reversing your point draw order fix it?

Jeremy Murray
  • 776
  • 7
  • 7