สมมติว่ามีวงรีหนึ่งวง (พิกัดศูนย์กลาง (h, k) และแกนกึ่งแกนหลัก a และแกนกึ่งรอง b) อีกจุดหนึ่งจะได้รับด้วย เราต้องหาว่าจุดนั้นอยู่ในวงรีหรือไม่ ในการแก้ เราต้องแก้สมการต่อไปนี้สำหรับจุดที่กำหนด (x, y)
$$\frac{\left(x-h\right)^2}{a^2}+\frac{\left(y-k\right)^2}{b^2}\leq1$$
หากผลลัพธ์มีค่าน้อยกว่าหนึ่ง แสดงว่าจุดนั้นอยู่ในวงรี มิฉะนั้นไม่ใช่
ตัวอย่าง
#include <iostream>
#include <cmath>
using namespace std;
bool isInsideEllipse(int h, int k, int x, int y, int a, int b) {
int res = (pow((x - h), 2) / pow(a, 2)) + (pow((y - k), 2) / pow(b, 2));
return res;
}
int main() {
int x = 2, y = 1, h = 0, k = 0, a = 4, b = 5;
if(isInsideEllipse(h, k, x, y, a, b) > 1){
cout <<"Outside Ellipse";
}
else if(isInsideEllipse(h, k, x, y, a, b) == 1){
cout <<"On the Ellipse";
} else{
cout <<"Inside Ellipse";
}
} ผลลัพธ์
Inside Ellipse