quad.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2013 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 ** Mark Page
28 */
29 
30 
31 #pragma once
32 
33 #include "../api_core.h"
34 #include "rect.h"
35 #include "size.h"
36 #include "point.h"
37 #include "origin.h"
38 
39 namespace clan
40 {
43 
48 template<typename Type>
49 class CL_API_CORE Quadx
50 {
53 
54 public:
56  Quadx() { }
57 
64  Quadx(const Vec2<Type> &new_p, const Vec2<Type> &new_q, const Vec2<Type> &new_r, const Vec2<Type> &new_s)
65  { p = new_p; q = new_q; r = new_r; s = new_s; }
66 
78  Quadx(const Rectx<Type> &rect)
79  { p.x = rect.left; p.y = rect.top; q.x = rect.right; q.y = rect.top;
80  r.x = rect.right; r.y = rect.bottom; s.x = rect.left; s.y = rect.bottom;
81  }
82 
86  Quadx(const Quadx<Type> &quad)
87  { p = quad.p; q = quad.q; r = quad.r; s = quad.s; }
88 
91  { p += quad.p; q += quad.q; r += quad.r; s += quad.s; return *this; }
92 
95  { p -= quad.p; q -= quad.q; r -= quad.r; s -= quad.s; return *this; }
96 
99  { p += point; q += point; r += point; s += point; return *this; }
100 
103  { p -= point; q -= point; r -= point; s -= point; return *this; }
104 
106  Quadx<Type> operator+(const Quadx<Type> &quad) const
107  { return Quadx(p + quad.p, q + quad.q, r + quad.r, s + quad.s); }
108 
110  Quadx<Type> operator-(const Quadx<Type> &quad) const
111  { return Quadx(p - quad.p, q - quad.q, r - quad.r, s - quad.s); }
112 
114  Quadx<Type> operator+(const Vec2<Type> &point) const
115  { return Quadx(p + point, q + point, r + point, s + point); }
116 
118  Quadx<Type> operator-(const Vec2<Type> &point) const
119  { return Quadx(p - point, q - point, r - point, s - point); }
120 
122  bool operator==(const Quadx<Type> &quad) const
123  { return (p == quad.p && q == quad.q && r == quad.r && s == quad.s); }
124 
126  bool operator!=(const Quadx<Type> &quad) const
127  { return (p != quad.p || q != quad.q || r != quad.r || s != quad.s); }
128 
132 
133 public:
136 
139 
142 
145 
147  Type get_width() const;
148 
150  Type get_height() const;
151 
153  Sizex<Type> get_size() const { return Sizex<Type>(get_width(), get_height()); }
154 
156  Rect get_bounds() const;
157 
161 
162 public:
169  Quadx<Type> &rotate(const Vec2<Type> &hotspot, const Angle &angle);
170 
177  Quadx<Type> &scale(float sx, float sy);
178 
186  Quadx<Type> &scale(const Vec2<Type> &hotspot, float sx, float sy);
187 
189  Vec2<Type> center() const;
190 
197  Quadx<Type> &apply_alignment(Origin origin, Type x, Type y);
198 
200  bool is_inside(const Vec2<Type> &point) const;
201 
203 };
204 
206 class Quad : public Quadx<int>
207 {
208 public:
209  Quad() : Quadx<int>() {}
210  Quad(const Vec2<int> &new_p, const Vec2<int> &new_q, const Vec2<int> &new_r, const Vec2<int> &new_s) : Quadx<int>(new_p, new_q, new_r, new_s) {}
211  Quad(const Rect &rect) : Quadx<int>(rect) {}
212  Quad(const Quadx<int> &quad) : Quadx<int>(quad) {}
213 };
214 
216 class Quadf : public Quadx<float>
217 {
218 public:
219  Quadf() : Quadx<float>() {}
220  Quadf(const Vec2<float> &new_p, const Vec2<float> &new_q, const Vec2<float> &new_r, const Vec2<float> &new_s) : Quadx<float>(new_p, new_q, new_r, new_s) {}
221  Quadf(const Rectf &rect) : Quadx<float>(rect) {}
222  Quadf(const Quadx<float> &quad) : Quadx<float>(quad) {}
223 };
224 
226 class Quadd : public Quadx<double>
227 {
228 public:
229  Quadd() : Quadx<double>() {}
230  Quadd(const Vec2<double> &new_p, const Vec2<double> &new_q, const Vec2<double> &new_r, const Vec2<double> &new_s) : Quadx<double>(new_p, new_q, new_r, new_s) {}
231  Quadd(const Rectd &rect) : Quadx<double>(rect) {}
232  Quadd(const Quadx<double> &quad) : Quadx<double>(quad) {}
233 };
234 
235 }
236 
2D (left,top,right,bottom) rectangle structure - Double
Definition: rect.h:482
Quadd(const Quadx< double > &quad)
Definition: quad.h:232
2D quad structure - Double
Definition: quad.h:226
Angle class.
Definition: angle.h:63
Vec2< Type > s
Fourth Point.
Definition: quad.h:144
Quadf(const Rectf &rect)
Definition: quad.h:221
Quadf()
Definition: quad.h:219
Quadf(const Quadx< float > &quad)
Definition: quad.h:222
Quadx(const Vec2< Type > &new_p, const Vec2< Type > &new_q, const Vec2< Type > &new_r, const Vec2< Type > &new_s)
Constructs a quad.
Definition: quad.h:64
Origin
Alignment origins.
Definition: origin.h:41
Quad(const Rect &rect)
Definition: quad.h:211
Quadx(const Quadx< Type > &quad)
Constructs a quad.
Definition: quad.h:86
Vec2< Type > r
Third Point.
Definition: quad.h:141
Quad()
Definition: quad.h:209
Type top
Y1-coordinate (top point inside the rectangle)
Definition: rect.h:122
Sizex< Type > get_size() const
Returns the size of the rectangle.
Definition: quad.h:153
Quadx< Type > operator+(const Vec2< Type > &point) const
Quad + Point operator.
Definition: quad.h:114
Quadx< Type > & operator-=(const Quadx< Type > &quad)
Quad -= Quad operator.
Definition: quad.h:94
2D (left,top,right,bottom) rectangle structure - Integer
Definition: rect.h:453
Quadx< Type > operator-(const Quadx< Type > &quad) const
Quad - Quad operator.
Definition: quad.h:110
Quadx()
Constructs a quad.
Definition: quad.h:56
Quad(const Vec2< int > &new_p, const Vec2< int > &new_q, const Vec2< int > &new_r, const Vec2< int > &new_s)
Definition: quad.h:210
2D (left,top,right,bottom) rectangle structure - Float
Definition: rect.h:467
2D quad structure - Float
Definition: quad.h:216
Vec2< Type > p
First Point.
Definition: quad.h:135
2D quad structure - Integer
Definition: quad.h:206
2D vector
Definition: line.h:49
Quadx< Type > operator+(const Quadx< Type > &quad) const
Quad + Quad operator.
Definition: quad.h:106
Quadx< Type > & operator-=(const Vec2< Type > &point)
Quad -= Point operator.
Definition: quad.h:102
2D (left,top,right,bottom) rectangle structure.
Definition: line.h:46
Quadf(const Vec2< float > &new_p, const Vec2< float > &new_q, const Vec2< float > &new_r, const Vec2< float > &new_s)
Definition: quad.h:220
2D quad structure.
Definition: quad.h:49
Type left
X1-coordinate (left point inside the rectangle)
Definition: rect.h:119
Quad(const Quadx< int > &quad)
Definition: quad.h:212
Quadx< Type > & operator+=(const Quadx< Type > &quad)
Quad += Quad operator.
Definition: quad.h:90
Type right
X2-coordinate (point outside the rectangle)
Definition: rect.h:125
Vec2< Type > q
Second Point.
Definition: quad.h:138
Quadd()
Definition: quad.h:229
Quadx< Type > operator-(const Vec2< Type > &point) const
Quad - Point operator.
Definition: quad.h:118
Quadx(const Rectx< Type > &rect)
Constructs a quad.
Definition: quad.h:78
Quadd(const Vec2< double > &new_p, const Vec2< double > &new_q, const Vec2< double > &new_r, const Vec2< double > &new_s)
Definition: quad.h:230
bool operator==(const Quadx< Type > &quad) const
Quad == Quad operator.
Definition: quad.h:122
Type bottom
Y2-coordinate (point outside the rectange)
Definition: rect.h:128
bool operator!=(const Quadx< Type > &quad) const
Quad != Quad operator.
Definition: quad.h:126
2D (width,height) size structure.
Definition: size.h:55
Quadx< Type > & operator+=(const Vec2< Type > &point)
Quad += Point operator.
Definition: quad.h:98
Quadd(const Rectd &rect)
Definition: quad.h:231