3点画圆
解三元二次方程组
X,Y,R为未知数,x1,y1,x2,y2,x3,y3为3点坐标
(x1-X)²-(y1-Y)²=R²
(x2-X)²-(y2-Y)²=R²
(x3-X)²-(y3-Y)²=R²
- (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); }