[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> |
---|
[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 | |
---|
[281] | 74 | static string wstrToUtf8(const wchar_t *str); |
---|
| 75 | #ifdef _WIN32 |
---|
| 76 | static wstring utf8ToUtf16(const char *str); |
---|
| 77 | #endif |
---|
[109] | 78 | }; |
---|
| 79 | |
---|
| 80 | |
---|
| 81 | |
---|
[247] | 82 | 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] | 83 | { |
---|
| 84 | private: |
---|
[247] | 85 | double angle; //in radians, read-only |
---|
[109] | 86 | public: |
---|
[247] | 87 | double angle_deg; //read-only |
---|
| 88 | int angle_deg_int; //read-only |
---|
[109] | 89 | |
---|
[247] | 90 | Angle() { set(0); } |
---|
| 91 | Angle(double k) { set(k); } |
---|
| 92 | Angle(Angle &kt) { set(kt.get()); } |
---|
| 93 | Angle(double dy, double dx) { set(dy, dx); } |
---|
| 94 | 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; } |
---|
| 95 | void set(double dy, double dx) { set(Convert::atan_2(dy, dx)); } |
---|
| 96 | void add(double dk) { set(angle + dk); } |
---|
| 97 | void add(Angle &kt) { set(angle + kt.get()); } |
---|
| 98 | double get() { return angle; } |
---|
| 99 | double sine, cosine; |
---|
[109] | 100 | }; |
---|
| 101 | |
---|
| 102 | |
---|
| 103 | #endif |
---|