libvisual  0.5.0
lv_rectangle.h
1 /* Libvisual - The audio visualisation framework.
2  *
3  * Copyright (C) 2012 Libvisual team
4  * 2004-2006 Dennis Smit
5  *
6  * Authors: Chong Kai Xiong <kaixiong@codeleft.sg>
7  * Dennis Smit <ds@nerds-incorporated.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser General Public License as
11  * published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */
23 
24 #ifndef _LV_RECTANGLE_H
25 #define _LV_RECTANGLE_H
26 
32 #ifdef __cplusplus
33 
34 #include <libvisual/lv_math.h>
35 
36 namespace LV {
37 
39  class LV_API Rect
40  {
41  public:
42 
43  int x;
44  int y;
45  int width;
46  int height;
47 
51  Rect ()
52  : x (0), y (0), width (0), height (0)
53  {}
54 
63  Rect (int x_, int y_, int width_, int height_)
64  : x (x_), y (y_), width (width_), height (height_)
65  {}
66 
73  Rect (int width_, int height_)
74  : x (0), y (0), width (width_), height (height_)
75  {}
76 
85  void set (int x_, int y_, int width_, int height_)
86  {
87  x = x_; y = y_; width = width_; height = height_;
88  }
89 
96  bool empty () const
97  {
98  return ( width <= 0 || height <= 0 );
99  }
100 
109  bool contains (int x_, int y_) const
110  {
111  return ( x_ >= x && x_ <= x + width && y_ >= y && y_ <= y + height );
112  }
113 
122  bool contains (Rect const& r) const;
123 
131  bool intersects (Rect const& r) const;
132 
140  Rect clip (Rect const& r) const;
141 
148  void normalize ()
149  {
150  x = y = 0;
151  }
152 
159  void normalize_to (Rect const& r)
160  {
161  x = r.x; y = r.y;
162  }
163 
179  void denormalize_point (float fx, float fy, int32_t& x_, int32_t& y_) const
180  {
181  fx = clamp (fx, 0.0f, 1.0f);
182  fy = clamp (fx, 0.0f, 1.0f);
183 
184  x_ = x + fx * width;
185  y_ = y + fy * height;
186  }
187 
200  void denormalize_points (float const* fxlist, float const* fylist, int32_t* xlist, int32_t* ylist, unsigned int size) const;
201 
216  void denormalize_point_neg (float fx, float fy, int32_t& x_, int32_t& y_) const
217  {
218  fx = clamp (fx, -1.0f, 1.0f) * 0.5f + 0.5f;
219  fy = clamp (fy, -1.0f, 1.0f) * 0.5f + 0.5f;
220 
221  x_ = x + fx * width;
222  y_ = y + fy * height;
223  }
224 
237  void denormalize_points_neg (float const* fxlist, float const* fylist, int32_t* xlist, int32_t* ylist, unsigned int size) const;
238  };
239 
240 } // LV namespace
241 
242 #endif /* __cplusplus */
243 
244 #ifdef __cplusplus
245 typedef ::LV::Rect VisRectangle;
246 #else
247 typedef struct _VisRectangle VisRectangle;
248 struct _VisRectangle;
249 #endif
250 
251 LV_BEGIN_DECLS
252 
253 LV_API VisRectangle *visual_rectangle_new (int x, int y, int width, int height);
254 LV_API VisRectangle *visual_rectangle_new_empty (void);
255 
256 LV_API void visual_rectangle_free (VisRectangle *rect);
257 
258 LV_API void visual_rectangle_set (VisRectangle *rect, int x, int y, int width, int height);
259 LV_API void visual_rectangle_copy (VisRectangle *dest, VisRectangle *src);
260 
261 LV_API VisRectangle *visual_rectangle_clone (VisRectangle *rect);
262 
263 LV_API void visual_rectangle_set_x (VisRectangle *rect, int x);
264 LV_API int visual_rectangle_get_x (VisRectangle *rect);
265 LV_API void visual_rectangle_set_y (VisRectangle *rect, int y);
266 LV_API int visual_rectangle_get_y (VisRectangle *rect);
267 LV_API void visual_rectangle_set_width (VisRectangle *rect, int width);
268 LV_API int visual_rectangle_get_width (VisRectangle *rect);
269 LV_API void visual_rectangle_set_height (VisRectangle *rect, int height);
270 LV_API int visual_rectangle_get_height (VisRectangle *rect);
271 
272 LV_API int visual_rectangle_is_empty (VisRectangle *rect);
273 LV_API int visual_rectangle_intersects (VisRectangle *dest, VisRectangle *src);
274 LV_API int visual_rectangle_contains_point (VisRectangle *rect, int x, int y);
275 LV_API int visual_rectangle_contains_rect (VisRectangle *dest, VisRectangle *src);
276 LV_API void visual_rectangle_clip (VisRectangle *dest, VisRectangle *within, VisRectangle *src);
277 
278 LV_API void visual_rectangle_normalize (VisRectangle *rect);
279 LV_API void visual_rectangle_normalize_to (VisRectangle *dest, VisRectangle *src);
280 
281 LV_API void visual_rectangle_denormalize_point (VisRectangle *rect, float fx, float fy, int32_t *x, int32_t *y);
282 LV_API void visual_rectangle_denormalize_points (VisRectangle *rect, const float *fxlist, const float *fylist, int32_t *xlist, int32_t *ylist, unsigned int size);
283 
284 LV_API void visual_rectangle_denormalize_point_neg (VisRectangle *rect, float fx, float fy, int32_t *x, int32_t *y);
285 LV_API void visual_rectangle_denormalize_points_neg (VisRectangle *rect, const float *fxlist, const float *fylist, int32_t *xlist, int32_t *ylist, unsigned int size);
286 
287 LV_END_DECLS
288 
293 #endif /* _LV_RECTANGLE_H */