สมมติว่าเรามีวงกลมและเส้นตรงอีกเส้น งานของเราคือค้นหาว่าเส้นนั้นสัมผัสกับวงกลมหรือตัดกับวงกลมหรือไม่ มิฉะนั้น เส้นจะผ่านด้านนอก ดังนั้นจึงมีสามกรณีที่แตกต่างกันดังนี้ -

ที่นี่เราจะแก้ปัญหาโดยทำตามขั้นตอน เหล่านี้เป็นเหมือนด้านล่าง −
- หา P ตั้งฉากระหว่างจุดศูนย์กลางและกำหนดเส้นตรง
- เปรียบเทียบ P กับรัศมี r −
- ถ้า P> r แล้วข้างนอก
- ถ้า P =r ให้แตะ
- อย่างอื่นข้างใน
เพื่อให้ได้ระยะตั้งฉาก เราต้องใช้สูตรนี้ (จุดศูนย์กลางคือ (h, k))
$$\frac{ah+bk+c}{\sqrt{a^2+b^2}}$$
ตัวอย่าง
#include <iostream>
#include <cmath>
using namespace std;
void isTouchOrIntersect(int a, int b, int c, int h, int k, int radius) {
int dist = (abs(a * h + b * k + c)) / sqrt(a * a + b * b);
if (radius == dist)
cout << "Touching the circle" << endl;
else if (radius > dist)
cout << "Intersecting the circle" << endl;
else
cout << "Outside the circle" << endl;
}
int main() {
int radius = 5;
int h = 0, k = 0;
int a = 3, b = 4, c = 25;
isTouchOrIntersect(a, b, c, h, k, radius);
} ผลลัพธ์
Touching the circle