假设我们有两个圆(中心点和半径值),我们必须检查一个圆是否适合另一个圆。有三种可能的原因。
较小的圆圈完全位于较大的圆圈内,彼此不接触。在这种情况下,中心之间的距离和较小的半径之和小于较大的半径。因此,较小的将位于较大的内部。
第二种情况是较小的圆圈位于较大的圆圈内,但也触及较大圆圈的圆周。
第三种情况是,较小圆的某些部分位于较大圆的内部。
为了解决这个问题,我们必须找到两个中心之间的距离,然后使用距离和半径值来确定这些情况。
#include <iostream> #include <cmath> using namespace std; void isCircleInside(int x_big, int y_big, int x_small, int y_small, int r_big, int r_small) { int distSq = sqrt(((x_big - x_small) * (x_big - x_small)) + ((y_big - y_small) * (y_big - y_small))); if (distSq + r_small == r_big) cout << "Inside the bigger circle, touching circimferene" << endl; else if (distSq + r_small < r_big) cout << "Completely inside the bigger circle" << endl; else cout << "Not inside the bigger circle" << endl; } int main() { int x1 = 10, y1 = 8; int x2 = 1, y2 = 2; int r1 = 30, r2 = 10; isCircleInside(x1, y1, x2, y2, r1, r2); }
输出结果
Completely inside the bigger circle