CIRCLES2 - EDITORIAL

My Code is running correctly on my local machine as well as other IDE’s on sample test cases, but not on CodeChef.
Error is that my Code gives Wrong Answer
I can’t figure out what’s wrong with my code. I have tried to follow the approach mentioned in the Editorial.

#include <bits/stdc++.h>
using namespace std;

int main()
{
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	
	int test_cases; cin>>test_cases;
	
	for (int i = 0; i < test_cases; ++i)
	{
		long int X1, Y1, R1, X2, Y2, R2;
		cin>>X1>>Y1>>R1>>X2>>Y2>>R2;


		if(X1==X2 && Y1==Y2)
		{
			if(R1>R2)
			{	printf("YES\n");
				printf("%f %f\n",double(X1+R1),double(Y1)); }
			else
				printf("NO\n");
		}
		else
		{
                  //distance between centres of circles
			double dis1=(X1-X2)*(X1-X2) + (Y1-Y2)*(Y1-Y2);
			dis1=sqrt(dis1);
                      
                  //finding the point farthest from circle 2 centre on circle 1
			double y=Y1 - R1*(double(Y2-Y1)/dis1);
			double x=X1 - R1*(double(X2-X1)/dis1);

                    //distance between the point found and circle2 center

			double dis=(x-X2)*(x-X2) + (y-Y2)*(y-Y2);

			if(dis>R2*R2)
			{	printf("YES\n");
				printf("%f %f\n",double(x),double(y)); 	}
			else
				printf("NO\n");
		}
	}
}

My solution passed successfully and I don’t think it should have passed.
My approach is pretty straightforward.
In my approach if two circles are intersecting, I just answered one of the intersection points of the two circles. Now, the intersection point is on the perimeter of first circle which satisfies one of the conditions but does not satisfy the second condition as it does not lie strictly outside the second circle. Were the test cases weak or am I missing something?

Link to my solution : CodeChef: Practical coding for everyone

2 Likes

Maybe you got around due to that 10^-6 error margin.

Hello everyone,
I have used the logic that if a circle has its center coordinates as integers and also the radius as the integer,then there must be atleast 4 points who have their coordinates as an integer.
Now,two circles can have
1)No intersection-Hence the answer is yes and the point can be any one of them.
2)One point intersection-The answer is yes and the point will be anyone of those 4 points the one that doesn’t lie on the second circle.
3)Two point intersection-The answer is yes again and the point is the one which doesn’t lie on the second circle
4)circle coincide-No point
Please help me with my solution.
Link to solution-(CodeChef: Practical coding for everyone)

1 Like

In case of no intersection, first circle could lie inside second one. In that case answer is NO.
Your approach will fail in that case as you are answering YES

1 Like

Can anyone please tell where am i going wrong. I have calculated distance between two centers of the circles + radius of circle1, if this is <= radius of circle2 then answer will be no otherwise yes. Point C can be any point, firstly checked if (x1+r1,y1) is possible then (x1-r1,y1), then (x1,y1+r1) and finally (x1, y1-r1) and center also.

https://www.codechef.com/viewsolution/26008763

1 Like

can you please have a look at my code.I think you will understand it.

that case has been handled.Could you please help me with the code

Hello… could you please take a look to my code as well and help me out…

Link : CodeChef: Practical coding for everyone
please brother if you can help me… I have put in 10+ attempts over this question ; still I cannot figure out what the mistake is…

Please … Thanks in advance…

Hello… could you please take a look to my code as well and help me out…

Link : CodeChef: Practical coding for everyone
please brother if you can help me… I have put in 10+ attempts over this question ; still I cannot figure out what the mistake is…

Please … Thanks in advance…!

You are missing the case when there is two point intersection and all the other four points lie inside the bigger circle.

this is not a good programming practice to define a macro with the name of an existing data type. like #define int long long

For answering only yes or no…
Just checking this is enough…
If R1+distance(Center1, Center2) > R2 yes, else no

