## 190-Circle Through Three Points

Moderator: Board moderators

### Re: 190-Circle Through Three Points....What appends here

i got WA.....But all given above input and output is satisfy..... help me guru

my code:
Code: Select all
`#include<stdio.h>#include<math.h>int main(){  //freopen("a.in","r",stdin);  //freopen("b.in","w",stdout);  double x1,y1,x2,y2,y3,x3;  double h,k,r,g,f,c;  int flag=0;  while(1){    if(scanf("%lf %lf %lf %lf %lf %lf",&x1,&y1,&x2,&y2,&x3,&y3)!=6)       break;        double ma,mb;        char p='-',q='-',a='+',b='+',s='+';         if(x1==x2)       ma=1;    else       ma = (y1-y2)/(x1-x2);    if(x2==x3)       mb=1;    else       mb = (y2-y3)/(x2-x3);        h=((y1-y3)*ma*mb + mb*(x1+x2)-ma*(x2+x3))/(2*(mb-ma));    k=((x3-x1)-(y1+y2)*ma+(y2+y3)*mb)/(2*(mb-ma));    r=sqrt((x1-h)*(x1-h) + (y1-k)*(y1-k));       g=-2*h;    f=-2*k;    //c=h*h+k*k-r*r;    c = -(x2 * x2 + y2 * y2 + g * x2 + f * y2);    if(h<=0){      p='+';      h=-1*h;    }    if(k<=0){      q ='+';      k=-1*k;    }     printf("(x %c %.3lf)^2 + (y %c %.3lf)^2 = %.3lf^2\n",p,h,q,k,r);     if(g<0){       a='-';      g=-1*g;    }    if(f<0){      b ='-';      f=-1*f;    }    if(c<0){       s='-';      c=-1*c;    }    printf("x^2 + y^2 %c %.3lfx %c %.3lfy %c %.3lf = 0\n\n",a,g,b,f,s,c);  }  return 0;}`
Rizoan toufiq
New poster

Posts: 5
Joined: Mon Apr 21, 2008 9:38 pm

### Re: 190-Circle Through Three Points

EDIT: I got an AC on mine... A few pointers though:
1. Use fabs... And not -h
2. No need to make it x^2 rather than (x + 0.000)^2
3. Division by zero can be avoided if you don't find slope.
Aim is to find equation: ax + by + c = 0
Slope:
m = (y2 - y1)/(x2 - x1)
also m = -a/b
take a = y1 - y2
and b = x2 - x1
then c = -(ax1 + by1)
Perpendicular to ax + by + c = 0 is bx - ay + k = 0

Two methods that you could be used:
1. Find two lines passing through the points, then perpendicular bisectors, center and then the radius.
2. Find one line (with first two points), and a random circle passing through them. Find family of circles, third point lies on it.
drcoolsanta
New poster

Posts: 1
Joined: Mon Jul 19, 2010 5:31 pm

Previous