我需要在C程序中计算一个角度。下面是JAVA的方法,我需要转换为C.C语言中是否有函数来计算度/弧度?
private static double calculateDirection(double x, double y)
{
return Math.toDegrees(Math.atan2(y, x));
}
是否有类似于C语言toDegrees的功能,所以我不必由我自己写所有的代码? 谢谢
我需要在C程序中计算一个角度。下面是JAVA的方法,我需要转换为C.C语言中是否有函数来计算度/弧度?
private static double calculateDirection(double x, double y)
{
return Math.toDegrees(Math.atan2(y, x));
}
是否有类似于C语言toDegrees的功能,所以我不必由我自己写所有的代码? 谢谢
#include <math.h>
inline double to_degrees(double radians) {
return radians * (180.0/M_PI);
}
我在此期间发现它,但谢谢! – 2013-02-17 11:57:41
¸虽然包含math.h,但是没有找到M_PI?任何想法 – 2013-02-17 12:15:12
某些math.h未获取M_PI定义。添加'#ifndef M_PI #define M_PI 3.1415926535#endif'到'fix'that @AntunTun – 2014-06-04 21:37:13
没有必要使用这样的方法。转换为度数非常简单:
double radians = 2.0;
double degrees = radians * 180.0/M_PI;
如果需要,将其转换为函数。
M_PI
顺便说一下在math.h
中定义*。
*在大多数编译器中。
尽管如此,这仍然是最佳实践。 – 2013-02-17 11:44:44
@OliCharlesworth嗯,我不会为他做所有的工作。 – antonijn 2013-02-17 11:45:49
不,这很好;)我只是在争辩“不需要使用方法”...... – 2013-02-17 11:46:25
如果您喜欢把刚才复制/粘贴一对夫妇宏:
#include <math.h>
#define degToRad(angleInDegrees) ((angleInDegrees) * M_PI/180.0)
#define radToDeg(angleInRadians) ((angleInRadians) * 180.0/M_PI)
如果你想省略#include
,更换这里面从math.h
头复制该行:
#define M_PI 3.14159265358979323846264338327950288
我听说C实现了基于y的符号的有符号划分的atan。所以,虽然看起来微不足道,但并不需要聪明。谢谢 – 2013-02-17 12:07:57