在 C++ 中查找一个点是否在圆内
假设有一个圆(中心坐标和半径),也给出另一个点。我们必须找出该点是否在圆内。要解决此问题,我们必须找到给定点到圆心的距离。如果该距离小于或等于半径,则该点位于圆内,否则则不在圆内。
示例
#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
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP