## 190 - Circle Through Three Points

Moderator: Board moderators

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Contact:
Ami ekhono shopno dekhi...
HomePage

kintu
New poster
Posts: 6
Joined: Sun Mar 18, 2007 11:31 am
Location: In home
Why i am getting WA again & again. If can plzzzzzzzzz check my code or give me some i/o for which my code show WA. plzzzz

Code: Select all

``````#include<stdio.h>
#include<math.h>

main()
{
// freopen("input.txt","rt",stdin);
//    freopen("out.txt","wt",stdout);

double x1,x2,x3,y1,y2,y3,a,b,g,f,r,c,eps=1e-7;
char f_sign,g_sign,c_sign;

while(scanf("%lf %lf %lf %lf %lf %lf",&x1,&y1,&x2,&y2,&x3,&y3)==6)
{
a=(x1*x1+y1*y1)-(x2*x2+y2*y2);
b=(x2*x2+y2*y2)-(x3*x3+y3*y3);
if(2*(y2-y3)*(x1-x2) - 2*(y1-y2)*(x2-x3))
g=(a*(x2-x3) - b*(x1-x2) )/( 2*(y2-y3)*(x1-x2) - 2*(y1-y2)*(x2-x3) );
else
g=0;
if(2*(x1-x2))
f=(-a-2*g*(y1-y2))/(2*(x1-x2));
else
f=0;
c=-(x1*x1+y1*y1+2*f*x1+2*g*y1);
r=sqrt(f*f+g*g-c);

if(f<0)
f_sign='-';
else
f_sign='+';
f=fabs(f);
if(g<0)
g_sign='-';
else
g_sign='+';
g=fabs(g);
if(c<0)
c_sign='-';
else
c_sign='+';
c=fabs(c);

if(f)
printf("(x %c %.3lf)^2",f_sign,f+eps);
else
printf("x^2");
if(g)
printf(" + (y %c %.3lf)^2 = %.3lf^2\n",g_sign,g+eps,r+eps);
else
printf(" + y^2 = %.3lf^2\n",r+eps);

printf("x^2 + y^2");
if(2*f)
printf(" %c %.3lfx",f_sign,2*f+eps);
if(2*g)
printf(" %c %.3lfy",g_sign,2*g+eps);
//if(c)
printf(" %c %.3lf",c_sign,c+eps);
printf(" = 0");
printf("\n\n");

}

return 0;
}``````

AcmNightivy
New poster
Posts: 36
Joined: Tue Dec 04, 2007 10:20 am

### 190WA!!ReallyStrange~Help

I test all the cases i found and all results is ok..i suppose the output like:
(x + 0.000)^2 is the same as x^2..plz help..Thx..And here is my code:

Code: Select all

``````#include <stdio.h>
#include "math.h"

int main ()
{
double x1, y1, x2, y2 ,x3, y3;
double a, b, i, f, g, l;
double h, k, r, c, d, e;
bool first = true;

while (scanf ("%lf%lf%lf%lf%lf%lf", &x1, &y1, &x2, &y2, &x3, &y3) != EOF)
{
if (first)
{
first = false;
}
else
printf ("\n");

i = x1 * x1 + y1 * y1 - x2 * x2 - y2 * y2;
l = x3 * x3 + y3 * y3 - x2 * x2 - y2 * y2;
a = 2 * (x1 - x2);
b = 2 * (y1 - y2);
f = 2 * (x3 - x2);
g = 2 * (y3 - y2);
k = (i * f - a * l) / (f * b - a * g);
h = (i * g - b * l) / (a * g - b * f);

r = sqrt ((h - x1) * (h - x1) + (k - y1) * (k - y1));
c = -2 * h;
d = -2 * k;
e = h * h + k * k - r * r;

//(x - 3.000)^2 + (y + 2.000)^2 = 5.000^2
printf ("(x ");
if (h < 0)
printf ("+ %.3lf)^2 + (y ", -h);
else
printf ("- %.3lf)^2 + (y ", h);

if (k < 0)
printf ("+ %.3lf)^2 = %.3lf^2\n", -k, r);
else
printf ("- %.3lf)^2 = %.3lf^2\n", k, r);

//x^2 + y^2 - 6.000x + 4.000y - 12.000 = 0
printf ("x^2 + y^2 ");
if (c < 0)
printf ("- %.3lfx ", -c);
else
printf ("+ %.3lfx ", c);
if (d < 0)
printf ("- %.3lfy ", -d);
else
printf ("+ %.3lfy ", d);
if (e < 0)
printf ("- %.3lf = 0\n", -e);
else
printf ("+ %.3lf = 0\n", e);
}
return 0;
}``````

AcmNightivy
New poster
Posts: 36
Joined: Tue Dec 04, 2007 10:20 am
Almost three weeks...got mad...I have test all of the cases I found..
Does there any problem with +0.000 and -0.000??

sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York
Particularly this one..

assasin
New poster
Posts: 7
Joined: Wed Feb 13, 2008 3:59 pm

### 190 Getting WA

I don't know why I am getting WA
In fact , i am not clear about the problem
if any coefficient gets zero then what has to be done?

Please give some critical input and output
and why i am getting WA?

Here is my code:

Code: Select all

``````#include <stdio.h>
#include <math.h>

#define epsilon 0.00001

int main(){

double count = 0.0;
double x1,x2,x3,y1,y2,y3;

while(scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3) != EOF ){

double a1,a2,b1,b2,c1,c2,h,k,r,c,d,e;

a1 = 2*(x2-x1);
b1 = 2*(y2-y1);
c1 = x1*x1 + y1*y1 - x2*x2 - y2*y2;

a2 = 2*(x3-x2);
b2 = 2*(y3-y2);
c2 = x2*x2 + y2*y2 - x3*x3 - y3*y3;

h = (b1*c2 - b2*c1) / (a1*b2 - a2*b1);
k = (a2*c1 - a1*c2) / (a1*b2 - a2*b1);
r = pow( (x1 - h)*(x1 - h) + (y1 - k )*(y1 - k ) , 0.5) ;

c = -2*h;
d = -2*k;
e = h*h + k*k -r*r;

//printf("h %lf , k %lf , r %lf\n",h,k,r);

if(count)
printf("\n");
count++;

//1st equation of the form 	//(x - 3.000)^2 + (y + 2.000)^2 = 5.000^2 ;

printf("(x ");
if( h >= 0.00)
printf("- %0.3lf)^2 + (y ",h);
else printf("+ %0.3lf)^2 + (y ",fabs(h));

if(k >= 0.00)
printf("- %0.3lf)^2 = %0.3lf^2\n",k,r);

else printf("+ %0.3lf)^2 = %0.3lf^2\n",fabs(k),r);

//2nd equation of the form 	//x^2 + y^2 - 6.000x + 4.000y - 12.000 = 0 ;

printf("x^2 + y^2 ");

if(c == -0.0)
printf("- %0.3lfx ",fabs(c));
else if(c >= 0.00)
printf("+ %0.3lfx ",c);
else printf("- %0.3lfx ",fabs(c));

if(d == -0.0)
printf("- %0.3lfy ",fabs(d));
else if(d >= 0.00)
printf("+ %0.3lfy ",d);
else printf("- %0.3lfy ",fabs(d));

if( e == -0.00)
printf("- %0.3lf = 0\n",fabs(e));
else if(e >= 0.000)
printf("+ %0.3lf = 0\n",e);
else printf("- %0.3lf = 0\n",fabs(e));
}
return 0;
}
``````
Thanks

Obaida
A great helper
Posts: 380
Joined: Wed Jan 16, 2008 6:51 am

### Re: 190-Circle Through Three Points

I tried to update my code.. But after updating still got wa!!!
Here is my code.

Code: Select all

``removed``
Last edited by Obaida on Thu Mar 12, 2009 6:08 am, edited 1 time in total.
try_try_try_try_&&&_try@try.com
This may be the address of success.

