mockup/util übernommen (fixed point, rectangle)
authorWintermate <wintermute@hannover.ccc.de>
Tue, 8 Oct 2013 00:49:33 +0000 (02:49 +0200)
committerWintermate <wintermute@hannover.ccc.de>
Tue, 8 Oct 2013 00:49:33 +0000 (02:49 +0200)
badge/util/fixed_point.h [new file with mode: 0644]
badge/util/rectangle.h [new file with mode: 0644]

diff --git a/badge/util/fixed_point.h b/badge/util/fixed_point.h
new file mode 100644 (file)
index 0000000..05a8c79
--- /dev/null
@@ -0,0 +1,37 @@
+#ifndef INCLUDED_FIXED_POINT_H
+#define INCLUDED_FIXED_POINT_H
+
+#include <stdint.h>
+#include <stdbool.h>
+
+/* Unterstes Byte als Nachkomma. 16 Bit Nutzlast! Sonst geht Multiplikation flöten. */
+typedef struct { int32_t data; } fixed_point;
+
+static inline fixed_point fixed_point_add(fixed_point x, fixed_point y) { fixed_point r = { x.data + y.data         }; return r; }
+static inline fixed_point fixed_point_sub(fixed_point x, fixed_point y) { fixed_point r = { x.data - y.data         }; return r; }
+static inline fixed_point fixed_point_mul(fixed_point x, fixed_point y) { fixed_point r = { x.data * y.data / 256   }; return r; }
+static inline fixed_point fixed_point_div(fixed_point x, fixed_point y) { fixed_point r = { (x.data * 256) / y.data }; return r; }
+
+static inline fixed_point fixed_point_neg(fixed_point x) { fixed_point r = { -x.data                       }; return r; }
+static inline fixed_point fixed_point_abs(fixed_point x) { fixed_point r = { x.data < 0 ? -x.data : x.data }; return r; }
+
+static inline bool fixed_point_lt(fixed_point x, fixed_point y) { return x.data <  y.data; }
+static inline bool fixed_point_gt(fixed_point x, fixed_point y) { return x.data >  y.data; }
+static inline bool fixed_point_le(fixed_point x, fixed_point y) { return x.data <= y.data; }
+static inline bool fixed_point_ge(fixed_point x, fixed_point y) { return x.data >= y.data; }
+static inline bool fixed_point_eq(fixed_point x, fixed_point y) { return x.data == y.data; }
+static inline bool fixed_point_ne(fixed_point x, fixed_point y) { return x.data != y.data; }
+
+#define FIXED_POINT_I(x, y) { ((x) * 256) + ((y) * 256 / 1000) }
+
+static inline fixed_point FIXED_POINT(unsigned x, unsigned y) {
+  fixed_point r = { ((int) x * 256) + ((int) y * 256 / 1000) };
+  return r;
+}
+
+static inline int fixed_point_cast_int(fixed_point x) { return x.data / 256; }
+
+static inline fixed_point fixed_point_min(fixed_point x, fixed_point y) { return fixed_point_lt(x, y) ? x : y; }
+static inline fixed_point fixed_point_max(fixed_point x, fixed_point y) { return fixed_point_gt(x, y) ? x : y; }
+
+#endif
diff --git a/badge/util/rectangle.h b/badge/util/rectangle.h
new file mode 100644 (file)
index 0000000..8653e6b
--- /dev/null
@@ -0,0 +1,52 @@
+#ifndef INCLUDED_RECTANGLE_H
+#define INCLUDED_RECTANGLE_H
+
+#include "fixed_point.h"
+
+typedef struct {
+  fixed_point x;
+  fixed_point y;
+} vec2d;
+
+static inline vec2d vec2d_add(vec2d v1, vec2d v2) {
+  vec2d r = {
+    fixed_point_add(v1.x, v2.x),
+    fixed_point_add(v1.y, v2.y)
+  };
+
+  return r;
+}
+
+typedef struct {
+  vec2d pos;
+  vec2d extent;
+} rectangle;
+
+static inline fixed_point rectangle_top   (rectangle const *r) { return r->pos.y; }
+static inline fixed_point rectangle_left  (rectangle const *r) { return r->pos.x; }
+static inline fixed_point rectangle_bottom(rectangle const *r) { return fixed_point_add(rectangle_top (r), r->extent.y); }
+static inline fixed_point rectangle_right (rectangle const *r) { return fixed_point_add(rectangle_left(r), r->extent.x); }
+
+static inline fixed_point rectangle_width (rectangle const *r) { return r->extent.x; }
+static inline fixed_point rectangle_height(rectangle const *r) { return r->extent.y; }
+
+static inline vec2d rectangle_mid(rectangle const *r) {
+  vec2d v = { fixed_point_add(r->pos.x, fixed_point_div(r->extent.x, FIXED_POINT(2, 0))),
+             fixed_point_add(r->pos.y, fixed_point_div(r->extent.y, FIXED_POINT(2, 0)))
+  };
+  return v;
+}
+
+static inline void rectangle_move_to  (rectangle *r, vec2d new_pos) { r->pos = new_pos; }
+static inline void rectangle_move_to_x(rectangle *r, fixed_point    new_x  ) { r->pos.x = new_x; }
+static inline void rectangle_move_to_y(rectangle *r, fixed_point    new_y  ) { r->pos.x = new_y; }
+
+static inline bool rectangle_intersect(rectangle const *r1,
+                                      rectangle const *r2) {
+  return (fixed_point_lt(rectangle_top   (r1), rectangle_bottom(r2)) &&
+         fixed_point_gt(rectangle_bottom(r1), rectangle_top   (r2)) &&
+         fixed_point_lt(rectangle_left  (r1), rectangle_right (r2)) &&
+         fixed_point_gt(rectangle_right (r1), rectangle_left  (r2)));
+}
+
+#endif
This page took 0.029229 seconds and 4 git commands to generate.