Point lies inside, outside or on the circle

C program to find whether the given point lies inside, outside or on the circle.

Program in method 2 tab is the little improvisation of the program in method 1 tab. In first program floating computations are there, whereas, in second one modifications are made to avoid the floating computations.

Program

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
    int x, y, xc, yc, r;
    float dist;

    printf("Enter the x and y coordinate of center of the circle\t:");
    scanf("%d%d",&xc,&yc);
    printf("Enter the radius of the circle\t:");
    scanf("%d",&r);
    printf("Enter the x and y coordinates of a point\t:");
    scanf("%d%d",&x,&y);

    dist = sqrt(((x-xc) * (x-xc)) + ((y-yc) * (y-yc)));
    if(dist < r)
		printf("\nPoint lies inside the circle");
    else if(dist > r)
		printf("\nPoint lies outside the circle");
    else
		printf("\nPoint lies on the circle");

    getch();
}

Output

********** Run1 **********
Enter the x and y coordinate of center of the circle    :4
4
Enter the radius of the circle  :5
Enter the x and y coordinates of a point        :9
4

Point lies on the circle


********** Run2 **********
Enter the x and y coordinate of center of the circle    :4
4
Enter the radius of the circle  :5
Enter the x and y coordinates of a point        :6
7

Point lies inside the circle


********** Run3 **********
Enter the x and y coordinate of center of the circle    :4
4
Enter the radius of the circle  :5
Enter the x and y coordinates of a point        :11
2

Point lies outside the circle

Explanation

Coordinates of centre of the circle and radius are taken as an input from the user in the variables say, 'xc', 'yc', and 'r'. Coordinates of the point to be checked are taken as an input in the variables named say, 'x', and 'y'.

The idea is to compute distance of point from center of the circle.
If distance is less than the radius, then the point is inside the circle.
If distance is greater than the radius, then the points is outside the circle.
If distance is equal to radius, then the point lies inside the circle.

distance between point(x,y) and centre (xc,yc) = sqrt(((x-xc) * (x-xc)) + ((y-yc) * (y-yc)))

The above method uses floating computations due to sqrt(), as square root of a number is of float type.

To avoid floating computations above method can be modified as follows:
For the point to lie on the circle,
r = sqrt(((x-xc) * (x-xc)) + ((y-yc) * (y-yc)))
r*r = ((x-xc) * (x-xc)) + ((y-yc) * (y-yc))
So, the conditions can be,
For point to be on the circle,
(((x-xc) * (x-xc)) + ((y-yc) * (y-yc)) - (r * r)) == 0
For point to be inside the circle,
(((x-xc) * (x-xc)) + ((y-yc) * (y-yc)) - (r * r)) < 0
For point to be outside the circle,
(((x-xc) * (x-xc)) + ((y-yc) * (y-yc)) - (r * r)) > 0
The program for the same is in "method 2" tab.

Program

#include<stdio.h>
#include<conio.h>
void main()
{
    int x, y, xc, yc, r, temp;

	printf("Enter the x and y coordinate of center of the circle\t:");
	scanf("%d%d",&xc,&yc);
	printf("Enter the radius of the circle\t:");
	scanf("%d",&r);
	printf("Enter the x and y coordinates of a point\t:");
	scanf("%d%d",&x,&y);

	temp = (((x-xc) * (x-xc)) + ((y-yc) * (y-yc)) - (r * r));
	if(temp < 0)
		printf("\nPoint lies inside the circle");
	else if(temp > 0)
		printf("\nPoint lies outside the circle");
	else
		printf("\nPoint lies on the circle");

	getch();
}

Output

********** Run1 **********
Enter the x and y coordinate of center of the circle    :4
4
Enter the radius of the circle  :5
Enter the x and y coordinates of a point        :9
4

Point lies on the circle


********** Run2 **********
Enter the x and y coordinate of center of the circle    :4
4
Enter the radius of the circle  :5
Enter the x and y coordinates of a point        :6
7

Point lies inside the circle


********** Run3 **********
Enter the x and y coordinate of center of the circle    :4
4
Enter the radius of the circle  :5
Enter the x and y coordinates of a point        :11
2

Point lies outside the circle

Explanation

Coordinates of centre of the circle and radius are taken as an input from the user in the variables say, 'xc', 'yc', and 'r'. Coordinates of the point to be checked are taken as an input in the variables named say, 'x', and 'y'.

The idea is to compute distance of point from center of the circle.
If distance is less than the radius, then the point is inside the circle.
If distance is greater than the radius, then the points is outside the circle.
If distance is equal to radius, then the point lies inside the circle.

distance between point(x,y) and centre (xc,yc) = sqrt(((x-xc) * (x-xc)) + ((y-yc) * (y-yc)))

The above formula uses floating computations due to sqrt(), as square root of a number is of float type.

To avoid floating computations following modifications can be done:
For the point to lie on the circle,
r = sqrt(((x-xc) * (x-xc)) + ((y-yc) * (y-yc)))
r*r = ((x-xc) * (x-xc)) + ((y-yc) * (y-yc))
So, the conditions can be,
For point to be on the circle,
(((x-xc) * (x-xc)) + ((y-yc) * (y-yc)) - (r * r)) == 0
For point to be inside the circle,
(((x-xc) * (x-xc)) + ((y-yc) * (y-yc)) - (r * r)) < 0
For point to be outside the circle,
(((x-xc) * (x-xc)) + ((y-yc) * (y-yc)) - (r * r)) > 0