mathutil.cpp

来自「3D赛车游戏源代码-用Visual Studio 2005」· C++ 代码 · 共 56 行

CPP
56
字号
/////////////////////////////////////////////////////////////////////////////
//
// 3D Math Primer for Games and Graphics Development
//
// MathUtil.cpp - Miscellaneous math utilities
//
// Visit gamemath.com for the latest version of this file.
//
/////////////////////////////////////////////////////////////////////////////

#include "StdAfx.h"
#include "MathUtil.h"
#include "Vector3.h"

const Vector3 kZeroVector(0.0f, 0.0f, 0.0f);

//-------------------------------------------------------------------------------
// 把角度控制在 -pi 和 pi 之间(通过加减2pi)
//-------------------------------------------------------------------------------
float wrapPi(float theta) 
{
	theta += kPi;
	theta -= floor(theta * k1Over2Pi) * k2Pi;
	theta -= kPi;
	return theta;
}

//---------------------------------------------------------------------------
// safeAcos
//
// Same as acos(x), but if x is out of range, it is "clamped" to the nearest
// valid value.  The value returned is in range 0...pi, the same as the
// standard C acos() function

//-------------------------------------------------------------------------------
// 安全的acos函数,处理了x超出范围的情况
//-------------------------------------------------------------------------------
float safeAcos(float x) 
{
	// Check limit conditions

	if (x <= -1.0f) 
	{
		return kPi;
	}

	if (x >= 1.0f) 
	{
		return 0.0f;
	}

	// Value is in the domain - use standard C function

	return acos(x);
}

⌨️ 快捷键说明

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