We shall consider a C++ Program to check if a point d lies inside or outside a circle defined by points a, b, c in a plane by using equation
s = (x-xt)^2 + (y-yt)^2 – r*r
Where, 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).
for s < 0, t lies inside the circle.
For s >0, t lies outside the circle.
For s = 0, t lies on the circle.
Algorithm
Begin Take the points at input. Declare constant L = 0 and H = 20 Declare the variables of the equation. For generating equation, generate random numbers for coefficient of x and y by using rand at every time of compilation. Calculate the center of the circle. Calculate the radius of the circle. Calculate s. if s < 0, print point lies inside the circle. else if s >0, print point lies outside the circle. else if s = 0, print point lies on the circle. End
Example Code
#include<time.h>
#include<stdlib.h>
#include<iostream>
#include<math.h>
using namespace std;
const int L= 0;
const int H = 20;
int main(int argc, char **argv) {
time_t s;
time(&s);
srand((unsigned int) s);
double x1, x2, y1, y2, x3, y3;
double a1, a2, c1, c2, r;
x1 = rand() % (H - L+ 1) + L;
x2 = rand() % (H - L + 1) + L;
x3 = rand() % (H- L + 1) + L;
y1 = rand() % (H- L + 1) + L;
y2 = rand() % (H- L+ 1) + L;
y3 = rand() % (H- L + 1) + L;
a1 = (y1 - y2) / (x1 - x2);
a2 = (y3 - y2) / (x3 - x2);
c1 = ((a1 * a2 * (y3 - y1)) + (a1 * (x2 + x3)) - (a2 * (x1 + x2))) / (2 * (a1 - a2));//calculate center of circle
c2 = ((((x1 + x2) / 2) - c1) / (-1 * a1)) + ((y1 + y2) / 2);//calculate center of circle
r = sqrt(((x3 - c1) * (x3 - c1)) + ((y3 - c2) * (y3 - c2)));//calcultate radius
cout << "The points on the circle are: (" << x1 << ", " << y1 << "), (" << x2 << ", " << y2 << "), (" << x3 << ", " << y3 << ")";
cout << "\nThe center of the circle is (" << c1 << ", " << c2 << ") and radius is " << r;
cout << "\nEnter the point : ";
int u, v;
cin >>u;
cin >>v;
double s1 = ((u - c1) * (u - c1)) + ((v - c2) * (v - c1)) - (r * r);
if (s1 < 0)
cout << "\nThe point lies inside the circle";
else if (s1 >0)
cout << "\nThe point lies outside the circle";
else
cout << "\nThe point lies on the circle";
return 0;
}Output
The points on the circle are: (8, 4), (9, 17), (5, 9) The center of the circle is (12.6364, 10.8182) and radius is 7.84983 Enter the point : 7 6 The point lies outside the circle