utils.h
来自「游戏开发人工智能技术-AI.Techniques.for.Game.Progra」· C头文件 代码 · 共 132 行
H
132 行
#ifndef UTILS_H
#define UTILS_H
#include <stdlib.h>
#include <math.h>
#include <sstream>
#include <string>
#include <iostream>
#include <vector>
using namespace std;
//----------------------------------------------------------------------------
// some random number functions.
//----------------------------------------------------------------------------
//returns a random integer between x and y
inline int RandInt(int x,int y) {return rand()%(y-x+1)+x;}
//returns a random floating point number between zero and 1
inline double RandFloat() {return (rand())/(RAND_MAX+1.0);}
//returns a random bool
inline bool RandBool()
{
if (RandInt(0,1)) return true;
else return false;
}
//returns a random float in the range -1 < n < 1
inline double RandomClamped() {return RandFloat() - RandFloat();}
//-----------------------------------------------------------------------
//
// some handy little functions
//-----------------------------------------------------------------------
//converts an integer to a string
inline string itos(int arg)
{
ostringstream buffer;
//send the int to the ostringstream
buffer << arg;
//capture the string
return buffer.str();
}
//converts a float to a string
inline string ftos(float arg)
{
ostringstream buffer;
//send the int to the ostringstream
buffer << arg;
//capture the string
return buffer.str();
}
//clamps the first argument between the second two
inline void Clamp(double &arg, float min, float max)
{
if (arg < min)
{
arg = min;
}
if (arg > max)
{
arg = max;
}
}
//rounds a double up or down depending on its value
inline int Rounded(double val)
{
int integral = (int)val;
double mantissa = val - integral;
if (mantissa < 0.5)
{
return integral;
}
else
{
return integral + 1;
}
}
//rounds a double up or down depending on whether its
//mantissa is higher or lower than offset
inline int RoundUnderOffset(double val, double offset)
{
int integral = (int)val;
double mantissa = val - integral;
if (mantissa < offset)
{
return integral;
}
else
{
return integral + 1;
}
}
/////////////////////////////////////////////////////////////////////
//
// Point structure
//
/////////////////////////////////////////////////////////////////////
struct SPoint
{
float x, y;
SPoint(){}
SPoint(float a, float b):x(a),y(b){}
};
#endif
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?