#pragma once
#include "cocos2d.h"
USING_NS_CC;
#define CC_RADIANS_TO_DEGREES(__ANGLE__) ((__ANGLE__) * 57.29577951f) // PI * 180
class Math
{
public:
// math
static const float PI;
static float sin_d(float a);
static float cos_d(float a);
static float getRotation(const cocos2d::Point& from, const cocos2d::Point& to);//by pchj
static float getAngle(Point ptFir, Point ptSec);
// random
static void randomize();
static bool random();
static int random(int max);
static int random(int min, int max);
static float random(float min, float max);
};
#include "Math.h"
USING_NS_CC
const float Math::PI = 3.14159265f;
float Math::sin_d(float a)
{
return sin(a * PI / 180);
}
float Math::cos_d(float a)
{
return cos(a * PI / 180);
}
float Math::getRotation(const Point& from, const Point& to)
{
Point d = to - from;
float angle = atan2(d.x, d.y);
return angle * 180 / PI;
}
float Math::getAngle(Point ptFir, Point ptSec)
{
float xdif = ptSec.x - ptFir.x;
float ydif = ptSec.y - ptFir.y;
float angle;
if (xdif == 0 || ydif == 0)
{
angle = 0.f;
if (xdif == 0 && ydif > 0)
angle = 90.f;
else if (xdif == 0 && ydif < 0)
angle = 270.f;
else if (xdif < 0 && ydif == 0)
angle = 180.f;
}
else
{
angle = (float)CC_RADIANS_TO_DEGREES(atan(fabs((float)ydif / xdif)));
if (xdif < 0 && ydif > 0)
angle = 180.f - angle;
else if (xdif < 0 && ydif < 0)
angle += 180.f;
else if (xdif >0 && ydif < 0)
angle = 360.f - angle;
}
return -angle;
}
void Math::randomize()
{
srand(time(nullptr));
}
bool Math::random()
{
return rand() % 2 == 0;
}
int Math::random(int max)
{
return random(0, max);
}
int Math::random(int min, int max)
{
if (min == max)
return min;
return min + rand() % (max - min) ;
}
float Math::random(float min, float max)
{
return min + (max - min) * (rand() % 4096) / 4095;
}
#include "cocos2d.h"
USING_NS_CC;
#define CC_RADIANS_TO_DEGREES(__ANGLE__) ((__ANGLE__) * 57.29577951f) // PI * 180
class Math
{
public:
// math
static const float PI;
static float sin_d(float a);
static float cos_d(float a);
static float getRotation(const cocos2d::Point& from, const cocos2d::Point& to);//by pchj
static float getAngle(Point ptFir, Point ptSec);
// random
static void randomize();
static bool random();
static int random(int max);
static int random(int min, int max);
static float random(float min, float max);
};
#include "Math.h"
USING_NS_CC
const float Math::PI = 3.14159265f;
float Math::sin_d(float a)
{
return sin(a * PI / 180);
}
float Math::cos_d(float a)
{
return cos(a * PI / 180);
}
float Math::getRotation(const Point& from, const Point& to)
{
Point d = to - from;
float angle = atan2(d.x, d.y);
return angle * 180 / PI;
}
float Math::getAngle(Point ptFir, Point ptSec)
{
float xdif = ptSec.x - ptFir.x;
float ydif = ptSec.y - ptFir.y;
float angle;
if (xdif == 0 || ydif == 0)
{
angle = 0.f;
if (xdif == 0 && ydif > 0)
angle = 90.f;
else if (xdif == 0 && ydif < 0)
angle = 270.f;
else if (xdif < 0 && ydif == 0)
angle = 180.f;
}
else
{
angle = (float)CC_RADIANS_TO_DEGREES(atan(fabs((float)ydif / xdif)));
if (xdif < 0 && ydif > 0)
angle = 180.f - angle;
else if (xdif < 0 && ydif < 0)
angle += 180.f;
else if (xdif >0 && ydif < 0)
angle = 360.f - angle;
}
return -angle;
}
void Math::randomize()
{
srand(time(nullptr));
}
bool Math::random()
{
return rand() % 2 == 0;
}
int Math::random(int max)
{
return random(0, max);
}
int Math::random(int min, int max)
{
if (min == max)
return min;
return min + rand() % (max - min) ;
}
float Math::random(float min, float max)
{
return min + (max - min) * (rand() % 4096) / 4095;
}