vector.cc Testcase
authorMarkus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:49:12 +0200
changeset 43 cf8c1b5127b2
parent 42 83b8151b966d
child 44 bb6586b1c547
vector.cc Testcase -> Eine Referenz ist günstiger bei Vektoren -> move ist ohne Referenz flexibler -> Es werden weniger Objekte erzeugt und dementsprechend auch wieder zerstört. committer: Markus Bröker <mbroeker@largo.homelinux.org>
Makefile
pmc/cube.cc
pmc/include/cube.h
pmc/include/drawable.h
pmc/include/rectangle.h
pmc/include/vector.h
pmc/rectangle.cc
pmc/vector.cc
vector.cc
--- a/Makefile
+++ b/Makefile
@@ -46,7 +46,8 @@
 	min2time \
 	recursive_compiler \
 	getpwnam_error \
-	xmlparser
+	xmlparser \
+	vector
 
 .SUFFIXES: .c .cc .asm
 
@@ -250,6 +251,10 @@
 	@echo Linking $< ...
 	@$(CC) -o $@ $< -lexpat
 
+vector: vector.o
+	@echo Linking $< ...
+	@$(CPP) -o $@ $<
+
 .PHONY: clean uninstall
 
 clean:
--- a/pmc/cube.cc
+++ b/pmc/cube.cc
@@ -22,10 +22,11 @@
     height = 25;
 }
 
-Cube::Cube (Surface * s, Vector p1, Vector p2, Vector p3, Vector p4, int h)
+Cube::Cube (Surface * s, Vector& p1, Vector& p2, Vector& p3, Vector& p4, int h)
 {
     height = h;
     surface = s;
+    Vector location(h/2, h/2);
 
     P[0] = new Vector (p1);
     P[1] = new Vector (p2);
@@ -34,13 +35,13 @@
 
     for (int i = 0; i < 4; i++) {
         P[4 + i] = new Vector (*P[i]);
-        *P[4 + i] = *P[i] + Vector (h / 2, h / 2);
+        *P[4 + i] = *P[i] + location;
     }
 }
 
 Cube::~Cube ()
 {
-    for (int i = 0; i < 6; i++) {
+    for (int i = 0; i < 8; i++) {
 #ifdef DEBUG
         fprintf (stderr, "Removing P[%d]: ", i);
 #endif
--- a/pmc/include/cube.h
+++ b/pmc/include/cube.h
@@ -16,7 +16,7 @@
     int height;
 
   public:
-    Cube (Surface *, Vector, Vector, Vector, Vector, int);
+    Cube (Surface *, Vector&, Vector&, Vector&, Vector&, int);
     Cube (Surface *);
 
     virtual ~ Cube ();
--- a/pmc/include/drawable.h
+++ b/pmc/include/drawable.h
@@ -16,7 +16,7 @@
 
   public:
     virtual ~ Drawable () {};
-    virtual void move (Vector location) = 0;
+    virtual void move (Vector) = 0;
     virtual void show () = 0;
 };
 #endif
--- a/pmc/include/rectangle.h
+++ b/pmc/include/rectangle.h
@@ -15,7 +15,7 @@
     Vector * P[4];
 
   public:
-    Rectangle (Surface *, Vector p1, Vector p2, Vector p3, Vector p4);
+    Rectangle (Surface *, Vector& p1, Vector& p2, Vector& p3, Vector& p4);
     Rectangle (Surface *);
     virtual ~ Rectangle ();
     void move (Vector);
--- a/pmc/include/vector.h
+++ b/pmc/include/vector.h
@@ -18,12 +18,12 @@
     int X ();
     int Y ();
 
-    Vector operator+ (Vector);
-    Vector operator- (Vector);
+    Vector operator+ (Vector&);
+    Vector operator- (Vector&);
     double abs ();
 
     void vector ();
-    double angle (Vector);
+    double angle (Vector&);
     void setMode (Mode m = RAD);
 
   private:
--- a/pmc/rectangle.cc
+++ b/pmc/rectangle.cc
@@ -18,7 +18,7 @@
     surface = s;
 }
 
-Rectangle::Rectangle (Surface * s, Vector p1, Vector p2, Vector p3, Vector p4)
+Rectangle::Rectangle (Surface * s, Vector& p1, Vector& p2, Vector& p3, Vector& p4)
 {
     P[0] = new Vector (p1);
     P[1] = new Vector (p2);
--- a/pmc/vector.cc
+++ b/pmc/vector.cc
@@ -37,12 +37,12 @@
     return y;
 }
 
-Vector Vector::operator+ (Vector a)
+Vector Vector::operator+ (Vector& a)
 {
     return Vector (x + a.X (), y + a.Y ());
 }
 
-Vector Vector::operator- (Vector a)
+Vector Vector::operator- (Vector& a)
 {
     return Vector (x - a.X (), y - a.Y ());
 }
@@ -57,7 +57,7 @@
     fprintf (stderr, "(%.3d, %.3d)", x, y);
 }
 
