檢查線條是否觸及或與 C++ 中的圓相交
假設我們有一個圓和其他直線。我們的任務是找出線條是否觸及圓或與之相交,否則,它將從外面穿過。因此,有三種不同的情況,如下所示-

這裡,我們將按照以下步驟來解決它。它們如下所示-
- 找出圓心和給定直線之間的垂直線 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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP