c语言 判断点与圆的位置关系
时间: 2023-05-12 08:07:38 浏览: 71
可以使用勾股定理计算点与圆心的距离,如果距离小于等于圆的半径,则点在圆内;如果距离大于圆的半径,则点在圆外;如果距离等于圆的半径,则点在圆上。以下是示例代码:
```c
#include <stdio.h>
#include <math.h>
struct Point {
double x;
double y;
};
struct Circle {
struct Point center;
double radius;
};
double distance(struct Point p1, struct Point p2) {
double dx = p1.x - p2.x;
double dy = p1.y - p2.y;
return sqrt(dx * dx + dy * dy);
}
int position(struct Point p, struct Circle c) {
double d = distance(p, c.center);
if (d < c.radius) {
return -1; // inside
} else if (d > c.radius) {
return 1; // outside
} else {
return 0; // on the circle
}
}
int main() {
struct Point p = {1.0, 2.0};
struct Circle c = {{0.0, 0.0}, 1.0};
int pos = position(p, c);
printf("Position: %d\n", pos);
return 0;
}
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)