summaryrefslogtreecommitdiff
path: root/tolua/src/tests/tfunction.h
blob: a14bb6e63fdcda0653ed87d5ad0778d65c18649d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#ifndef tfunction_h
#define tfunction_h

#include <stdio.h>

typedef enum {
 FIRST = 1,
	SECOND = 2
} Order;

class Point
{          
 float m_x;
	float m_y;

public:

	enum Error {
		SUCCESS = 0,
		ERROR = 1
	};

	Point (float x=0, float y=0)
	: m_x(x), m_y(y)
	{
	}
	virtual ~Point ()
	{
	}

	void set (float x, float y)
	{
		m_x = x, m_y = y;
	}
	void set (float v[2]=0)
	{
		m_x = v[0], m_y=v[1];
	}
	void setpointer (Point* p)
	{
		*this = *p;
	}
	void setref (Point& p)
	{
		*this = p;
	}
	void setvalue (Point p)
	{
		*this = p;
	}
	void setconst (const Point* p)
	{
		*this = *p;
	}

	void get (float* x, float* y) const
	{
		*x = m_x, *y = m_y;
	}
	void get (float v[2]) const
	{
		v[0] = m_x, v[1] = m_y;
	}
	Point* getpointer ()
	{
		return this;
	}
	Point& getref ()
	{
		return *this;
	}
	Point getvalue ()
	{
		return *this;
	}
	const Point* getconst () const
	{
		return this;
	}

	Point operator+ (const Point& p) const
	{
		return Point(m_x+p.m_x,m_y+p.m_y);
	}
	Point operator- (const Point& p) const
	{
		return Point(m_x-p.m_x,m_y-p.m_y);
	}
	Point operator* (const Point& p) const
	{
		return Point(m_x*p.m_x,m_y*p.m_y);
	}
	Point operator/ (float n) const
	{
		return Point(m_x/n,m_y/n);
	}
	bool operator< (const Point& p) const
	{
		if (m_x < p.m_x) return true;
		else if (m_x > p.m_x) return false;
		else return m_y < p.m_y;
	}
	bool operator<= (const Point& p) const
	{
		return operator<(p) || operator==(p);
	}
	bool operator== (const Point& p) const
	{
		return m_x==p.m_x && m_y==p.m_y;
	}

 float operator[] (int i) const
	{
		return (i==0) ? m_x : m_y;
	}
	float& operator[] (int i)
	{
		return (i==0) ? m_x : m_y;
	}

	static Error echo (Error e)
	{
		return e;
	}

};


inline Point add (const Point& p1, const Point& p2)
{
	return p1+p2;
}
inline Point sub (const Point& p1, const Point& p2)
{
	return p1-p2;
}
inline Point mult (const Point& p1, const Point& p2)
{
	return p1*p2;
}
inline Point div (const Point& p1, float n)
{
	return p1/n;
}

inline void getpoint (const Point* p, float* x, float* y)
{
	p->get(x,y);
}
inline void setpoint (Point* p, float x=0, float y=0)
{
	p->set(x,y);
}

inline	Point average (int n, Point v[])
{
	Point p(0,0);
	for (int i=0; i<n; ++i)
		p = p+v[i];
	return p/n;
}

inline	Point averagepointer (int n, Point* v[])
{
	Point p(0,0);
	for (int i=0; i<n; ++i)
		p = p+(*v[i]);
	return p/n;
}

inline	void copyvector (int n, const Point v[], Point u[])
{
	for (int i=0; i<n; ++i)
		u[i] = v[i];
}

inline Order invert (Order o)
{
	if (o==FIRST)
		return SECOND;
	else
		return FIRST;
}


class ColorPoint : public Point   //tolua_export
{                                 //tolua_export
 float r, g, b;
public:

//tolua_begin
 ColorPoint (float px, float py, float cr=0.0f, float cg=0.0f, float cb=0.0f)
 : Point(px,py), r(cr), g(cg), b(cb)
 {
 }
 virtual ~ColorPoint () { }

 virtual void getcolor (float* red, float *green, float *blue) const
	{
		*red = r;
		*green = g;
		*blue = b;
	}
 static ColorPoint* MakeRed (float x, float y)
 {
  return new ColorPoint(x,y,1,0,0);
 }
};
//tolua_end
#endif