สมมติมี 2 Circles (จุดศูนย์กลางและรัศมี) ต้องตรวจสอบ Circle หนึ่งอยู่ในอีกวงหนึ่งหรือไม่ มี 3 กรณี:
- Circle เล็กอยู่ภายใน Circle ใหญ่สนิท ไม่สัมผัส: Distance + r_small < r_big
- Circle เล็กอยู่ภายในแต่สัมผัส Circumference: Distance + r_small = r_big
- บางส่วนของ Circle เล็กอยู่ภายใน
หา Distance ระหว่างศูนย์กลาง แล้วใช้ Distance และ Radii กำหนดกรณี
ตัวอย่างโค้ด C++
#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