view 2dvector.mm @ 49:178642d134a7 tip

xtra files
author Robert Tubb <rt300@eecs.qmul.ac.uk>
date Wed, 01 May 2013 17:34:33 +0100
parents 43df75088d85
children
line wrap: on
line source
/*
 *  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();
}