Browse Source

Add support for xy type

James Bowman 6 years ago
parent
commit
fe22526912
2 changed files with 25 additions and 1 deletions
  1. 22 1
      GD2.cpp
  2. 3 0
      GD2.h

+ 22 - 1
GD2.cpp

@@ -97,6 +97,25 @@ class xy xy::operator+=(class xy &other)
   return *this;
 }
 
+class xy xy::operator-=(class xy &other)
+{
+  x -= other.x;
+  y -= other.y;
+  return *this;
+}
+
+long xy::operator*(class xy &other)
+{
+  return (long(x) * other.x) + (long(y) * other.y);
+}
+
+class xy xy::operator*=(int s)
+{
+  x *= s;
+  y *= s;
+  return *this;
+}
+
 int xy::nearer_than(int distance, xy &other)
 {
   int lx = abs(x - other.x);
@@ -111,7 +130,9 @@ int xy::nearer_than(int distance, xy &other)
   if ((lx < d2) && (ly < d2))
     return 1;
 
-  return ((lx * lx) + (ly * ly)) < (distance * distance);
+#define SQ(c) (long(c) * (c))
+  return (SQ(lx) + SQ(ly)) < SQ(distance);
+#undef SQ
 }
 
 

+ 3 - 0
GD2.h

@@ -415,6 +415,9 @@ public:
   void draw(byte offset = 0);
   int onscreen(void);
   class xy operator+=(class xy &other);
+  class xy operator-=(class xy &other);
+  long operator*(class xy &other);
+  class xy operator*=(int);
   int nearer_than(int distance, xy &other);
 };