检查线条是否触及或与 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