Moshiur Rahman
New poster
Posts: 13
Joined: Mon Sep 08, 2008 6:57 pm

### Re: 190-Circle Through Three Points

"Print a single blank line after each equation pair."
Try this:

Code: Select all

``````0.0 0.0 1.0 0.0 0.0 2.0
``````
Output:

Code: Select all

``````(x - 0.500)^2 + (y - 1.000)^2 = 1.118^2
x^2 + y^2 - 1.000x - 2.000y - 0.000 = 0

``````
Never think too hard, let ideas come to you...

Obaida
A great helper
Posts: 380
Joined: Wed Jan 16, 2008 6:51 am

### Re: 190-Circle Through Three Points

But the I/O posted by jan vai above drove me wrong in printing blank line. Thank you for helping me... thanks a lot!
try_try_try_try_&&&_try@try.com
This may be the address of success.

Moshiur Rahman
New poster
Posts: 13
Joined: Mon Sep 08, 2008 6:57 pm

### Re: 190-Circle Through Three Points

well... I got AC by printing blank line after each testcases, but got WA by printing blank line between testcases.
Never think too hard, let ideas come to you...

Obaida
A great helper
Posts: 380
Joined: Wed Jan 16, 2008 6:51 am

### Re: 190-Circle Through Three Points

Same for me also... Actually m not sloving now a days. A bit busy.. Hope to come back.
&&&&& Thank you also. try_try_try_try_&&&_try@try.com
This may be the address of success.

New poster
Posts: 1
Joined: Thu Sep 17, 2009 6:30 am

### Re: 190-Circle Through Three Points

all the sample input and outputs are matching!! still y wrong answer???

#include<stdio.h>
#include<math.h>

const double pi=3.141592653589793;
int main(){
double x1, y1, x2, y2, x3, y3;
double h, k, r, c;
double m1, m2;
char a, b, s;
while(scanf("%lf%lf%lf%lf%lf%lf", &x1, &y1, &x2, &y2, &x3, &y3)!=EOF){
m1= (y2-y1)/(x2-x1);
m2= (y3-y1)/(x3-x1);
if (m2==0){
k=(x2-x3)/m1/2+(y1+y2)/2;
h=(x1+x3)/2;
}if (m1==0){
k=(x3-x2)/m2/2+(y1+y3)/2;
h=(x1+x2)/2;
}else if (m1!=0&&m2!=0){
h=((x1+x2)/m1-(x1+x3)/m2+y2-y3)/2/(1/m1 - 1/m2);
k=( h-(x1+x2)/2)/(-m1)+(y1+y2)/2;
}
r= sqrt((h-x1)*(h-x1) + (k-y1)*(k-y1));
c= ((r*r)-(h*h)-(k*k));
a=(h<0)?'+' : '-';
b=(k<0)? '+' : '-';
s=(c<0)? '+' : '-';
if(k==0){
printf("(x %1c %.3lf)^2 + y^2 = %.3lf^2\n", a, fabs(h), r);
printf("x^2 + y^2 %1c %.3lfx %1c %.3lf = 0\n", a, fabs(2*h), s, fabs(c));
}else if(h==0){
printf("x^2 + (y %1c %.3lf)^2 = %.3lf^2\n", b, fabs(k), r);
printf("x^2 + y^2 %1c %.3lfy %1c %.3lf = 0\n", b, fabs(2*k), s, fabs(c));
}

else if(k!=0&&h!=0){
printf("(x %1c %.3lf)^2 + (y %1c %.3lf)^2 = %.3lf^2\n", a, fabs(h), b, fabs(k), r);
printf("x^2 + y^2 %1c %.3lfx %1c %.3lfy %1c %.3lf = 0\n", a, fabs(2*h), b, fabs(2*k), s, fabs(c));
}

}

return 0;

}

Rizoan toufiq
New poster
Posts: 4
Joined: Mon Apr 21, 2008 9:38 pm
.
Last edited by Rizoan toufiq on Tue Sep 29, 2015 6:23 pm, edited 1 time in total.

