views:

98

answers:

1

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?

+2  A: 

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);
}
Brad Goss
I notice that there is technology in XCode to create Bezier curves as well (appendBezier...), which should give greater control over the shape of the hypotenuese.John Doner
John R Doner
I believe your referring to NSBezierPath, this API is not available on the iPhone.
Brad Goss
CGContextAddCurveToPoint() does exist on the iPhone, and it would let you work with Bezier curves in this context.
Brad Larson
Nifty! Good to know, thanks Brad.
Brad Goss