Changeset 247 for cpp/common/Convert.h
- Timestamp:
- 11/07/14 17:51:01 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/common/Convert.h
r197 r247 10 10 #include "nonstd_stl.h" 11 11 #include "2d.h" 12 #include <stdint.h> 13 12 14 13 15 typedef XY<double> Pt2D; … … 17 19 { 18 20 public: 19 static int toInt(string s); 20 static float toFloat(string s); 21 static string toLowerCase(string s); 22 static string toUpperCase(string s); 23 static char toLowerCase(char c); 24 static char toUpperCase(char c); 25 template<class T> static string _toString(const T& value); 26 static string toString(unsigned int v); 27 static string toString(int v); 28 static string toString(short v); 29 static string toString(float v); 30 static string toString(double v);// ze niby w badzie ma nie byc? ale w frams na pewno ma byc bo tu same double 31 static string zeroPad(string s,int l) {while((int)s.length()<l) s=string("0")+s; return s;} 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); 32 35 33 static double toRadians(double kat) {return kat*M_PI/180;}34 static double toDegrees(double kat) {return kat/M_PI*180;}35 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?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? 36 39 37 static double odleglosc_sq(double x1,double y1,double x2,double y2) //odleglosc do kwadratu, wystarczy do porownywania 38 {double dx=x2-x1, dy=y2-y1; return dx*dx+dy*dy;} 39 static double odleglosc_sq(const Pt2D& p1,const Pt2D& p2) //odleglosc do kwadratu 40 {return odleglosc_sq(p1.x,p1.y,p2.x,p2.y);} 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 } 41 48 42 static double odleglosc(double x1,double y1,double x2,double y2) {return sqrt(odleglosc_sq(x1,y1,x2,y2));} 43 static double odleglosc(const Pt2D& p1,const Pt2D& p2) 44 {return sqrt(odleglosc_sq(p1,p2));} 45 46 //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);} 47 //static float odleglosc(float x1,float y1,float x2,float y2) {return sqrt(odleglosc_sq(x1,y1,x2,y2));} 48 //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;} 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 } 49 54 50 static struct tm localtime(const time_t &timep);//jak ::localtime ale zwraca strukture zamiast wskaznika, ref w parametrze dla wygodnego wywolywania 51 static string asctime(const struct tm &tm);//jak ::asctime ale thread safe i bez glupiego \n na koncu, ref w parametrze dla wygodnego wywolywania 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 52 93 }; 53 94 54 95 55 96 56 struct Kat //znormalizowany k¹t w radianach [0,2pi) i stopniach [0,360) z obliczonymi sinus i cosinus oraz intem[0,359]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] 57 98 { 58 99 private: 59 double kat; //w radianach, read-only100 double angle; //in radians, read-only 60 101 public: 61 double kat_stopnie; //read-only62 int kat_stopnie_int; //read-only102 double angle_deg; //read-only 103 int angle_deg_int; //read-only 63 104 64 Kat() {set(0);}65 Kat(double k) {set(k);}66 Kat(Kat &kt) {set(kt.get());}67 Kat(double dy,double dx) {set(dy,dx);}68 void set(double k) { k=fmod(k,M_PI*2); if (k<0) k+=M_PI*2; kat=k; sinus=sin(k); cosinus=cos(k); kat_stopnie=Convert::toDegrees(kat); kat_stopnie_int=roundToInt(kat_stopnie); kat_stopnie_int%=360; }69 void set(double dy, double dx) {set(Convert::atan_2(dy,dx));}70 void add(double dk) { set(kat+dk);}71 void add( Kat &kt) {set(kat+kt.get());}72 double get() { return kat;}73 double sin us,cosinus;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; 74 115 }; 75 116
Note: See TracChangeset
for help on using the changeset viewer.