[286] | 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
| 2 | // Copyright (C) 1999-2015 Maciej Komosinski and Szymon Ulatowski. |
---|
| 3 | // See LICENSE.txt for details. |
---|
[122] | 4 | |
---|
[109] | 5 | #ifndef _CONVERT_H_ |
---|
| 6 | #define _CONVERT_H_ |
---|
| 7 | |
---|
| 8 | #include "nonstd.h" |
---|
| 9 | #include "nonstd_math.h" |
---|
| 10 | #include "nonstd_stl.h" |
---|
| 11 | #include "2d.h" |
---|
[247] | 12 | #include <stdint.h> |
---|
[1005] | 13 | #include <cmath> |
---|
[109] | 14 | |
---|
[247] | 15 | |
---|
[109] | 16 | typedef XY<double> Pt2D; |
---|
| 17 | |
---|
[691] | 18 | #ifdef LINUX |
---|
| 19 | #define UINT64_FORMAT "%llu" //we want to avoid this and ambiguous "long long", but gcc does not seem to support I64u (yet?) |
---|
| 20 | #else |
---|
| 21 | #define UINT64_FORMAT "%I64u" |
---|
| 22 | #endif |
---|
[109] | 23 | |
---|
| 24 | struct Convert |
---|
| 25 | { |
---|
| 26 | public: |
---|
[247] | 27 | static int toInt(string s); |
---|
| 28 | static float toFloat(string s); |
---|
| 29 | static string toLowerCase(string s); |
---|
| 30 | static string toUpperCase(string s); |
---|
| 31 | static char toLowerCase(char c); |
---|
| 32 | static char toUpperCase(char c); |
---|
| 33 | template<class T> static string _toString(const T& value); |
---|
| 34 | static string toString(unsigned int v); |
---|
| 35 | static string toString(int v); |
---|
| 36 | static string toString(short v); |
---|
| 37 | static string toString(float v); |
---|
| 38 | static string toString(double v); |
---|
| 39 | static string zeroPad(string s, int l) { while ((int)s.length() < l) s = string("0") + s; return s; } |
---|
| 40 | static uint32_t hexToInt(const string& col); |
---|
[109] | 41 | |
---|
[1005] | 42 | static double toRadians(double angle) { return angle * M_PI / 180; } |
---|
[247] | 43 | static double toDegrees(double angle) { return angle / M_PI * 180; } |
---|
| 44 | static double atan_2(double y, double x) { if (x == 0 && y == 0) return 0; else return atan2(y, x); } //needed by borland 5/6 only? |
---|
[109] | 45 | |
---|
[247] | 46 | static double odleglosc_sq(double x1, double y1, double x2, double y2) //odleglosc do kwadratu, wystarczy do porownywania |
---|
| 47 | { |
---|
[913] | 48 | double dx = x2 - x1, dy = y2 - y1; |
---|
[1005] | 49 | return dx * dx + dy * dy; |
---|
[247] | 50 | } |
---|
| 51 | static double odleglosc_sq(const Pt2D& p1, const Pt2D& p2) //odleglosc do kwadratu |
---|
| 52 | { |
---|
| 53 | return odleglosc_sq(p1.x, p1.y, p2.x, p2.y); |
---|
| 54 | } |
---|
[109] | 55 | |
---|
[1005] | 56 | static double odleglosc(double x1, double y1, double x2, double y2) { return sqrt(odleglosc_sq(x1, y1, x2, y2)); } |
---|
[247] | 57 | static double odleglosc(const Pt2D& p1, const Pt2D& p2) |
---|
| 58 | { |
---|
| 59 | return sqrt(odleglosc_sq(p1, p2)); |
---|
| 60 | } |
---|
[109] | 61 | |
---|
[247] | 62 | //static float odleglosc(int x1,int y1,int x2,int y2) {float dx=x1-x2; float dy=y1-y2; return sqrt(dx*dx+dy*dy);} |
---|
| 63 | //static float odleglosc(float x1,float y1,float x2,float y2) {return sqrt(odleglosc_sq(x1,y1,x2,y2));} |
---|
| 64 | //static float odleglosc_sq(float x1,float y1,float x2,float y2) {float dx=x1-x2; float dy=y1-y2; return dx*dx+dy*dy;} |
---|
| 65 | |
---|
| 66 | static struct tm localtime(const time_t &timep);//jak ::localtime ale zwraca strukture zamiast wskaznika, ref w parametrze dla wygodnego wywolywania |
---|
| 67 | static string asctime(const struct tm &tm);//jak ::asctime ale thread safe i bez glupiego \n na koncu, ref w parametrze dla wygodnego wywolywania |
---|
| 68 | |
---|
| 69 | static std::wstring strTOwstr(const char *s) |
---|
| 70 | { |
---|
| 71 | string str(s); |
---|
| 72 | return std::wstring(str.begin(), str.end()); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | static string wstrTOstr(const wchar_t *s) |
---|
| 76 | { |
---|
| 77 | wstring str(s); |
---|
| 78 | return string(str.begin(), str.end()); |
---|
| 79 | } |
---|
| 80 | |
---|
[281] | 81 | static string wstrToUtf8(const wchar_t *str); |
---|
| 82 | #ifdef _WIN32 |
---|
| 83 | static wstring utf8ToUtf16(const char *str); |
---|
| 84 | #endif |
---|
[109] | 85 | }; |
---|
| 86 | |
---|
| 87 | |
---|
| 88 | |
---|
[247] | 89 | struct Angle //normalized angle in radians [0,2pi) and degrees [0,360) with pre-computed sine and cosine and degree as integer [0,359] |
---|
[109] | 90 | { |
---|
| 91 | private: |
---|
[247] | 92 | double angle; //in radians, read-only |
---|
[109] | 93 | public: |
---|
[247] | 94 | double angle_deg; //read-only |
---|
| 95 | int angle_deg_int; //read-only |
---|
[109] | 96 | |
---|
[247] | 97 | Angle() { set(0); } |
---|
| 98 | Angle(double k) { set(k); } |
---|
| 99 | Angle(Angle &kt) { set(kt.get()); } |
---|
| 100 | Angle(double dy, double dx) { set(dy, dx); } |
---|
[913] | 101 | Angle(const Pt2D& xy) { set(xy); } |
---|
[247] | 102 | void set(double k) { k = fmod(k, M_PI * 2); if (k < 0) k += M_PI * 2; angle = k; sine = sin(k); cosine = cos(k); angle_deg = Convert::toDegrees(angle); angle_deg_int = roundToInt(angle_deg); angle_deg_int %= 360; } |
---|
| 103 | void set(double dy, double dx) { set(Convert::atan_2(dy, dx)); } |
---|
[913] | 104 | void set(const Pt2D& xy) { set(xy.y, xy.x); } |
---|
[247] | 105 | void add(double dk) { set(angle + dk); } |
---|
| 106 | void add(Angle &kt) { set(angle + kt.get()); } |
---|
| 107 | double get() { return angle; } |
---|
| 108 | double sine, cosine; |
---|
[109] | 109 | }; |
---|
| 110 | |
---|
| 111 | |
---|
| 112 | #endif |
---|