From ef20385e0da90072cace19decb7ee5c00d045a82 Mon Sep 17 00:00:00 2001
From: Nomango <569629550@qq.com>
Date: Sun, 8 Oct 2017 16:46:44 +0800
Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=BA=86Math=E7=B1=BB?=
=?UTF-8?q?=EF=BC=8C=E5=8F=AF=E4=BB=A5=E8=8E=B7=E5=8F=96=E4=BB=BB=E6=84=8F?=
=?UTF-8?q?=E8=8C=83=E5=9B=B4=E5=86=85=E7=9A=84=E9=9A=8F=E6=9C=BA=E6=95=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Easy2D/Easy2D.vcxproj | 1 +
Easy2D/Easy2D.vcxproj.filters | 3 +++
Easy2D/Tool/Math.cpp | 9 +++++++++
Easy2D/easy2d.h | 28 ++++++++++++++++++++++++++++
4 files changed, 41 insertions(+)
create mode 100644 Easy2D/Tool/Math.cpp
diff --git a/Easy2D/Easy2D.vcxproj b/Easy2D/Easy2D.vcxproj
index 3331284e..804ed744 100644
--- a/Easy2D/Easy2D.vcxproj
+++ b/Easy2D/Easy2D.vcxproj
@@ -388,6 +388,7 @@
+
diff --git a/Easy2D/Easy2D.vcxproj.filters b/Easy2D/Easy2D.vcxproj.filters
index 5bee1125..93d61bbf 100644
--- a/Easy2D/Easy2D.vcxproj.filters
+++ b/Easy2D/Easy2D.vcxproj.filters
@@ -168,6 +168,9 @@
源文件\Object
+
+ 源文件\Tool
+
diff --git a/Easy2D/Tool/Math.cpp b/Easy2D/Tool/Math.cpp
new file mode 100644
index 00000000..2d0a3821
--- /dev/null
+++ b/Easy2D/Tool/Math.cpp
@@ -0,0 +1,9 @@
+#include "..\easy2d.h"
+
+
+std::default_random_engine &Math::getEngine()
+{
+ static std::random_device device;
+ static std::default_random_engine engine(device());
+ return engine;
+}
diff --git a/Easy2D/easy2d.h b/Easy2D/easy2d.h
index 7be7615d..252c0a43 100644
--- a/Easy2D/easy2d.h
+++ b/Easy2D/easy2d.h
@@ -24,6 +24,7 @@
#include
#include
#include
+#include
#if defined(UNICODE) && (_DEBUG)
@@ -1564,6 +1565,27 @@ private:
static void addAction(Action * action);
};
+class Math
+{
+public:
+ // ȡͷΧڵһ
+ template
+ static T randomInt(T min, T max)
+ {
+ std::uniform_int_distribution dist(min, max);
+ return dist(getEngine());
+ }
+ // ȡøͷΧڵһ
+ template
+ static T randomReal(T min, T max)
+ {
+ std::uniform_real_distribution dist(min, max);
+ return dist(getEngine());
+ }
+ // ȡ
+ static std::default_random_engine &getEngine();
+};
+
} // End of easy2d namespace
@@ -1572,4 +1594,10 @@ private:
inline void SafeRelease(easy2d::Object * p) { if (p) p->release(); }
inline void SafeDelete(void * p) { if (p) delete p; }
+template
+inline T random(T min, T max) { return easy2d::Math::randomInt(min, max); }
+inline float random(float min, float max) { return easy2d::Math::randomReal(min, max); }
+inline double random(double min, double max) { return easy2d::Math::randomReal(min, max); }
+inline long double random(long double min, long double max) { return easy2d::Math::randomReal(min, max); }
+
using namespace easy2d;
\ No newline at end of file