diff 2dvector.mm @ 0:a223551fdc1f

First commit - copy from tweakathlon.
author Robert Tubb <rt300@eecs.qmul.ac.uk>
date Fri, 10 Oct 2014 11:46:42 +0100
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/2dvector.mm	Fri Oct 10 11:46:42 2014 +0100
@@ -0,0 +1,80 @@
+/*
+ *  2dvector.cpp
+ *  simplespring
+ *
+ *  Created by Robert Tubb on 01/06/2011.
+ *  Copyright 2011 __MyCompanyName__. All rights reserved.
+ *
+ */
+
+#include "2dvector.h"
+
+
+TwoVector::TwoVector(){
+	x = 0.0;
+	y = 0.0;
+	//cout << "def constr set vector to zeros" << endl;
+}
+
+TwoVector::TwoVector(double ax, double ay){
+	x = ax;
+	y = ay;
+	//cout << "spec constr set vector to " << ax << "," << ay << endl;
+}
+
+double TwoVector::norm(){
+	double norm;
+	norm = sqrt(x * x + y * y);
+	return norm;
+	
+}
+
+void TwoVector::setCoord(double ax, double ay){
+	x = ax;
+	y = ay;
+
+}
+
+TwoVector TwoVector::minus(TwoVector otherPoint){
+    TwoVector diff;
+    diff.setCoord(x - otherPoint.x, y - otherPoint.y);
+    return diff;
+}
+
+TwoVector TwoVector::operator-(TwoVector otherPoint){
+    TwoVector diff;
+    diff.setCoord(x - otherPoint.x, y - otherPoint.y);
+    return diff;
+}
+
+TwoVector TwoVector::operator*(TwoVector otherPoint){ // if multiplying two vectors - elementwise
+    TwoVector diff;
+    diff.setCoord(x * otherPoint.x, y * otherPoint.y);
+    return diff;
+}
+
+TwoVector TwoVector::operator*(const double& scalar){
+    TwoVector diff;
+    diff.setCoord(x * scalar, y * scalar);
+    return diff;
+}
+
+TwoVector TwoVector::operator+(TwoVector otherPoint){
+    TwoVector diff;
+    diff.setCoord(otherPoint.x + x, otherPoint.y + y);
+    return diff;
+}
+    
+/*
+TwoVector TwoVector::operator=(TwoVector otherPoint){
+    TwoVector result;
+    result.setCoord(otherPoint.x, otherPoint.y);
+    return result;
+}
+*/
+double TwoVector::distanceTo(TwoVector otherPoint){
+    TwoVector diff;
+    diff.setCoord(otherPoint.x - x, otherPoint.y - y);
+    return diff.norm();
+}
+