Can you find what is wrong with my approach:-
MY APPROACH IS TO CHECK FOR ONLY THESE 4 POINTS
that is
lx=x1-r;
ly=y1;
rx=x1+r;
ry=y1;
ux=x1;
uy=y1+r1;
dx=x1;
dy=y1-r;
IF ANY OF THESE POINTS SATISFIES THE CONDITION THEN RETURN TRUE AND write the required point else no code is given below:-----

PLEASE TELL ME WHERE I AM GOING WRONG IN MY APPROACH:-

#include<bits/stdc++.h>

using namespace std;
int main()
{
long long int i,j,k,l,m,n,o,p,q,r,t,x1,x2,y1,y2,r1,r2;
double rx,ry,lx,ly,ux,uy,dx,dy,ld,rd,ud,dd,ld1,rd1,ud1,dd1,rr1,rr2;
cin>>t;
while(t–)
{
cin>>x1>>y1>>r1>>x2>>y2>>r2;
lx=x1-r;
ly=y1;
rx=x1+r;
ry=y1;
ux=x1;
uy=y1+r1;
dx=x1;
dy=y1-r;
ld=((pow(lx-x1,2)+pow(ly-y1,2)));
ld1=((pow(lx-x2,2)+pow(ly-y2,2)));
rr1=pow(r1,2);
rr2=pow(r2,2);
if(ld<=rr1&&ld1>rr2)
{
cout<<“YES\n”;
printf("%.40lf %.40lf\n",lx,ly);
continue;
}
rd=((pow(rx-x1,2)+pow(ry-y1,2)));
rd1=((pow(rx-x2,2)+pow(ry-y2,2)));
if(rd<=rr1&&rd1>rr2)
{
cout<<“YES\n”;
printf("%.40lf %.40lf\n",rx,ry);
continue;
}
ud=((pow(ux-x1,2)+pow(uy-y1,2)));
ud1=((pow(ux-x2,2)+pow(uy-y2,2)));
if(ud<=rr1&&ud1>rr2)
{
cout<<“YES\n”;
printf("%.40lf %.40lf\n",ux,uy);
continue;
}
dd=((pow(dx-x1,2)+pow(dy-y1,2)));
dd1=((pow(dx-x2,2)+pow(dy-y2,2)));
if(dd<=rr1&&dd1>rr2)
{
cout<<“YES\n”;
printf("%.40lf %.40lf\n",dx,dy);
continue;
}
cout<<“NO\n”;
}
return 0;
}

Can someone please help me out

Did you get the answer ? I think the test cases were weak as that point would not be strictly outside the second circle.

Tester’s solution will give you WA, use double instead of int . I had a hard time figuring out why is this solution giving WA :relieved: .

@taran_1407
I have 2 solution(s) :slight_smile:

  1. which uses the formula for intersection of line and circle, this method involves too much calculation and gives WA :frowning:

Other solution uses trig. and it gives AC as it only does 2 calculations…

Logic of both solutions is ditto same.

So, we should do as less calculations as possible in geometry problems to not get WA?

What about the solution using (x2*(n+m) - x3*m)/n to calculate the x coordinate (similarly for y coordinate) ? I think this is the most efficient one. I tried using C++ trigo functions (tan() and atan()) but I ended up getting WA every time (even after taking care of precision).

double theta = Math.atan2(y2-y1, x2-x1);
double x3 = x1-r1Math.cos(theta), y3 = y1-r1Math.sin(theta);
if(dist(x3, y3, x2, y2) - r2 > eps){
pn(“YES”);
print(x3, y3);
}else pn(“NO”);
}
this case can work for all circles then why are you taking extra two cases,like if the center is same and y1<y2 ,y1+r1>y2+r2 ??
the angle pointed by yellow line must be subtracted from 180?
r1sin(θ) and r1cos(θ) will just give the distance ,how you are calculating the points in 2d co-ordinates?