Added Coord/Offset header
authorMartin Read <martin@blackswordsonics.com>
Mon, 30 Sep 2013 20:13:16 +0000 (21:13 +0100)
committerMartin Read <martin@blackswordsonics.com>
Mon, 30 Sep 2013 20:13:16 +0000 (21:13 +0100)
coord.hh [new file with mode: 0644]

diff --git a/coord.hh b/coord.hh
new file mode 100644 (file)
index 0000000..c0124d0
--- /dev/null
+++ b/coord.hh
@@ -0,0 +1,69 @@
+/* \file coord.hh
+ * \brief Positions and offsets
+ */
+
+/* Copyright 2013 Martin Read
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef COORD_HH
+#define COORD_HH
+
+#include <algorithm>
+
+template <typename T> T myabs(T val) { return (val < 0) ? -val : val; }
+template <typename T> T mysign(T val) { return (val < 0) ? -1 : ((val > 0) ? 1 : 0); }
+
+struct Offset
+{
+    int y;
+    int x;
+    int len_cheb(void) const { return std::max(myabs(y), myabs(x)); }
+    int len_taxi(void) const { return myabs(y) + myabs(x); }
+    int lensq_eucl(void) const { return y * y + x * x; }
+    const Offset& operator +=(const Offset& right) { y += right.y; x += right.x; return *this; }
+    const Offset& operator -=(const Offset& right) { y -= right.y; x -= right.x; return *this; }
+    bool operator !=(const Offset& right) const { return (y != right.y) || (x != right.x); }
+    bool operator ==(const Offset& right) const { return (y == right.y) && (x == right.x); }
+};
+
+struct Coord
+{
+    int y;
+    int x;
+    Offset delta(const Coord& right) const { Offset d = { y - right.y, x - right.x }; return d; }
+    Coord operator +(const Offset& right) const { Coord c = { y + right.y, x - right.x}; return c; }
+    Coord operator -(const Offset& right) const { Coord c = { y - right.y, x - right.x}; return c; }
+    const Coord& operator +=(const Offset& right) { y += right.y; x += right.x; return *this;}
+    const Coord& operator -=(const Offset& right) { y -= right.y; x -= right.x; return *this;}
+    bool operator !=(const Coord& right) const { return (y != right.y) || (x != right.x); }
+    bool operator ==(const Coord& right) const { return (y == right.y) && (x == right.x); }
+};
+
+extern const Coord Nowhere;
+extern const Offset North, Northeast, East, Southeast, South, Southwest, West, Northwest, Stationary;
+
+#endif
+
+/* coord.hh */
+// vim:cindent