分享

三点画圆

 just4peterpan 2018-09-08
复制代码
- (void)drawRect:(CGRect)rect {
    CGPoint point1=CGPointMake(40, 40);
    CGPoint point2=CGPointMake(40, 80);
    CGPoint point3=CGPointMake(80, 100);
    float a=2*(point2.x-point1.x);
    float b=2*(point2.y-point1.y);
    float c=point2.x*point2.x+point2.y*point2.y-point1.x*point1.x-point1.y*point1.y;
    float d=2*(point3.x-point2.x);
    float e=2*(point3.y-point2.y);
    float f=point3.x*point3.x+point3.y*point3.y-point2.x*point2.x-point2.y*point2.y;
    //圆心
    float x=(b*f-e*c)/(b*d-e*a);
    float y=(d*c-a*f)/(b*d-e*a);
    //半径
    float r=sqrt((x-point1.x)*(x-point1.x)+(y-point1.y)*(y-point1.y));
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    [[UIColor redColor] set];
    CGContextAddArc(context, x, y, r, 0, 2*M_PI, 1);
    CGContextDrawPath(context, kCGPathStroke);
    [[UIColor greenColor] set];
    CGContextFillRect(context, CGRectMake(point1.x-0.5,point1.y-0.5,1,1));
    CGContextFillRect(context, CGRectMake(point2.x-0.5,point2.y-0.5,1,1));
    CGContextFillRect(context, CGRectMake(point3.x-0.5,point3.y-0.5,1,1));
    CGContextDrawPath(context, kCGPathStroke);
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多