rt300@0: /* rt300@0: * 2dvector.cpp rt300@0: * simplespring rt300@0: * rt300@0: * Created by Robert Tubb on 01/06/2011. rt300@0: * Copyright 2011 __MyCompanyName__. All rights reserved. rt300@0: * rt300@0: */ rt300@0: rt300@0: #include "2dvector.h" rt300@0: #include rt300@0: rt300@0: TwoVector::TwoVector(){ rt300@0: x = 0.0; rt300@0: y = 0.0; rt300@0: //cout << "def constr set vector to zeros" << endl; rt300@0: } rt300@0: rt300@0: TwoVector::TwoVector(double ax, double ay){ rt300@0: x = ax; rt300@0: y = ay; rt300@0: //cout << "spec constr set vector to " << ax << "," << ay << endl; rt300@0: } rt300@0: rt300@0: double TwoVector::norm(){ rt300@0: double norm; rt300@0: norm = sqrt(x * x + y * y); rt300@0: return norm; rt300@0: rt300@0: } rt300@0: rt300@0: void TwoVector::setCoord(double ax, double ay){ rt300@0: x = ax; rt300@0: y = ay; rt300@0: rt300@0: } rt300@0: rt300@0: TwoVector TwoVector::minus(TwoVector otherPoint){ rt300@0: TwoVector diff; rt300@0: diff.setCoord(x - otherPoint.x, y - otherPoint.y); rt300@0: return diff; rt300@0: } rt300@0: rt300@0: TwoVector TwoVector::operator-(TwoVector otherPoint){ rt300@0: TwoVector diff; rt300@0: diff.setCoord(x - otherPoint.x, y - otherPoint.y); rt300@0: return diff; rt300@0: } rt300@0: rt300@0: TwoVector TwoVector::operator*(TwoVector otherPoint){ // if multiplying two vectors - elementwise rt300@0: TwoVector diff; rt300@0: diff.setCoord(x * otherPoint.x, y * otherPoint.y); rt300@0: return diff; rt300@0: } rt300@0: rt300@0: TwoVector TwoVector::operator*(double scalar){ // if multiplying two vectors - elementwise rt300@0: TwoVector diff; rt300@0: diff.setCoord(x * scalar, y * scalar); rt300@0: return diff; rt300@0: } rt300@0: rt300@0: TwoVector TwoVector::operator+(TwoVector otherPoint){ rt300@0: TwoVector diff; rt300@0: diff.setCoord(otherPoint.x + x, otherPoint.y + y); rt300@0: return diff; rt300@0: } rt300@0: rt300@0: TwoVector TwoVector::unitDir(){ rt300@0: TwoVector unit; rt300@0: double theNorm; rt300@0: theNorm = this->norm(); rt300@0: rt300@0: unit.setCoord(x/theNorm, y/theNorm); rt300@0: return unit; rt300@0: } rt300@0: rt300@0: double TwoVector::distanceTo(TwoVector otherPoint){ rt300@0: TwoVector diff; rt300@0: diff.setCoord(otherPoint.x - x, otherPoint.y - y); rt300@0: return diff.norm(); rt300@0: } rt300@0: