Andrew Johnson
Andrew Johnson

Reputation: 13286

Drawing a convex shape with CoreGraphics

I am trying to create a right triangle with a convex hypotenuse.

  1. Start with a square
  2. Cut off half of it diagonally such that what is left is a right triangle.
  3. Curve the hypotenuse inwards

How would I achieve this with CoreGraphics? Should I inscribe an ellipse over half of the rectangle?

Upvotes: 0

Views: 554

Answers (1)

Brad G
Brad G

Reputation: 2593

I'm not very good with math, maybe someone can elaborate on the tangent math.

Here is a custom subview drawing function to draw what your looking for. Simply make a few lines, use an arc for the hypotenuse.

- (void)drawRect:(CGRect)dirtyRect {
 CGContextRef ctx = UIGraphicsGetCurrentContext();
 CGContextMoveToPoint(ctx, 0, 50);
 CGContextAddLineToPoint(ctx, 100, 50);
 CGContextAddLineToPoint(ctx, 100, 0);

 CGPoint tangent1 = CGPointMake(85, 25);
 CGPoint tangent2 = CGPointMake(10, 50);
 CGContextAddArcToPoint(ctx, tangent1.x, tangent1.y, tangent2.x, tangent2.y, 125);

 CGFloat redComponents[4] = { 1., 0., 0., 1. };
 CGContextSetFillColor(ctx, redComponents);
 CGContextFillPath(ctx);
}

Upvotes: 2

Related Questions