Math.h

#ifndef MATH_H_
#define MATH_H_

#include <math.h>

#define PI (3.141592654f)
#define RADIAN_TO_DEGREE(r) ((r) * 180.0f / PI)		//		弧度到角度
#define DEGREE_TO_RADIAN(a) ((a) * PI / 180.0f)		//		角度到弧度

//	4D向量(齐次坐标)
struct Vector4
{
	union
	{
		float M[4] ;

		struct
		{
			float x ;
			float y ;
			float z ;
			float w ;
		} ;
	} ;
} ;

//	3D向量(坐标)
struct Vector3
{
	union
	{
		float M[3] ;

		struct
		{
			float x ;
			float y ;
			float z ;
		} ;
	} ;
}  ;

void VectorPlusVector(Vector3* lhs, Vector3* rhs, Vector3* pResult) ;

void VectorMinusVector(Vector3* endPoint, Vector3* startPoint, Vector3* pResult) ;

void VectorCrossVector(Vector3* lhs, Vector3* rhs, Vector3* pResult) ;

float VectorDotVector(Vector3* lhs, Vector3* rhs) ;

void Vector4ToVector3(Vector4* resourcePoint, Vector3* newPoint) ;

//	平面
struct Plane
{
	float distance ;
	Vector3 normal ;
} ;

//	多边形
struct Poly
{
	Vector4* pVertexList ;
	int vertexIndexArray[3] ;
} ;

//		自包含顶点多边形
struct PolySelfContained
{
	Vector4 vertexList[3] ;
	bool removed ;
} ;

//	3X3矩阵
struct Matrix3X3
{
	union
	{
		float M[3][3] ;

		struct
		{
			float m11, m12, m13 ;
			float m21, m22, m23 ;
			float m31, m32, m33 ;
		} ;
	} ;
} ;

void InitializeMatrix(Matrix3X3* pMatrix,
	float m11, float m12, float m13,
	float m21, float m22, float m23,
	float m31, float m32, float m33) ;

//	4X4矩阵
struct Matrix4X4
{
	union
	{
		float M[4][4] ;

		struct
		{
			float m11, m12, m13, m14 ;
			float m21, m22, m23, m24 ;
			float m31, m32, m33, m34 ;
			float m41, m42, m43, m44 ;
		} ;
	} ;
} ;

void InitializeMatrix(Matrix4X4* pMatrix,
	float m11, float m12, float m13, float m14,
	float m21, float m22, float m23, float m24,
	float m31, float m32, float m33, float m34,
	float m41, float m42, float m43, float m44) ;

void VectorMultMatrix(Vector4* point, Matrix4X4* matrix, Vector4* pResult) ;

void MatrixMultMatrix(Matrix4X4* lhs, Matrix4X4* rhs, Matrix4X4* pResult) ;

float GetDeterminant(const Matrix4X4* pMatrix) ;

class Cube
{
public:
	Vector4 position ;									//		位置
	Vector4 localVertices[8] ;						//		局部顶点坐标数组
	Vector4 transformedVertices[8] ;		//		转换后顶点坐标数组
	Poly polygones[12] ;								//		多边形数组
	int vertexCount ;										//		顶点数量
	int polyCount ;											//		多边形数量

	Cube(float x, float y, float z) ;

	void RotateAroundY(float unit) ;
	void RotateAroundX(float unit) ;
	void RotateAroundZ(float unit) ;
} ;

//		沿任意轴缩放(pAxis是规范化向量)
void ScaleAccordingTo(Cube* pCube, Vector3* pAxis, float k) ;

//		将生成立方体世界坐标
void ModelToWorld(Cube* pCube) ;

//		背面消除
void RemoveBackface(PolySelfContained* polyList, int count, Vector3* pViewPortPosition) ;

//		世界坐标到相机坐标
void WorldToCamera(PolySelfContained* polyList, int count, Matrix4X4* pMatrix) ;

//		相机坐标到裁剪坐标
void CameraToClip(PolySelfContained* polyList, int count, Matrix4X4* pMatrix) ;

//		裁剪坐标到投影坐标
void ClipToPerspective(PolySelfContained* polyList, int count, float nearDistance) ;

//		相机坐标到透视坐标
//		没有对Z值进行检测,在没有裁剪的情况下要当心
void CameraToperspective(PolySelfContained* polyList, int count, float viewPortWidth, float viewPortHeight, float fov) ;

