具有给定长度和宽度的给定角度的椭圆的半径

Radius of an Ellipse at a given angle with given length and width

我正在用 C 编写一个函数,它 returns 给定角度和给定长度和宽度的椭圆的半径;基本上用 C:

写这个计算

不幸的是,该平台不支持 math.h,但是我可以使用内置的 sincos 函数。

如何用 C 编写此计算并将其存储在 int 中?

我试过:

int theta = 90;
int a = 164;
int b = 144;

float aa = (((a^2) * ((sin_lookup(DEG_TO_TRIGANGLE(theta)))^2)) +
            ((b^2) * ((cos_lookup(DEG_TO_TRIGANGLE(theta)))^2))) /
           (TRIG_MAX_ANGLE^2);

float result = (a * b) / (my_sqrt(aa));
int value = (int)result;

足够简单

int getRadius(double a, double b, double theta)
{
     double s = sin(theta),
            c = cos(theta);
     return (a * b) / sqrt((a*a)*(s*s)+(b*b)*(c*c))
}

虽然我不确定你为什么要 return int。你会失去很多精度。


^运算符不是求幂的方法。它实际上是按位异或。这是新 (C) 程序员常犯的错误。 math.h有一个计算幂的函数pow(),但是你说你不能用math.h。这些值仅提高到二次方,因此手动相乘非常容易。