Changeset 913 for cpp/common


Ignore:
Timestamp:
04/20/20 23:27:35 (5 years ago)
Author:
Maciej Komosinski
Message:

Added a few utility/helper functions

Location:
cpp/common
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • cpp/common/Convert.h

    r691 r913  
    4545        static double odleglosc_sq(double x1, double y1, double x2, double y2) //odleglosc do kwadratu, wystarczy do porownywania
    4646        {
    47                 double dx = x2 - x1, dy = y2 - y1; return dx*dx + dy*dy;
     47                double dx = x2 - x1, dy = y2 - y1;
     48                return dx*dx + dy*dy;
    4849        }
    4950        static double odleglosc_sq(const Pt2D& p1, const Pt2D& p2) //odleglosc do kwadratu
     
    9798        Angle(Angle &kt) { set(kt.get()); }
    9899        Angle(double dy, double dx) { set(dy, dx); }
     100        Angle(const Pt2D& xy) { set(xy); }
    99101        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; }
    100102        void set(double dy, double dx) { set(Convert::atan_2(dy, dx)); }
     103        void set(const Pt2D& xy) { set(xy.y, xy.x); }
    101104        void add(double dk) { set(angle + dk); }
    102105        void add(Angle &kt) { set(angle + kt.get()); }
  • cpp/common/nonstd_math.h

    r899 r913  
    5858#endif
    5959
    60 
    6160//handling floating point exceptions
    6261void fpExceptInit(); //call once, before ...Enable/Disable
     
    6766template <typename Value, typename Linear> Value universal_lerp(Value a,Value b,Linear t) {return a*(1-t)+b*t;}
    6867
     68template <typename T> T linearTransform(T value, T min_in, T max_in, T min_out, T max_out)
     69{
     70        return min_out + (value-min_in)*(max_out-min_out)/(max_in-min_in);
     71}
     72
    6973#endif
  • cpp/common/util-string.cpp

    r897 r913  
    7777}
    7878
     79bool str_starts_with(const char *str, const char *prefix)
     80{
     81        return strncmp(str,prefix,strlen(prefix))==0;
     82}
     83
    7984char* strmove(char *a, char *b) //strcpy that works well for overlapping strings ("Source and destination overlap")
    8085{
  • cpp/common/util-string.h

    r840 r913  
    1010
    1111char* strmove(char *a, char *b); //strcpy that works well for overlapping strings ("Source and destination overlap")
     12bool str_starts_with(const char *str, const char *prefix);
     13inline bool starts_with(string& str, const char *prefix) { return str_starts_with(str.c_str(),prefix); } //std::string.starts_with(...) since c++20
    1214
    1315string ssprintf(const char* format, ...);
Note: See TracChangeset for help on using the changeset viewer.