source: cpp/common/Convert.h @ 185

Last change on this file since 185 was 185, checked in by Maciej Komosinski, 7 years ago

Avoid "atan2: domain error"

  • Property svn:eol-style set to native
File size: 3.1 KB
Line 
1// This file is a part of the Framsticks GDK.
2// Copyright (C) 2002-2014  Maciej Komosinski and Szymon Ulatowski.  See LICENSE.txt for details.
3// Refer to http://www.framsticks.com/ for further information.
4
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"
12
13typedef XY<double> Pt2D;
14
15
16struct Convert
17{
18public:
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;}
32
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
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);}
41
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
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
52};
53
54
55
56struct Kat //znormalizowany k¹t w radianach [0,2pi) i stopniach [0,360) z obliczonymi sinus i cosinus oraz intem [0,359]
57{
58private:
59        double kat; //w radianach, read-only
60public:
61        double kat_stopnie; //read-only
62        int kat_stopnie_int; //read-only
63
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 sinus,cosinus;
74};
75
76
77#endif
Note: See TracBrowser for help on using the repository browser.