| 1 |
#ifndef tfunction_h |
| 2 |
#define tfunction_h |
| 3 |
|
| 4 |
#include <stdio.h> |
| 5 |
#include <string.h> |
| 6 |
|
| 7 |
typedef enum { |
| 8 |
FIRST = 1, |
| 9 |
SECOND = 2 |
| 10 |
} Order; |
| 11 |
|
| 12 |
class Point |
| 13 |
{ |
| 14 |
char m_s[64]; |
| 15 |
float m_x; |
| 16 |
float m_y; |
| 17 |
|
| 18 |
public: |
| 19 |
|
| 20 |
enum Error { |
| 21 |
SUCCESS = 0, |
| 22 |
ERROR = 1 |
| 23 |
}; |
| 24 |
|
| 25 |
Point (float x=0, float y=0) |
| 26 |
: m_x(x), m_y(y) |
| 27 |
{ |
| 28 |
} |
| 29 |
virtual ~Point () |
| 30 |
{ |
| 31 |
} |
| 32 |
|
| 33 |
void set (float x, float y) |
| 34 |
{ |
| 35 |
m_x = x, m_y = y; |
| 36 |
} |
| 37 |
void set (float v[2]=0) |
| 38 |
{ |
| 39 |
m_x = v[0], m_y=v[1]; |
| 40 |
} |
| 41 |
void setpointer (Point* p) |
| 42 |
{ |
| 43 |
*this = *p; |
| 44 |
} |
| 45 |
void setref (Point& p) |
| 46 |
{ |
| 47 |
*this = p; |
| 48 |
} |
| 49 |
void setvalue (Point p) |
| 50 |
{ |
| 51 |
*this = p; |
| 52 |
} |
| 53 |
void setconst (const Point* p) |
| 54 |
{ |
| 55 |
*this = *p; |
| 56 |
} |
| 57 |
void setname (const char* s) |
| 58 |
{ |
| 59 |
strncpy(m_s,s,63); |
| 60 |
} |
| 61 |
|
| 62 |
void get (float* x, float* y) const |
| 63 |
{ |
| 64 |
*x = m_x, *y = m_y; |
| 65 |
} |
| 66 |
void get (float v[2]) const |
| 67 |
{ |
| 68 |
v[0] = m_x, v[1] = m_y; |
| 69 |
} |
| 70 |
Point* getpointer () |
| 71 |
{ |
| 72 |
return this; |
| 73 |
} |
| 74 |
Point& getref () |
| 75 |
{ |
| 76 |
return *this; |
| 77 |
} |
| 78 |
Point getvalue () |
| 79 |
{ |
| 80 |
return *this; |
| 81 |
} |
| 82 |
const Point* getconst () const |
| 83 |
{ |
| 84 |
return this; |
| 85 |
} |
| 86 |
const char* getname () const |
| 87 |
{ |
| 88 |
return m_s; |
| 89 |
} |
| 90 |
|
| 91 |
Point operator+ (const Point& p) const |
| 92 |
{ |
| 93 |
return Point(m_x+p.m_x,m_y+p.m_y); |
| 94 |
} |
| 95 |
Point operator- (const Point& p) const |
| 96 |
{ |
| 97 |
return Point(m_x-p.m_x,m_y-p.m_y); |
| 98 |
} |
| 99 |
Point operator* (const Point& p) const |
| 100 |
{ |
| 101 |
return Point(m_x*p.m_x,m_y*p.m_y); |
| 102 |
} |
| 103 |
Point operator/ (float n) const |
| 104 |
{ |
| 105 |
return Point(m_x/n,m_y/n); |
| 106 |
} |
| 107 |
bool operator< (const Point& p) const |
| 108 |
{ |
| 109 |
if (m_x < p.m_x) return true; |
| 110 |
else if (m_x > p.m_x) return false; |
| 111 |
else return m_y < p.m_y; |
| 112 |
} |
| 113 |
bool operator<= (const Point& p) const |
| 114 |
{ |
| 115 |
return operator<(p) || operator==(p); |
| 116 |
} |
| 117 |
bool operator== (const Point& p) const |
| 118 |
{ |
| 119 |
return m_x==p.m_x && m_y==p.m_y; |
| 120 |
} |
| 121 |
|
| 122 |
float operator[] (int i) const |
| 123 |
{ |
| 124 |
return (i==0) ? m_x : m_y; |
| 125 |
} |
| 126 |
float& operator[] (int i) |
| 127 |
{ |
| 128 |
return (i==0) ? m_x : m_y; |
| 129 |
} |
| 130 |
|
| 131 |
static Error echo (Error e) |
| 132 |
{ |
| 133 |
return e; |
| 134 |
} |
| 135 |
|
| 136 |
}; |
| 137 |
|
| 138 |
|
| 139 |
inline Point add (const Point& p1, const Point& p2) |
| 140 |
{ |
| 141 |
return p1+p2; |
| 142 |
} |
| 143 |
inline Point sub (const Point& p1, const Point& p2) |
| 144 |
{ |
| 145 |
return p1-p2; |
| 146 |
} |
| 147 |
inline Point mult (const Point& p1, const Point& p2) |
| 148 |
{ |
| 149 |
return p1*p2; |
| 150 |
} |
| 151 |
inline Point div (const Point& p1, float n) |
| 152 |
{ |
| 153 |
return p1/n; |
| 154 |
} |
| 155 |
|
| 156 |
inline void getpoint (const Point* p, float* x, float* y) |
| 157 |
{ |
| 158 |
p->get(x,y); |
| 159 |
} |
| 160 |
inline void setpoint (Point* p, float x=0, float y=0) |
| 161 |
{ |
| 162 |
p->set(x,y); |
| 163 |
} |
| 164 |
|
| 165 |
inline Point average (int n, Point v[]) |
| 166 |
{ |
| 167 |
Point p(0,0); |
| 168 |
for (int i=0; i<n; ++i) |
| 169 |
p = p+v[i]; |
| 170 |
return p/n; |
| 171 |
} |
| 172 |
|
| 173 |
inline Point averagepointer (int n, Point* v[]) |
| 174 |
{ |
| 175 |
Point p(0,0); |
| 176 |
for (int i=0; i<n; ++i) |
| 177 |
p = p+(*v[i]); |
| 178 |
return p/n; |
| 179 |
} |
| 180 |
|
| 181 |
inline void copyvector (int n, const Point v[], Point u[]) |
| 182 |
{ |
| 183 |
for (int i=0; i<n; ++i) |
| 184 |
u[i] = v[i]; |
| 185 |
} |
| 186 |
|
| 187 |
inline Order invert (Order o) |
| 188 |
{ |
| 189 |
if (o==FIRST) |
| 190 |
return SECOND; |
| 191 |
else |
| 192 |
return FIRST; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
class ColorPoint : public Point //tolua_export |
| 197 |
{ //tolua_export |
| 198 |
float r, g, b; |
| 199 |
public: |
| 200 |
|
| 201 |
//tolua_begin |
| 202 |
ColorPoint (float px, float py, float cr=0.0f, float cg=0.0f, float cb=0.0f) |
| 203 |
: Point(px,py), r(cr), g(cg), b(cb) |
| 204 |
{ |
| 205 |
} |
| 206 |
virtual ~ColorPoint () { } |
| 207 |
|
| 208 |
virtual void getcolor (float* red, float *green, float *blue) const |
| 209 |
{ |
| 210 |
*red = r; |
| 211 |
*green = g; |
| 212 |
*blue = b; |
| 213 |
} |
| 214 |
static ColorPoint* MakeRed (float x, float y) |
| 215 |
{ |
| 216 |
return new ColorPoint(x,y,1,0,0); |
| 217 |
} |
| 218 |
}; |
| 219 |
//tolua_end |
| 220 |
#endif |