c_cpp ICE_Maths lib

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp ICE_Maths lib相关的知识,希望对你有一定的参考价值。

#ifndef DEF_ICE_MATHS_HEADER_ONLY
#define DEF_ICE_MATHS_HEADER_ONLY

// Extracted from
// Vector(2d), Box(rectangle), Angle/Polar movement calculation

#include <math.h>

// DATATYPES
// ----------------------------------------------------------

	#ifndef ICE_BOOL_DEFINED
	#define ICE_BOOL_DEFINED
	typedef char iceBool;
	#define iceTrue 1
	#define iceTRUE 1
	#define icetrue 1
	#define iceFalse 0
	#define iceFALSE 0
	#define icefalse 0
	#endif

	#ifndef ICE_FLOAT_DEFINED
	#define ICE_FLOAT_DEFINED
	typedef double iceFloat;
	#endif

	#ifndef ICE_VECT_DEFINED
	#define ICE_VECT_DEFINED
	typedef struct iceVect
	{
		iceFloat x;
		iceFloat y;

	} iceVect;
	#endif

	#ifndef ICE_BOX_DEFINED
	#define ICE_BOX_DEFINED
	typedef struct iceBox
	{
		iceVect p;
		iceFloat w;
		iceFloat h;
	} iceBox;
	#endif

// MATHS
// ----------------------------------------------------------

	/// Move point from a angle to a r distance in float
	inline void iceMathsMoveFromAngle(iceVect *vect, const iceFloat angle, const iceFloat r) {
		vect->x += r * cos(angle); vect->y += r * sin(angle);
	}

	/// Calculate the angle for two point in radiant
	inline iceFloat iceMathsAngleCalculatRadiant(iceVect vect1, iceVect vect2) {
		iceFloat xdif = vect2.x - vect1.x; iceFloat ydif = vect2.y - vect1.y;
		return atan2(ydif, xdif);
	}

	/// Calculate the angle for two point in degree
	inline iceFloat iceMathsAngleCalculatDegree(iceVect vect1, iceVect vect2) {
		iceFloat xdif = vect2.x - vect1.x; iceFloat ydif = vect2.y - vect1.y;
		return 57.29*atan2(ydif, xdif);
	}

	inline void iceMathsMoveToPosition(iceVect *vect1, iceVect *vect2, iceFloat r) {
		iceFloat xdif = vect2->x - vect1->x; iceFloat ydif = vect2->y - vect1->y;
		iceFloat angle = atan2(ydif, xdif);
		iceFloat distance_r_r = xdif * xdif + ydif * ydif;
		vect1->x += r * cos(angle); vect1->y += r * sin(angle);
		if (distance_r_r < r*r) {
			vect1->x = vect2->x;
			vect1->y = vect2->y;
		}
	}

// BOX
// ----------------------------------------------------------

	// Creation
	inline iceBox iceBoxNew(const iceFloat x, const iceFloat y, const iceFloat w, const iceFloat h) {
		iceBox box = { x,y,w,h };
		return box;
	}

	// Math
	inline iceBox iceBoxScale(iceBox rect, iceFloat scale)
	{
		return iceBoxNew(rect.p.x, rect.p.y, rect.w * scale, rect.h * scale);
	}

	inline iceBool iceBoxCompare(iceBox box1, iceBox box2)
	{
		if (box1.p.x != box2.p.x ||
			box1.p.y != box2.p.y ||
			box1.w != box2.w ||
			box1.h != box2.h)
			return iceFalse;
		return iceTrue;
	}

	inline iceBool iceBoxCompareSize(iceBox box1, iceBox box2)
	{
		if (box1.w != box2.w ||
			box1.h != box2.h)
			return iceFalse;
		return iceTrue;
	}

	inline iceBool iceBoxComparePos(iceBox box1, iceBox box2)
	{
		if (box1.p.x != box2.p.x ||
			box1.p.y != box2.p.y)
			return iceFalse;
		return iceTrue;
	}

	// Edit
	inline void iceBoxSetPos(iceBox *rect, iceVect pos)
	{
		rect->p.x = pos.x; rect->p.y = pos.y;
	}
	inline void iceBoxSetSize(iceBox *rect, iceVect size)
	{
		rect->w = size.x; rect->h = size.y;
	}
	inline void iceBoxShift(iceBox *rect, iceFloat x, iceFloat y)
	{
		rect->p.x += x; rect->p.y += y;
	}

// Vector2D
// ----------------------------------------------------------

	// Create a new vector
	inline iceVect iceVectNew(iceFloat x, iceFloat y) {
		iceVect vect = { x,y };
		return vect;
	}
	// The sum of two vector
	inline iceVect iceVectSum(iceVect vect1, iceVect vect2) {
		return iceVectNew(vect1.x + vect2.x, vect1.y + vect2.y);
	}
	// Sub Vector 2 from vector 1
	inline iceVect iceVectSub(iceVect vect1, iceVect vect2) {
		return iceVectNew(vect1.x - vect2.x, vect1.y - vect2.y);
	}
	// Scale a vector from another value
	inline iceVect iceVectScale(iceVect vect1, iceFloat value) {
		return iceVectNew(vect1.x * value, vect1.y * value);
	}
	// Multiplication of two vectors
	inline iceVect iceVectMulti(iceVect vect1, iceVect vect2) {
		return iceVectNew(vect1.x * vect2.x, vect1.y * vect2.y);
	}
	// Calculate the magnitude of a vector
	inline iceFloat iceVectLenght(iceVect vect) {
		return sqrt((vect.x*vect.x) + (vect.y*vect.y));
	}

#endif

以上是关于c_cpp ICE_Maths lib的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 200.岛屿数量

c_cpp 127.单词阶梯

c_cpp MOFSET

c_cpp MOFSET

c_cpp 31.下一个排列

c_cpp string→char *