# 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<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:

\$ 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.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement

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

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, 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 & technical discussions at Telegram SanfoundryClasses.