用 C++ 檢查一個點是否在橢圓內部、外部或邊界
假設有一個給定的橢圓(中心座標 (h, k) 和長半軸 a,以及短半軸 b),還有一個給定的點。我們必須找出該點是否在橢圓內部。為解此問題,我們必須對給定點 (x, y) 求解以下方程。
$$\frac{\left(x-h\right)^2}{a^2}+\frac{\left(y-k\right)^2}{b^2}\leq1$$
如果結果小於 1,則該點在橢圓內部;否則在外部。
示例
#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
廣告
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP