annotate 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
rev   line source
rt300@0 1 /*
rt300@0 2 * 2dvector.cpp
rt300@0 3 * simplespring
rt300@0 4 *
rt300@0 5 * Created by Robert Tubb on 01/06/2011.
rt300@0 6 * Copyright 2011 __MyCompanyName__. All rights reserved.
rt300@0 7 *
rt300@0 8 */
rt300@0 9
rt300@0 10 #include "2dvector.h"
rt300@0 11 #include <iostream>
rt300@0 12
rt300@0 13 TwoVector::TwoVector(){
rt300@0 14 x = 0.0;
rt300@0 15 y = 0.0;
rt300@0 16 //cout << "def constr set vector to zeros" << endl;
rt300@0 17 }
rt300@0 18
rt300@0 19 TwoVector::TwoVector(double ax, double ay){
rt300@0 20 x = ax;
rt300@0 21 y = ay;
rt300@0 22 //cout << "spec constr set vector to " << ax << "," << ay << endl;
rt300@0 23 }
rt300@0 24
rt300@0 25 double TwoVector::norm(){
rt300@0 26 double norm;
rt300@0 27 norm = sqrt(x * x + y * y);
rt300@0 28 return norm;
rt300@0 29
rt300@0 30 }
rt300@0 31
rt300@0 32 void TwoVector::setCoord(double ax, double ay){
rt300@0 33 x = ax;
rt300@0 34 y = ay;
rt300@0 35
rt300@0 36 }
rt300@0 37
rt300@0 38 TwoVector TwoVector::minus(TwoVector otherPoint){
rt300@0 39 TwoVector diff;
rt300@0 40 diff.setCoord(x - otherPoint.x, y - otherPoint.y);
rt300@0 41 return diff;
rt300@0 42 }
rt300@0 43
rt300@0 44 TwoVector TwoVector::operator-(TwoVector otherPoint){
rt300@0 45 TwoVector diff;
rt300@0 46 diff.setCoord(x - otherPoint.x, y - otherPoint.y);
rt300@0 47 return diff;
rt300@0 48 }
rt300@0 49
rt300@0 50 TwoVector TwoVector::operator*(TwoVector otherPoint){ // if multiplying two vectors - elementwise
rt300@0 51 TwoVector diff;
rt300@0 52 diff.setCoord(x * otherPoint.x, y * otherPoint.y);
rt300@0 53 return diff;
rt300@0 54 }
rt300@0 55
rt300@0 56 TwoVector TwoVector::operator*(double scalar){ // if multiplying two vectors - elementwise
rt300@0 57 TwoVector diff;
rt300@0 58 diff.setCoord(x * scalar, y * scalar);
rt300@0 59 return diff;
rt300@0 60 }
rt300@0 61
rt300@0 62 TwoVector TwoVector::operator+(TwoVector otherPoint){
rt300@0 63 TwoVector diff;
rt300@0 64 diff.setCoord(otherPoint.x + x, otherPoint.y + y);
rt300@0 65 return diff;
rt300@0 66 }
rt300@0 67
rt300@0 68 TwoVector TwoVector::unitDir(){
rt300@0 69 TwoVector unit;
rt300@0 70 double theNorm;
rt300@0 71 theNorm = this->norm();
rt300@0 72
rt300@0 73 unit.setCoord(x/theNorm, y/theNorm);
rt300@0 74 return unit;
rt300@0 75 }
rt300@0 76
rt300@0 77 double TwoVector::distanceTo(TwoVector otherPoint){
rt300@0 78 TwoVector diff;
rt300@0 79 diff.setCoord(otherPoint.x - x, otherPoint.y - y);
rt300@0 80 return diff.norm();
rt300@0 81 }
rt300@0 82