C++中關於圓的座標象限查詢


我們有一個圓(圓心座標和半徑),我們需要找到另一個給定點 (x, y) 相對於圓心的象限,如果該點在圓內,則列印象限,否則列印錯誤資訊,因為該點在圓外。

假設圓心為 (h, k),點的座標為 (x, y)。我們知道圓的方程為:

(𝑥−ℎ)2+(𝑦−𝑘)2+𝑟2=0

現在有一些條件,我們可以根據這些條件來決定結果。

如果 (𝑥−ℎ)2+(𝑦−𝑘)2> 𝑟,則該點在圓外

如果 (𝑥−ℎ)2+(𝑦−𝑘)2= 0,則該點在圓上

如果 (𝑥−ℎ)2+(𝑦−𝑘)2< 𝑟,則該點在圓內

示例

線上演示

#include<iostream>
#include<cmath>
using namespace std;
int getQuadrant(int h, int k, int rad, int x, int y) {
   if (x == h && y == k)
      return 0;
   int val = pow((x - h), 2) + pow((y - k), 2);
   if (val > pow(rad, 2))
      return -1;
   if (x > h && y >= k)
      return 1;
   if (x <= h && y > k)
      return 2;
   if (x < h && y <= k)
      return 3;
   if (x >= h && y < k)
      return 4;
}
int main() {
   int h = 0, k = 3;
   int rad = 2;
   int x = 1, y = 4;
   int ans = getQuadrant(h, k, rad, x, y);
   if (ans == -1)
      cout << "Point is Outside of the circle" << endl;
   else if (ans == 0)
      cout << "Present at the center" << endl;
   else
      cout << ans << " Quadrant" << endl;
}

輸出

1 Quadrant

更新於:2019年10月22日

183 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告