สมมติมี Ellipse (จุดศูนย์กลาง (h, k) Semi-major axis a และ Semi-minor axis b) และมีจุดอีกจุดหนึ่ง ต้องการทราบว่าจุดอยู่ใน Ellipse หรือไม่ แก้สมการสำหรับจุด (x, y):
$$\frac{(x-h)^2}{a^2}+\frac{(y-k)^2}{b^2}\leq1$$
ถ้าผลลัพธ์ < 1 จุดอยู่ใน Ellipse มิฉะนั้นอยู่นอก
ตัวอย่างโค้ด C++
#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