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