Schrift erweitert.
[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 return r;
17 }
18
19 static inline vec2d vec2d_sub(vec2d v1, vec2d v2) {
20 vec2d r = {
21 fixed_point_sub(v1.x, v2.x),
22 fixed_point_sub(v1.y, v2.y)
23 };
24 return r;
25 }
26
27 static inline vec2d vec2d_neg(vec2d v) {
28 vec2d r = {
29 fixed_point_neg(v.x),
30 fixed_point_neg(v.y)
31 };
32 return r;
33 }
34
35 typedef struct {
36 vec2d pos;
37 vec2d extent;
38 } rectangle;
39
40 static inline rectangle rectangle_new(vec2d pos, vec2d extent) { rectangle r = { pos, extent }; return r; }
41
42 static inline fixed_point rectangle_top (rectangle const *r) { return r->pos.y; }
43 static inline fixed_point rectangle_left (rectangle const *r) { return r->pos.x; }
44 static inline fixed_point rectangle_bottom(rectangle const *r) { return fixed_point_add(rectangle_top (r), r->extent.y); }
45 static inline fixed_point rectangle_right (rectangle const *r) { return fixed_point_add(rectangle_left(r), r->extent.x); }
46
47 static inline fixed_point rectangle_width (rectangle const *r) { return r->extent.x; }
48 static inline fixed_point rectangle_height(rectangle const *r) { return r->extent.y; }
49
50 static inline fixed_point rectangle_mid_x (rectangle const *r) { return fixed_point_add(r->pos.x, fixed_point_div(r->extent.x, FIXED_INT(2))); }
51 static inline fixed_point rectangle_mid_y (rectangle const *r) { return fixed_point_add(r->pos.y, fixed_point_div(r->extent.y, FIXED_INT(2))); }
52
53 static inline vec2d rectangle_mid (rectangle const *r) { vec2d v = { rectangle_mid_x(r), rectangle_mid_y(r) }; return v; }
54
55 static inline void rectangle_move_to (rectangle *r, vec2d new_pos) { r->pos = new_pos; }
56 static inline void rectangle_move_to_x(rectangle *r, fixed_point new_x ) { r->pos.x = new_x; }
57 static inline void rectangle_move_to_y(rectangle *r, fixed_point new_y ) { r->pos.x = new_y; }
58
59 static inline void rectangle_move_rel (rectangle *r, vec2d vec ) { r->pos = vec2d_add(r->pos, vec); }
60 static inline void rectangle_expand (rectangle *r, vec2d extent ) { r->extent = extent; }
61
62 static inline bool rectangle_intersect(rectangle const *r1,
63 rectangle const *r2) {
64 return (fixed_point_lt(rectangle_top (r1), rectangle_bottom(r2)) &&
65 fixed_point_gt(rectangle_bottom(r1), rectangle_top (r2)) &&
66 fixed_point_lt(rectangle_left (r1), rectangle_right (r2)) &&
67 fixed_point_gt(rectangle_right (r1), rectangle_left (r2)));
68 }
69
70 #endif
This page took 0.055138 seconds and 5 git commands to generate.