How to draw a triangle programmatically

前端 未结 3 1133
长情又很酷
长情又很酷 2020-12-13 04:43

I have a triangle solver, I want a way to use the values I get from the answer to draw a triangle to the screen that matches it.

3条回答
  •  臣服心动
    2020-12-13 04:45

    If you subclass a UIView you can implement something like this in drawRect to draw a triangle:

    -(void)drawRect:(CGRect)rect
    {
        CGContextRef ctx = UIGraphicsGetCurrentContext();
    
        CGContextBeginPath(ctx);
        CGContextMoveToPoint   (ctx, CGRectGetMinX(rect), CGRectGetMinY(rect));  // top left
        CGContextAddLineToPoint(ctx, CGRectGetMaxX(rect), CGRectGetMidY(rect));  // mid right
        CGContextAddLineToPoint(ctx, CGRectGetMinX(rect), CGRectGetMaxY(rect));  // bottom left
        CGContextClosePath(ctx);
    
        CGContextSetRGBFillColor(ctx, 1, 1, 0, 1);
        CGContextFillPath(ctx);
    }
    

提交回复
热议问题