C Program to Find if a Point Lies Inside or Outside a Circle

This is a C Program to Check if a Point d lies Inside or Outside a Circle Defined by Points a, b, c in a Plane. For any point t (xt, yt) on the plane, its position with respect to the circle defined by 3 points (x1, y1) , (x2, y2), (x3, y3).
s = (x-xt)^2 + (y-yt)^2 – r*r
If s < 0, t lies inside the circle; if s > 0, t lies outside the circle; if s = 0, t lies on the circle.

Here is source code of the C Program to Check if a Point d lies Inside or Outside a Circle Defined by Points a, b, c in a Plane. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. const int LOW = 0;
  6. const int HIGH = 10;
  7. int main(int argc, char **argv) {
  8.     time_t seconds;
  9.     time(&seconds);
  10.     srand((unsigned int) seconds);
  11.  
  12.     double x1, x2, y1, y2, x3, y3;
  13.     double m1, m2, c1, c2, r;
  14.     x1 = rand() % (HIGH - LOW + 1) + LOW;
  15.     x2 = rand() % (HIGH - LOW + 1) + LOW;
  16.     x3 = rand() % (HIGH - LOW + 1) + LOW;
  17.     y1 = rand() % (HIGH - LOW + 1) + LOW;
  18.     y2 = rand() % (HIGH - LOW + 1) + LOW;
  19.     y3 = rand() % (HIGH - LOW + 1) + LOW;
  20.     m1 = (y1 - y2) / (x1 - x2);
  21.     m2 = (y3 - y2) / (x3 - x2);
  22.  
  23.     c1 = ((m1 * m2 * (y3 - y1)) + (m1 * (x2 + x3)) - (m2 * (x1 + x2))) / (2
  24.             * (m1 - m2));
  25.     c2 = ((((x1 + x2) / 2) - c1) / (-1 * m1)) + ((y1 + y2) / 2);
  26.     r = sqrt(((x3 - c1) * (x3 - c1)) + ((y3 - c2) * (y3 - c2)));
  27.  
  28.     printf("The points on the circle are: (%lf, %lf), (%lf, %lf), (%lf, %lf)\n",
  29.             x1, y1, x2, y2, x3, y3);
  30.     printf("The center of the circle is: (%lf, %lf) and radius is: %lf", c1,
  31.             c2, r);
  32.  
  33.     printf("\nEnter the point : <x>,<y>");
  34.     int x, y;
  35.     scanf("%lf", &x);
  36.     scanf("%lf", &y);
  37.  
  38.     double s = ((x - c1) * (x - c1)) + ((y - c2) * (y - c1)) - (r * r);
  39.     if (s < 0)
  40.         printf("\nThe point lies inside the circle");
  41.     else if (s > 0)
  42.         printf("\nThe point lies outside the circle");
  43.     else
  44.         printf("\nThe point lies on the circle");
  45.     return 0;
  46. }

Output:

$ gcc PointWRTCircle.c
$ ./a.out
 
The points on the circle are: (10.000000, 0.000000), (8.000000, 4.000000), (7.000000, 2.000000)
The center of the circle is: (9.250000, 1.875000) and radius is: 2.253470
Enter the point : <x>,<y> 
2 3
The point lies outside the circle

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.