-double Vector::angle (Vector v)
+double Vector::angle (Vector& v)
 {
     if (mode == DEG)
         return ((180.0 / M_PI) * std::acos ((v.X () * X () + v.Y () * Y ()) / (abs () * v.abs ())));
new file mode 100644
--- /dev/null
+++ b/vector.cc
@@ -0,0 +1,105 @@
+/**
+ * DEREFERNCING CLASSES
+ */
+
+#include <cstdio>
+#include <cstdlib>
+#include <cmath>
+#include <ctime>
+
+#define GETRANDOM(max) (1+(int)((float)max*rand()/RAND_MAX+1.0))
+#define MAX 6
+
+namespace algebra {
+    class Vector {
+      private:
+        int x;
+        int y;
+
+      public:
+        Vector (int xx = 0, int yy = 0);
+       ~Vector ();
+
+        int X () { return x; }
+        int Y () { return y; }
+
+        Vector operator+ (Vector&);
+        Vector operator- (Vector&);
+
+        void vector ();
+        double abs ();
+    };
+
+    Vector::Vector (int xx, int yy) {
+        x = xx;
+        y = yy;
+    }
+
+    Vector::~Vector () {
+        fprintf (stderr, "Removing Vector (%.3d, %.3d)\n", x, y);
+    }
+
+    Vector Vector::operator+ (Vector& v) {
+        return Vector (x + v.X (), y + v.Y ());
+    }
+
+    Vector Vector::operator- (Vector& v) {
+        return Vector (x - v.X (), y - v.Y ());
+    }
+
+    double Vector::abs () {
+        return sqrt (x * x + y * y);
+    }
+
+    void Vector::vector () {
+        printf ("(%.3d, %.3d)", x, y);
+    }
+}
+
+using namespace algebra;
+
+int main (int argc, char **argv)
+{
+    Vector *v[MAX];
+    Vector result;
+
+    srand (time (NULL));
+
+    if ((MAX % 2) != 0) {
+        printf ("MAX %% 2 != 0\n");
+        return EXIT_FAILURE;
+    }
+
+    for (int i = 0; i < MAX; i++) {
+        v[i] = new Vector (GETRANDOM (100), GETRANDOM (100));
+    }
+
+    for (int i = 0; i < MAX; i += 2) {
+        v[i + 1]->vector ();
+        printf ("+");
+        v[i]->vector ();
+        printf ("=");
+        result = *v[i + 1] + *v[i];
+        result.vector ();
+        printf (" = %3.2f\n", result.abs ());
+    }
+
+    printf ("\n");
+
+    for (int i = 0; i < MAX; i += 2) {
+        v[i + 1]->vector ();
+        printf ("-");
+        v[i]->vector ();
+        printf ("=");
+        result = *v[i + 1] - *v[i];
+        result.vector ();
+        printf (" = %3.2f\n", result.abs ());
+    }
+
+    printf ("\nCLEANUP\n");
+
+    for (int i = 0; i < MAX; i++)
+        delete v[i];
+
+    return EXIT_SUCCESS;
+}