//		透视坐标到视口坐标
void PerspectiveToScreen(PolySelfContained* polyList, int count, float viewPortWidth, float viewPortHeight) ;

#endif




                
<think>嗯,用户想查找关于C/C++中math.h库的函数和用法。首先,我得回忆一下math.h里有哪些常用函数,比如三角函数、指数对数、幂函数、取整之类的。然后,可能需要举几个例子,说明怎么在代码中使用这些函数。另外,用户提到标准库,可能需要提到C++中cmath的区别,比如命名空间的使用。 记得用户给的引用里有提到C++标准库和STL的关系,虽然math.h属于C标准库的一部分,但在C++中通常用cmath,并且放在std命名空间里。这点要指出来,避免混淆。还有,用户可能关心跨平台的问题,比如不同编译器或系统下的兼容性,或者某些函数在不同标准中的变化,比如C99增加的函数。 然后,需要检查示例代码的正确性。比如,sqrt、pow、sin这些函数的使用是否正确,变量类型是否匹配。比如log和log10的区别,exp函数的作用。还有ceil和floor的示例是否正确,fabs和abs的不同,后者可能属于stdlib.h,不过math.h里可能有fabs。 另外,用户可能需要知道如何包含头文件,在C++中应该用#include <cmath>而不是#include <math.h>,并且使用std命名空间。或者,如果是在C语言中,直接使用math.h,但函数可能不需要命名空间。 可能还需要提到一些注意事项,比如链接数学库时可能需要加上-lm,特别是在Linux下编译C程序的时候。此外,某些数学函数可能有精度问题,或者处理异常值的情况,比如NaN或无穷大。 用户的问题还可能涉及如何避免常见的错误,比如参数类型不正确,或者函数返回值的处理。例如,sqrt的参数不能为负数,除非使用复数,但math.h里的sqrt可能不接受负数,导致域错误。 在相关问题上,用户可能还想了解C++中更现代的数学函数,比如C++11引入的特殊函数,或者数值库中的算法。另外,数学函数在性能上的考虑,比如内联函数或者编译器优化。 需要确保回答结构清晰,分点列出常用函数,每个函数简要说明,然后给出代码示例,最后注意事项和相关问题。可能还需要对比C和C++中的不同用法,强调标准的变化和最佳实践。</think>### C/C++ math.h 数学函数库详解 C/C++标准库中的$math.h$(C++中推荐使用$cmath$)提供基础数学运算函数,涵盖三角函数、指数对数、取整运算等[^1]。以下是主要函数分类及示例: #### 一、常用函数分类 1. **基本运算** - `double sqrt(double x)`:计算平方根,例如:$\sqrt{2} \approx 1.414$ - `double pow(double base, double exponent)`:计算幂运算,例如:$3^4 = 81$ ```c #include <math.h> double result = pow(3.0, 4.0); // 81.0 ``` 2. **三角函数**(角度单位为弧度) - `double sin(double x)`:计算正弦值 - `double cos(double x)`:计算余弦值 ```c double angle = M_PI/6; // 30度 double sin_val = sin(angle); // 0.5 ``` 3. **指数与对数** - `double exp(double x)`:计算$e^x$ - `double log(double x)`:计算自然对数$\ln x$ - `double log10(double x)`:计算常用对数$\log_{10} x$ ```c double e_squared = exp(2); // e² ≈ 7.389 ``` 4. **取整与绝对值** - `double ceil(double x)`:向上取整 - `double floor(double x)`:向下取整 - `double fabs(double x)`:浮点绝对值 ```c double a = ceil(2.3); // 3.0 double b = floor(2.7); // 2.0 ``` #### 二、注意事项 1. **头文件差异** - C语言使用`#include <math.h>` - C++推荐使用`#include <cmath>`并位于`std`命名空间[^1] 2. **编译链接** 在Linux系统需添加`-lm`链接数学库: ```bash gcc program.c -o program -lm ``` 3. **特殊常量** 使用预定义常量如`M_PI`(&pi;)、`M_E`(自然对数底数e)需先定义: ```c #define _USE_MATH_DEFINES // 在包含math.h之前定义 ``` #### 三、代码示例(计算圆的面积) ```c #include <stdio.h> #include <math.h> int main() { double radius = 5.0; double area = M_PI * pow(radius, 2); printf("Area: %.2f\n", area); // 输出78.54 return 0; } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值