我们将考虑使用C ++程序通过使用等式检查点d是否位于平面中由点a,b,c定义的圆的内部或外部
s = (x-xt)^2 + (y-yt)^2 – r*r
其中,对于平面上的任何点t(xt,yt),其相对于由3个点(x1,y1),(x2,y2),(x3,y3)定义的圆的位置。
对于s <0,t位于圆内。
对于s> 0,t位于圆的外面。
对于s = 0,t位于圆上。
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
#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; }
输出结果
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