Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C++

Kiểm tra điểm nằm trong, ngoài hay trên elip trong C++


Cho một elip (tâm (h, k), trục lớn a, trục nhỏ b) và một điểm (x, y). Xác định điểm nằm trong, ngoài hay trên elip.

Phương trình elip:

((x-h)² / a²) + ((y-k)² / b²) ≤ 1

Tính giá trị biểu thức. Nếu < 1 → trong elip, = 1 → trên elip, > 1 → ngoài elip.

Ví dụ

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

Kết quả

Inside Ellipse