Alex Allen / vectr

Files at this revision

API Documentation at this revision

Comitter:
AlexAllen
Date:
Sat Oct 13 12:22:02 2012 +0000
Commit message:
A library for vectors

Changed in this revision

vectr.cpp Show annotated file Show diff for this revision Revisions of this file
vectr.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 55dbdc9614f8 vectr.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/vectr.cpp	Sat Oct 13 12:22:02 2012 +0000
@@ -0,0 +1,242 @@
+/* Copyright (c) 2011 Alex Allen, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
+ * and associated documentation files (the "Software"), to deal in the Software without restriction, 
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or 
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+ 
+#include "vectr.h"
+#include <cmath>
+#include <iostream>
+using namespace std;
+
+
+vectr::vectr() 
+{ 
+    x=y=z=0; 
+}
+
+vectr::vectr(double num) 
+{ 
+    x=y=z=num; 
+}
+
+vectr::vectr(double a, double b)
+{
+    x = a;
+    y = b;
+    z = 0;
+}
+
+vectr::vectr(double a, double b, double c)
+{
+    x = a;
+    y = b;
+    z = c;
+}
+
+void vectr::set(double a, double b, double c)
+{
+    x = a;
+    y = b;
+    z = c;
+}
+
+void vectr::setx(double a)
+{
+    x = a;
+}
+
+void vectr::sety(double b)
+{
+    y = b;
+}
+
+void vectr::setz(double c)
+{
+    z = c;
+}
+
+double vectr::getx()
+{
+    return x;
+}
+
+double vectr::gety()
+{
+    return y;
+}
+
+double vectr::getz()
+{
+    return z;
+}
+
+double vectr::mag()
+{
+    return sqrt(x*x + y*y + z*z);
+}
+
+double vectr::mag2()
+{
+    return x*x + y*y + z*z;
+}
+
+vectr vectr::unit()
+{
+    double size = sqrt(x*x+y*y+z*z);
+    vectr temp;
+
+    temp.x = x/size;
+    temp.y = y/size;
+    temp.z = z/size;
+
+    return temp;
+}
+
+vectr vectr::operator+(vectr ob2)
+{
+    vectr temp;
+
+    temp.x = x + ob2.x;
+    temp.y = y + ob2.y;
+    temp.z = z + ob2.z;
+
+    return temp;
+}
+
+vectr vectr::operator-(vectr ob2)
+{
+    vectr temp;
+
+    temp.x = x - ob2.x;
+    temp.y = y - ob2.y;
+    temp.z = z - ob2.z;
+
+    return temp;
+}
+
+double vectr::operator*(vectr ob2)
+{
+    return x*ob2.x + y*ob2.y + z*ob2.z;
+}
+
+vectr vectr::operator*(double num)
+{
+    vectr temp;
+
+    temp.x = x * num;
+    temp.y = y * num;
+    temp.z = z * num;
+
+    return temp;
+}
+
+vectr operator*(double num, vectr ob)
+{
+    vectr temp;
+
+    temp.x = ob.x * num;
+    temp.y = ob.y * num;
+    temp.z = ob.z * num;
+
+    return temp;
+}
+
+vectr vectr::operator%(vectr ob2)
+{
+    vectr temp;
+
+    temp.x = y * ob2.z - z * ob2.y;
+    temp.y = z * ob2.x - x * ob2.z;
+    temp.z = x * ob2.y - y * ob2.x;
+
+    return temp;
+}
+
+vectr vectr::operator/(double num)
+{
+    vectr temp;
+
+    temp.x = x / num;
+    temp.y = y / num;
+    temp.z = z / num;
+
+    return temp;
+}
+
+vectr operator/(double num, vectr ob)
+{
+    vectr temp;
+
+    temp.x = num / ob.x;
+    temp.y = num / ob.y;
+    temp.z = num / ob.z;
+
+    return temp;
+}
+
+double vectr::operator/(vectr ob2)
+{
+    ob2.x /= 1.0;
+    ob2.y /= 1.0;
+    ob2.z /= 1.0;
+
+    return x*ob2.x + y*ob2.y + z*ob2.z;
+}
+
+vectr vectr::operator+=(vectr ob2)
+{
+    x += ob2.x;
+    y += ob2.y;
+    z += ob2.z;
+
+    return *this;
+}
+
+vectr vectr::operator-=(vectr ob2)
+{
+    x -= ob2.x;
+    y -= ob2.y;
+    z -= ob2.z;
+
+    return *this;
+}
+
+vectr vectr::operator*=(double num)
+{
+    x *= num;
+    y *= num;
+    z *= num;
+
+    return *this;
+}
+
+vectr vectr::operator/=(double num)
+{
+    x /= num;
+    y /= num;
+    z /= num;
+
+    return *this;
+}
+
+ostream &operator<<(ostream &stream, vectr ob)
+{
+    stream << "(" << ob.x << ", ";
+    stream << ob.y << ", ";
+    stream << ob.z << ")";
+
+    return stream;
+}
\ No newline at end of file
diff -r 000000000000 -r 55dbdc9614f8 vectr.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/vectr.h	Sat Oct 13 12:22:02 2012 +0000
@@ -0,0 +1,70 @@
+/* Copyright (c) 2011 Alex Allen, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
+ * and associated documentation files (the "Software"), to deal in the Software without restriction, 
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or 
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+ 
+ #ifndef VECTR
+
+#include <iostream>
+using namespace std;
+
+class vectr
+{
+    double x,y,z;
+    
+    public:
+    
+    vectr(); // initialises vector as all 0s
+    vectr(double num); // initialises vector as all num
+    vectr(double a, double b); // x = a, y = b, z = 0
+    vectr(double a, double b, double c); // x = a, y = b, z = c
+    
+    void set(double a, double b, double c); // sets all values of the vector
+    void setx(double a);  // sets the x component
+    void sety(double b); // sets the y component
+    void setz(double c);  // sets the z component
+    
+    double getx(); // gets the x component
+    double gety(); // gets the y component
+    double getz(); // gets the z component
+    
+    double mag(); // returns magnitude of the vector
+    double mag2(); // returns magnitude squared of the vector
+
+    vectr unit(); // returns the unit vector
+    vectr operator+(vectr ob2); // output = this + ob2
+    vectr operator-(vectr ob2); // output = this - ob2
+    double operator*(vectr ob2); // output = dot product of this and ob2
+    vectr operator*(double num); // output = this * num
+    
+    friend vectr operator*(double num, vectr ob); // output = num * ob
+    vectr operator%(vectr ob2); // output = cross product of this and ob2
+    vectr operator/(double num); // output = this / num
+    friend vectr operator/(double num, vectr ob); // output = num * 1/ob
+    
+    // output = dot product of this and 1/ob
+    double operator/(vectr ob2);
+    vectr operator+=(vectr ob2);
+    vectr operator-=(vectr ob2);
+    vectr operator*=(double num);
+    vectr operator/=(double num);
+    
+    // output in the form "(x, y, z)"
+    friend ostream &operator<<(ostream &stream, vectr ob);
+};
+
+#define VECTR 1
+#endif
\ No newline at end of file