view 2dvector.mm @ 15:d5758530a039 tip

oF0.84 Retina, and iPhone support
author Robert Tubb <rt300@eecs.qmul.ac.uk>
date Tue, 12 May 2015 15:48:52 +0100
parents c667dfe12d47
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"
#include <iostream>

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*(double scalar){ // if multiplying two vectors - elementwise
    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::unitDir(){
    TwoVector unit;
    double theNorm;
    theNorm = this->norm();
    
    unit.setCoord(x/theNorm, y/theNorm);
    return unit;
}

double TwoVector::distanceTo(TwoVector otherPoint){
    TwoVector diff;
    diff.setCoord(otherPoint.x - x, otherPoint.y - y);
    return diff.norm();
}