Finding Quadrant of a Coordinate with respect to a Circle in C++

C++Server Side ProgrammingProgramming

We have one circle (center coordinate and radius), we have to find the quadrant of another given point (x, y) lies with respect to the center of the circle, if this is present in the circle, print quadrant, otherwise print error as the point is present outside.

Suppose the center of the circle is (h, k), the coordinate of the point is (x, y). We know that the equation of the circle is −

(๐‘ฅ−โ„Ž)2+(๐‘ฆ−๐‘˜)2+๐‘Ÿ2=0

Now there are few conditions, based on which we can decide the result.

๐‘–๐‘“ (๐‘ฅ−โ„Ž)2+(๐‘ฆ−๐‘˜)2> ๐‘Ÿ, ๐‘กโ„Ž๐‘’๐‘› ๐‘กโ„Ž๐‘’ ๐‘๐‘œ๐‘–๐‘›๐‘ก ๐‘–๐‘  ๐‘œ๐‘ข๐‘ก๐‘ ๐‘–๐‘‘๐‘’ ๐‘กโ„Ž๐‘’ ๐‘๐‘–๐‘Ÿ๐‘๐‘™๐‘’

๐‘–๐‘“ (๐‘ฅ−โ„Ž)2+(๐‘ฆ−๐‘˜)2= 0, ๐‘กโ„Ž๐‘’๐‘› ๐‘กโ„Ž๐‘’ ๐‘๐‘œ๐‘–๐‘›๐‘ก ๐‘–๐‘  ๐‘œ๐‘› ๐‘กโ„Ž๐‘’ ๐‘๐‘–๐‘Ÿ๐‘๐‘™๐‘’

๐‘–๐‘“ (๐‘ฅ−โ„Ž)2+(๐‘ฆ−๐‘˜)2< ๐‘Ÿ, ๐‘กโ„Ž๐‘’๐‘› ๐‘กโ„Ž๐‘’ ๐‘๐‘œ๐‘–๐‘›๐‘ก ๐‘–๐‘  ๐‘–๐‘›๐‘ ๐‘–๐‘‘๐‘’ ๐‘กโ„Ž๐‘’ ๐‘๐‘–๐‘Ÿ๐‘๐‘™๐‘’

Example

 Live Demo

#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;
}

Output

1 Quadrant
raja
Published on 22-Oct-2019 10:53:43
Advertisements