用 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