C++ Program to Check if a Point d lies Inside or Outside a Circle Defined by Points a, b, c in a Plane

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

Output:

advertisement
$ g++ PointWRTCircle.cpp
$ a.out
 
The points on the circle are: (2, 5), (10, 8), (3, 6)
The center of the circle is (8.7, 13.7) and radius is 9.58019
Enter the point : <x>,<y> 1 2
 
The point lies outside the circle
 
The points on the circle are: (0, 6), (9, 7), (6, 10)
The center of the circle is (4.6, 7.4) and radius is 2.95296
Enter the point : <x>,<y>6 5
 
The point lies inside the circle
------------------
(program exited with code: 0)
Press return to continue

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement

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

advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn