在 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

更新于:2019 年 10 月 21 日

3K+ 浏览量

开启您的 职业生涯

完成课程以获得认证

开始
广告
© . All rights reserved.