Verbesserte Rotor-Sprites.
[hackover2013-badge-firmware.git] / badge / util / rectangle.h
1 #ifndef INCLUDED_RECTANGLE_H
2 #define INCLUDED_RECTANGLE_H
3
4 #include "fixed_point.h"
5
6 typedef struct {
7 fixed_point x;
8 fixed_point y;
9 } vec2d;
10
11 static inline vec2d vec2d_add(vec2d v1, vec2d v2) {
12 vec2d r = {
13 fixed_point_add(v1.x, v2.x),
14 fixed_point_add(v1.y, v2.y)
15 };
16
17 return r;
18 }
19
20 static inline vec2d vec2d_neg(vec2d v) {
21 vec2d r = {
22 fixed_point_neg(v.x),
23 fixed_point_neg(v.y)
24 };
25 return r;
26 }
27
28 typedef struct {
29 vec2d pos;
30 vec2d extent;
31 } rectangle;
32
33 static inline rectangle rectangle_new(vec2d pos, vec2d extent) { rectangle r = { pos, extent }; return r; }
34
35 static inline fixed_point rectangle_top (rectangle const *r) { return r->pos.y; }
36 static inline fixed_point rectangle_left (rectangle const *r) { return r->pos.x; }
37 static inline fixed_point rectangle_bottom(rectangle const *r) { return fixed_point_add(rectangle_top (r), r->extent.y); }
38 static inline fixed_point rectangle_right (rectangle const *r) { return fixed_point_add(rectangle_left(r), r->extent.x); }
39
40 static inline fixed_point rectangle_width (rectangle const *r) { return r->extent.x; }
41 static inline fixed_point rectangle_height(rectangle const *r) { return r->extent.y; }
42
43 static inline vec2d rectangle_mid(rectangle const *r) {
44 vec2d v = { fixed_point_add(r->pos.x, fixed_point_div(r->extent.x, FIXED_POINT(2, 0))),
45 fixed_point_add(r->pos.y, fixed_point_div(r->extent.y, FIXED_POINT(2, 0)))
46 };
47 return v;
48 }
49
50 static inline void rectangle_move_to (rectangle *r, vec2d new_pos) { r->pos = new_pos; }
51 static inline void rectangle_move_to_x(rectangle *r, fixed_point new_x ) { r->pos.x = new_x; }
52 static inline void rectangle_move_to_y(rectangle *r, fixed_point new_y ) { r->pos.x = new_y; }
53
54 static inline void rectangle_move_rel (rectangle *r, vec2d vec ) { r->pos = vec2d_add(r->pos, vec); }
55 static inline void rectangle_expand (rectangle *r, vec2d extent ) { r->extent = extent; }
56
57 static inline bool rectangle_intersect(rectangle const *r1,
58 rectangle const *r2) {
59 return (fixed_point_lt(rectangle_top (r1), rectangle_bottom(r2)) &&
60 fixed_point_gt(rectangle_bottom(r1), rectangle_top (r2)) &&
61 fixed_point_lt(rectangle_left (r1), rectangle_right (r2)) &&
62 fixed_point_gt(rectangle_right (r1), rectangle_left (r2)));
63 }
64
65 #endif
This page took 0.046542 seconds and 5 git commands to generate.