sdk: const correctness
[sdk] / ecere / src / sys / units.ec
1 namespace sys;
2
3 import "instance"
4
5 public struct Point { int x, y; };
6 public struct Pointf { float x, y; };
7
8 public class MinMaxValue : int
9 {
10    const char * OnGetString(char * string, void * fieldData, bool * needClass)
11    {
12       if(this == MAXINT)
13          sprintf(string, "Inf");
14       else if(this == MININT) //-MAXINT)
15          sprintf(string, "-Inf");
16       else
17          sprintf(string, "%d", this);
18       return string;
19    }
20
21    bool OnGetDataFromString(const char * string)
22    {
23       if(!strcmpi(string, "Inf"))
24          this = MAXINT;
25       else if(!strcmpi(string, "-Inf"))
26          this = MININT; //-MAXINT;
27       else
28          this = strtol(string, null, 0);
29       return true;
30    }
31 };
32
33 public struct Size { MinMaxValue w, h; };
34
35
36 public struct Box
37 {
38    int left, top, right, bottom;
39
40    // Clip "box" against "Against" after adjusting position by (x,y)
41    void ClipOffset(Box against, int x, int y)
42    {
43       // Clip Top Left
44       //if(against)
45       {
46          if(left   + x < against.left)   left   = against.left   - x;
47          if(top    + y < against.top)    top    = against.top    - y;
48          if(right  + x > against.right)  right  = against.right  - x;
49          if(bottom + y > against.bottom) bottom = against.bottom - y;
50
51          if((left   + x > against.right) || // left  = against.right - x +1;
52             (top    + y > against.bottom)|| // top   = against.bottom - y +1;
53             (right  + x < against.left)  || // right = against.left - x;
54             (bottom + y < against.top))     // bottom = against.top - y;
55          {
56             top = MAXINT;
57             left = MAXINT;
58             bottom = MININT;
59             right = MININT;
60          }
61       }
62    }
63
64    void Clip(Box against)
65    {
66       // Clip Top Left
67       if(against != null)
68       {
69          if(left   < against.left)   left   = against.left;
70          if(top    < against.top)    top    = against.top;
71          if(right  > against.right)  right  = against.right;
72          if(bottom > against.bottom) bottom = against.bottom;
73
74          if((left   > against.right)  ||
75             (top    > against.bottom) ||
76             (right  < against.left)   ||
77             (bottom < against.top))
78          {
79             top = MAXINT;
80             left = MAXINT;
81             bottom = MININT;
82             right = MININT;
83          }
84       }
85    }
86
87    bool Overlap(Box box)
88    {
89       return left < box.right && right > box.left && top < box.bottom && bottom > box.top;
90    }
91
92    bool IsPointInside(Point point)
93    {
94       return point.x >= left && point.y >= top && point.x <= right && point.y <= bottom;
95    }
96
97    property int width
98    {
99       set { right = left + value - 1; }
100       get { return right - left + 1; }
101    }
102
103    property int height
104    {
105       set { bottom = top + value - 1; }
106       get { return bottom - top + 1; }
107    }
108 };
109
110 public class Radians : Angle
111 {
112    public property Angle { };
113 }
114
115 public class Degrees : Angle
116 {
117    public property Radians
118    {
119       //get { return (Angle)this * Pi / 180; }
120       //set { return (Angle)(value * 180 / Pi); }
121       get { return this * (double)Pi / 180; }
122       set { return (double)value * 180 / Pi; }
123    }
124 }
125
126 public define Pi = Radians { 3.1415926535897932384626433832795028841971 };
127
128 public class Distance      : double;
129 public class Meters        : Distance { public property Distance {} };
130 public class Centimeters   : Distance { public property Meters { set { return value * 100.0f;  } get { return this / 100.0f; } } };
131 public class Feet          : Distance { public property Meters { set { return value / 0.3048f; } get { return this * 0.3048f; } } };
132 public class NauticalMiles : Distance { public property Meters { set { return value / 1852; }    get { return this * 1852; } } };
133
134 public struct WorldPoint
135 {
136    NauticalMiles x, y;
137 };
138
139 public struct PolarPoint
140 {
141    Degrees angle;
142    NauticalMiles distance;
143
144    property WorldPoint
145    {
146       set
147       {
148          angle = atan2(value.y, value.x);
149          distance = Distance { sqrt((double)(Distance)value.x * (Distance)value.x + (double)(Distance)value.y * (Distance)value.y) };
150       }
151
152       get
153       {
154          value.x = cos(angle) * distance;
155          value.y = sin(angle) * distance;
156       }
157    }
158 };
159
160 public struct GeoPoint
161 {
162    Degrees lat, lon;
163 };