只考虑焦点在坐标轴,质点在圆心的情况
#include <math.h>
#include <iostream>
#include <utility>
inline float degrees_to_radians(float deg) {
return deg / 180.0 * M_PI;
}
inline float radians_to_degrees(float rad) {
return rad / M_PI * 180.0;
}
// 计算离心角
inline float eccentric_angle(float a, float b, float rad) {
return atan2(a*sin(rad), b*cos(rad));
}
std::pair<float, float> getEllipseXY(float a, float b, float degree) {
auto rad = degrees_to_radians(degree);
auto ecc_angle = eccentric_angle(a, b, rad);
auto x = a * cos(ecc_angle);
auto y = b * sin(ecc_angle);
return {x, y};
}
int main() {
std::pair<float, float> xy;
for (int deg = 0; deg <= 480; deg += 90) {
xy = getEllipseXY(10, 5, deg);
std::cout << "degree = " << deg << ": x, y = "<< xy.first << " " << xy.second << std::endl;
}
return 0;
}