สมมุติว่าให้วงกลมหนึ่งวง (พิกัดศูนย์กลางและรัศมี) ให้อีกจุดหนึ่งด้วย เราต้องหาว่าจุดนั้นอยู่ในวงกลมหรือไม่ เราต้องหาระยะทางของจุดที่กำหนดจากจุดศูนย์กลางวงกลม หากระยะทางนั้นน้อยกว่าหรือเท่ากับรัศมี แสดงว่าอยู่ภายในวงกลม ไม่เช่นนั้นก็ไม่ใช่
ตัวอย่าง
#include <iostream>
#include <cmath>
using namespace std;
bool isInsideCircle(int cx, int cy, int r, int x, int y) {
int dist = (x - cx) * (x - cx) + (y - cy) * (y - cy);
if ( dist <= r * r)
return true;
else
return false;
}
int main() {
int x = 4, y = 4, cx = 1, cy = 1, rad = 6;
if(isInsideCircle(cx, cy, rad, x, y)){
cout <<"Inside Circle";
} else {
cout <<"Outside Circle";
}
} ผลลัพธ์
Inside Circle