drcoolsanta
New poster
Posts: 1
Joined: Mon Jul 19, 2010 5:31 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.

No.47WK
New poster
Posts: 4
Joined: Thu Mar 31, 2011 1:25 pm

### 190 WA

Code: Select all

``````#include<iostream>
#include<cstdio>
using namespace std;
#include<cmath>
double xl(double x1,double y1,double x2,double y2,double &a)
{
if(y2==y1)
return false;
else
{
a=(x1-x2)/(y2-y1);
return true;
}
}
void out(double x1,double y1,double x2,double y2,double x3,double y3)
{
double a,b,x4=(x1+x2)/2,y4=(y1+y2)/2,x5=(x1+x3)/2,y5=(y1+y3)/2,x0,y0,r,sum;
bool o,p;
o=xl(x1,y1,x2,y2,a);
p=xl(x1,y1,x3,y3,b);
if(o)
{
if(p)
{
x0=(a*x4-b*x5+y5-y4)/(a-b);
y0=a*(x0-x4)+y4;
}
else
{
x0=x5;
y0=a*(x0-x4)+y4;
}
}
else
{
x0=x4;
y0=b*(x0-x5)+y5;
}
r=sqrt((x1-x0)*(x1-x0)+(y1-y0)*(y1-y0));
if(x0>0)
{
if(y0>0)
printf("(x - %.3lf)^2 + (y - %.3lf)^2 = %.3lf^2\n",x0,y0,r);
else if(y0==0)
printf("(x - %.3lf)^2 + y^2 = %.3lf^2\n",x0,r);
else
printf("(x - %.3lf)^2 + (y + %.3lf)^2 = %.3lf^2\n",x0,-y0,r);
}
else if(x0==0)
{
if(y0>0)
printf("x^2 + (y - %.3lf)^2 = %.3lf^2\n",y0,r);
else if(y0==0)
printf("x^2 + y^2 = %.3lf^2\n",r);
else
printf("x^2 + (y + %.3lf)^2 = %.3lf^2\n",-y0,r);
}
else
{
if(y0>0)
printf("(x + %.3lf)^2 + (y - %.3lf)^2 = %.3lf^2\n",-x0,y0,r);
else if(y0==0)
printf("(x + %.3lf)^2 + y^2 = %.3lf^2\n",-x0,r);
else
printf("(x + %.3lf)^2 + (y + %.3lf)^2 = %.3lf^2\n",-x0,-y0,r);
}
if(x0>0)
{
if(y0>0)
printf("x^2 + y^2 - %.3lfx - %.3lfy",2*x0,2*y0);
else if(y0==0)
printf("x^2 + y^2 - %.3lfx",2*x0);
else
printf("x^2 + y^2 - %.3lfx + %.3lfy",2*x0,-2*y0);
}
else if(x0==0)
{
if(y0>0)
printf("x^2 + y^2 - %.3lfy",2*y0);
else if(y0==0)
printf("x^2 + y^2");
else
printf("x^2 + y^2 + %.3lfy",-2*y0);
}
else
{
if(y0>0)
printf("x^2 + y^2 + %.3lfx - %.3lfy",-2*x0,2*y0);
else if(y0==0)
printf("x^2 + y^2 + %.3lfx -",-2*x0);
else
printf("x^2 + y^2 + %.3lfx + %.3lfy",-2*x0,-2*y0);
}
sum=-(x0*x0+y0*y0-r*r);
if(sum>0.00001)
printf("  - %.3lf = 0\n",sum);
else if(sum<0)
printf("  + %.3lf = 0\n",fabs(sum));
else
printf(" = 0\n");

}
int main()
{
double x1,x2,x3,y1,y2,y3,p=0;
while(cin>>x1>>y1>>x2>>y2>>x3>>y3)
{
if(p)
cout<<endl;
out(x1,y1,x2,y2,x3,y3);
++p;
}
return 0;
}

``````
give me some I/O to help me get though this boring problem please.
I don't know which part of my code is wrong