⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mymath.h

📁 一个3D的保龄球的源代码
💻 H
字号:
#include "aee.h"
#include "../System/IMath.h"


#ifndef _MATH3D_H
#define _MATH3D_H

//#define USE_FLOAT
//#define DEBUG_OVERFLOW
#define CRIT_VALUE 32768 //2^15
#define DANGER_VALUE 524288//2^19
#define DANGER_VALUET 4194304 //2^22

#ifdef USE_FLOAT
	#include <math.h>
	typedef float PhType;
	typedef double TriValue;
	typedef float AngDegree;
	#define PHT_EXT 0
	#define TRI_EXT 0
	#define MULT_BTW_TRI_PHT 0
	#define DEGREE_EXT 1
	TriValue DegreesToRadians(PhType deg);
	TriValue RadiansToDegrees(PhType rad);
#else
	typedef long PhType;
	typedef long TriValue;
	typedef long AngDegree;
	#define PHT_EXT 7  //2^7=128
	#define TRI_EXT 14 //2^14=16384
	#define MULT_BTW_TRI_PHT  (TRI_EXT-PHT_EXT)
	#define DEGREE_EXT 10
//	#define PhTypeRef long
	
	//TriValue sin(AngDegree degree);
	TriValue sin360(AngDegree degree);		//the degree is multiple 10 and the return type is sin(x)<<TRI_EXT 
	//TriValue cos(AngDegree degree);
	TriValue cos360(AngDegree degree);		//the degree is multiple 10 and the return type is sin(x)<<TRI_EXT 
	TriValue tan(AngDegree degree);
	TriValue tan360(AngDegree degree);
	AngDegree asin(TriValue value);			
	AngDegree acos(TriValue value);
	AngDegree atan(TriValue value);
	AngDegree atan2(PhType y, PhType x);
	PhType mysqrt(PhType value);			// the sqrt function that deal with phType value
	inline	PhType abs(PhType value);
#endif


class Vector
{
public:
	PhType x;
	PhType y;
	PhType z;

	Vector(void);
	Vector(PhType xi, PhType yi, PhType zi);

	PhType Magnitude(void);
	void  Normalize(void);
	void  Reverse(void);

	Vector& operator+=(Vector u);	// vector addition
	Vector& operator-=(Vector u);	// vector subtraction
	Vector& operator*=(PhType s);	// scalar multiply
	Vector& operator/=(PhType s);	// scalar divide

	Vector operator-(void);

};

Vector operator+(Vector u, Vector v);
Vector operator-(Vector u, Vector v);
Vector operator^(Vector u, Vector v);
PhType operator*(Vector u, Vector v);
Vector operator*(PhType s, Vector u);
Vector operator*(Vector u, PhType s);
Vector operator/(Vector u, PhType s);
//PhType TripleScalarProduct(Vector u, Vector v, Vector w);
bool isParallel(Vector l1, Vector l2);// return ture if 2 given vectors are parallel else return false;

/* 
this function test if 2 line is parallel, if not it will return the perpendicular of the 2 line.
p1: the point on the first line.
l1: the direction of the first line.
p2: the point on the second line.
l2: the direction of the second line.
p:  the 2 points of the perpendicular that are on the 2 line if the 2 lines are nonparallel. null if they are parallel.
return: 0 if the 2 lines are parallel, 1 if they are  nonparallel.
*/
int calcPlumbAmong2L(Vector p1, Vector l1, Vector p2, Vector l2, Vector *p);


class Matrix3x3 {
public:
	// elements eij: i -> row, j -> column
	TriValue	e11, e12, e13, e21, e22, e23, e31, e32, e33;	

	Matrix3x3(void);
	Matrix3x3(	TriValue r1c1, TriValue r1c2, TriValue r1c3, 
				TriValue r2c1, TriValue r2c2, TriValue r2c3, 
				TriValue r3c1, TriValue r3c2, TriValue r3c3 );

	PhType	det(void);// the 
	Matrix3x3	Transpose(void);//return the Transpose of the matrix
	Matrix3x3	Inverse(void);	//return the inverse matrix
	//Matrix3x3	InverseR(void);
	//PhType	havezero(void){return ((e11==0)||(e12==0)||(e13==0)||(e21==0)||(e22==0)||(e23==0)||(e31==0)||(e32==0)||(e33==0));}

	Matrix3x3& operator+=(Matrix3x3 m);
	Matrix3x3& operator-=(Matrix3x3 m);
	Matrix3x3& operator*=(PhType s);
	Matrix3x3& operator/=(PhType s);
};

Matrix3x3 operator+(Matrix3x3 m1, Matrix3x3 m2);
Matrix3x3 operator-(Matrix3x3 m1, Matrix3x3 m2);
Matrix3x3 operator/(Matrix3x3 m, PhType s);
Matrix3x3 operator*(Matrix3x3 m1, Matrix3x3 m2);
Matrix3x3 operator*(Matrix3x3 m, PhType s);
Matrix3x3 operator*(PhType s, Matrix3x3 m);
Vector operator*(Matrix3x3 m, Vector u);
Vector operator*(Vector u, Matrix3x3 m);

//Vector MulRMV(Matrix3x3 m, Vector v);
//Vector MulVRM(Vector v, Matrix3x3 m );


class Quaternion {
public:
	TriValue	n;	// number (scalar) part
	TriValue	x;	// vector part: v.x, v.y, v.z
	TriValue	y;
	TriValue	z;

	Quaternion(void);
	Quaternion(TriValue e0, TriValue e1, TriValue e2, TriValue e3);

	PhType	Magnitude(void);
	void	Normalize(void);
	Vector	GetVector(void);// return the vector(x,y,z);
//	TriValue	GetScalar(void);
	Quaternion	operator+=(Quaternion q);
	Quaternion	operator-=(Quaternion q);
	Quaternion operator*=(PhType s);
	Quaternion operator/=(PhType s);
	Quaternion	operator~(void) const { return Quaternion(n, -x, -y, -z);}// the inverse operator.
};

Quaternion operator+(Quaternion q1, Quaternion q2);
Quaternion operator-(Quaternion q1, Quaternion q2);
Quaternion operator*(Quaternion q1, Quaternion q2);
Quaternion operator*(Quaternion q, PhType s);
Quaternion operator*(PhType s, Quaternion q);
Quaternion operator*(Quaternion q, Vector v);
Quaternion operator*(Vector v, Quaternion q);
Quaternion operator/(Quaternion q, PhType s);
//PhType QGetAngle(Quaternion q);
//Vector QGetAxis(Quaternion q);
Quaternion QRotate(Quaternion q1, Quaternion q2);//use the quaternion q2 rotate the quaternion q1.
Vector	QVRotate(Quaternion q, Vector v);// rotate the vector v by quaternion q.
Quaternion	MakeQFromEulerAngles(AngDegree x, AngDegree y, AngDegree z);//make a quaternion by angle x,y,z.
Vector	MakeEulerAnglesFromQ(Quaternion q);// get the Euler angle x,y,z in vector mode from quaternion q. 
Matrix3x3 MakeMatrixFromQuaternion(Quaternion q);// get the rotation matrix form quaternion q.




#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -