Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

การหาจตุภาคของพิกัดเทียบกับวงกลมใน 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