在 C++ 中查找某个点是否位于圆内
c++server side programmingprogramming更新于 2025/3/15 9:22:17
假设给定一个圆(圆心坐标和半径),还给定另一个点。我们必须确定该点是否位于圆内。要解决这个问题,我们必须找到给定点与圆心的距离。如果该距离小于或等于半径,则该点位于圆内,否则不位于圆内。
示例
#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