source: cpp/common/Convert.h @ 111

Last change on this file since 111 was 109, checked in by sz, 11 years ago

source reorganization (see README)
new feature added: part/joint shapes (see frams/_demos/part_shapes.cpp)

  • Property svn:eol-style set to native
File size: 2.9 KB
Line 
1#ifndef _CONVERT_H_
2#define _CONVERT_H_
3
4#include "nonstd.h"
5#include "nonstd_math.h"
6#include "nonstd_stl.h"
7#include "2d.h"
8
9typedef XY<double> Pt2D;
10
11
12struct Convert
13{
14public:
15  static int toInt(string s);
16  static float toFloat(string s);
17  static string toLowerCase(string s);
18  static string toUpperCase(string s);
19  static char toLowerCase(char c);
20  static char toUpperCase(char c);
21  template<class T> static string _toString(const T& value);
22  static string toString(unsigned int v);
23  static string toString(int v);
24  static string toString(short v);
25  static string toString(float v);
26  static string toString(double v);// ze niby w badzie ma nie byc? ale w frams na pewno ma byc bo tu same double
27  static string zeroPad(string s,int l) {while((int)s.length()<l) s=string("0")+s; return s;}
28
29  static double toRadians(double kat) {return kat*M_PI/180;}
30  static double toDegrees(double kat) {return kat/M_PI*180;}
31  static double atan_2(double y,double x) {if (x==0 && y==0) return 0; else return atan2(y,x);}
32
33        static double odleglosc_sq(double x1,double y1,double x2,double y2) //odleglosc do kwadratu, wystarczy do porownywania
34        {double dx=x2-x1, dy=y2-y1; return dx*dx+dy*dy;}
35        static double odleglosc_sq(const Pt2D& p1,const Pt2D& p2) //odleglosc do kwadratu
36        {return odleglosc_sq(p1.x,p1.y,p2.x,p2.y);}
37
38        static double odleglosc(double x1,double y1,double x2,double y2)        {return sqrt(odleglosc_sq(x1,y1,x2,y2));}
39        static double odleglosc(const Pt2D& p1,const Pt2D& p2)
40        {return sqrt(odleglosc_sq(p1,p2));}
41       
42  //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);}
43  //static float odleglosc(float x1,float y1,float x2,float y2) {return sqrt(odleglosc_sq(x1,y1,x2,y2));}
44  //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;}
45
46  static struct tm localtime(const time_t &timep);//jak ::localtime ale zwraca strukture zamiast wskaznika, ref w parametrze dla wygodnego wywolywania
47  static string asctime(const struct tm &tm);//jak ::asctime ale thread safe i bez glupiego \n na koncu, ref w parametrze dla wygodnego wywolywania
48};
49
50
51
52struct Kat //znormalizowany k¹t w radianach [0,2pi) i stopniach [0,360) z obliczonymi sinus i cosinus oraz intem [0,359]
53{
54private:
55        double kat; //w radianach, read-only
56public:
57        double kat_stopnie; //read-only
58        int kat_stopnie_int; //read-only
59
60        Kat() {set(0);}
61        Kat(double k) {set(k);}
62        Kat(Kat &kt) {set(kt.get());}
63        Kat(double dy,double dx) {set(dy,dx);}
64        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; }
65        void set(double dy,double dx) {set(Convert::atan_2(dy,dx));}
66        void add(double dk) {set(kat+dk);}
67        void add(Kat &kt) {set(kat+kt.get());}
68        double get() {return kat;}
69        double sinus,cosinus;
70};
71
72
73
74#endif
Note: See TracBrowser for help on using the repository browser.