[122] | 1 | // This file is a part of the Framsticks GDK. |
---|
[197] | 2 | // Copyright (C) 1999-2014 Maciej Komosinski and Szymon Ulatowski. See LICENSE.txt for details. |
---|
[122] | 3 | // Refer to http://www.framsticks.com/ for further information. |
---|
| 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> |
---|
[109] | 13 | |
---|
[247] | 14 | |
---|
[109] | 15 | typedef XY<double> Pt2D; |
---|
| 16 | |
---|
| 17 | |
---|
| 18 | struct Convert |
---|
| 19 | { |
---|
| 20 | public: |
---|
[247] | 21 | static int toInt(string s); |
---|
| 22 | static float toFloat(string s); |
---|
| 23 | static string toLowerCase(string s); |
---|
| 24 | static string toUpperCase(string s); |
---|
| 25 | static char toLowerCase(char c); |
---|
| 26 | static char toUpperCase(char c); |
---|
| 27 | template<class T> static string _toString(const T& value); |
---|
| 28 | static string toString(unsigned int v); |
---|
| 29 | static string toString(int v); |
---|
| 30 | static string toString(short v); |
---|
| 31 | static string toString(float v); |
---|
| 32 | static string toString(double v); |
---|
| 33 | static string zeroPad(string s, int l) { while ((int)s.length() < l) s = string("0") + s; return s; } |
---|
| 34 | static uint32_t hexToInt(const string& col); |
---|
[109] | 35 | |
---|
[247] | 36 | static double toRadians(double angle) { return angle*M_PI / 180; } |
---|
| 37 | static double toDegrees(double angle) { return angle / M_PI * 180; } |
---|
| 38 | 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] | 39 | |
---|
[247] | 40 | static double odleglosc_sq(double x1, double y1, double x2, double y2) //odleglosc do kwadratu, wystarczy do porownywania |
---|
| 41 | { |
---|
| 42 | double dx = x2 - x1, dy = y2 - y1; return dx*dx + dy*dy; |
---|
| 43 | } |
---|
| 44 | static double odleglosc_sq(const Pt2D& p1, const Pt2D& p2) //odleglosc do kwadratu |
---|
| 45 | { |
---|
| 46 | return odleglosc_sq(p1.x, p1.y, p2.x, p2.y); |
---|
| 47 | } |
---|
[109] | 48 | |
---|
[247] | 49 | static double odleglosc(double x1, double y1, double x2, double y2) { return sqrt(odleglosc_sq(x1, y1, x2, y2)); } |
---|
| 50 | static double odleglosc(const Pt2D& p1, const Pt2D& p2) |
---|
| 51 | { |
---|
| 52 | return sqrt(odleglosc_sq(p1, p2)); |
---|
| 53 | } |
---|
[109] | 54 | |
---|
[247] | 55 | //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);} |
---|
| 56 | //static float odleglosc(float x1,float y1,float x2,float y2) {return sqrt(odleglosc_sq(x1,y1,x2,y2));} |
---|
| 57 | //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;} |
---|
| 58 | |
---|
| 59 | static struct tm localtime(const time_t &timep);//jak ::localtime ale zwraca strukture zamiast wskaznika, ref w parametrze dla wygodnego wywolywania |
---|
| 60 | static string asctime(const struct tm &tm);//jak ::asctime ale thread safe i bez glupiego \n na koncu, ref w parametrze dla wygodnego wywolywania |
---|
| 61 | |
---|
| 62 | static std::wstring strTOwstr(const char *s) |
---|
| 63 | { |
---|
| 64 | string str(s); |
---|
| 65 | return std::wstring(str.begin(), str.end()); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | static string wstrTOstr(const wchar_t *s) |
---|
| 69 | { |
---|
| 70 | wstring str(s); |
---|
| 71 | return string(str.begin(), str.end()); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | static string wstrTOutf8(const wchar_t *s) |
---|
| 75 | { |
---|
| 76 | if (s == NULL) return ""; |
---|
| 77 | string res; |
---|
| 78 | wchar_t *wcp = (wchar_t*)s; |
---|
| 79 | while (*wcp != 0) |
---|
| 80 | { |
---|
| 81 | int c = *wcp; |
---|
| 82 | if (c < 0x80) res += c; |
---|
| 83 | else if (c < 0x800) { res += 192 + c / 64; res += 128 + c % 64; } |
---|
| 84 | else if (c - 0xd800u < 0x800) res += "<ERR-CHAR>"; |
---|
| 85 | else if (c < 0x10000) { res += 224 + c / 4096; res += 128 + c / 64 % 64; res += 128 + c % 64; } |
---|
| 86 | else if (c < 0x110000) { res += 240 + c / 262144; res += 128 + c / 4096 % 64; res += 128 + c / 64 % 64; res += 128 + c % 64; } |
---|
| 87 | else res += "<ERR-CHAR>"; |
---|
| 88 | wcp++; |
---|
| 89 | } |
---|
| 90 | return res; |
---|
| 91 | } |
---|
| 92 | //#endif |
---|
[109] | 93 | }; |
---|
| 94 | |
---|
| 95 | |
---|
| 96 | |
---|
[247] | 97 | 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] | 98 | { |
---|
| 99 | private: |
---|
[247] | 100 | double angle; //in radians, read-only |
---|
[109] | 101 | public: |
---|
[247] | 102 | double angle_deg; //read-only |
---|
| 103 | int angle_deg_int; //read-only |
---|
[109] | 104 | |
---|
[247] | 105 | Angle() { set(0); } |
---|
| 106 | Angle(double k) { set(k); } |
---|
| 107 | Angle(Angle &kt) { set(kt.get()); } |
---|
| 108 | Angle(double dy, double dx) { set(dy, dx); } |
---|
| 109 | 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; } |
---|
| 110 | void set(double dy, double dx) { set(Convert::atan_2(dy, dx)); } |
---|
| 111 | void add(double dk) { set(angle + dk); } |
---|
| 112 | void add(Angle &kt) { set(angle + kt.get()); } |
---|
| 113 | double get() { return angle; } |
---|
| 114 | double sine, cosine; |
---|
[109] | 115 | }; |
---|
| 116 | |
---|
| 117 | |
---|
| 118 | #endif |
---|