-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoordinate.cpp
executable file
·63 lines (50 loc) · 1.54 KB
/
coordinate.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "coordinate.h"
using namespace std;
Coordinate::Coordinate() : Matrix(2, 1){
this->index(1) = this->index(2) = 0;
}
Coordinate::Coordinate(double x, double y) : Matrix(2, 1){
this->index(1) = x;
this->index(2) = y;
}
Coordinate::Coordinate(Coordinate start, double distance, double theta): Matrix(2, 1){
this->index(1) = start(1) + distance * cos (theta);
this->index(2) = start(2) + distance * sin (theta);
}
Coordinate::Coordinate(const Matrix &mat) : Matrix(2, 1){
if ((mat.getRows() == this->getRows()) && (mat.getColumns() == this->getColumns())){
*this = mat;
} else {
throw range_error("Cannot initialise coordinate with wrong dimension");
}
}
const double & Coordinate::index(int in) const{
return Matrix::index(in, 1);
}
const double & Coordinate::operator()(int in) const{
return this->index(in);
}
double & Coordinate::index(int in){
return Matrix::index(in,1);
}
double & Coordinate::operator()(int in){
return this->index(in);
}
Coordinate Coordinate::getNormal() const {
return Coordinate(this->index(2),this->index(1));
}
double Coordinate::operator*(const Coordinate &v) const{
if (v.getRows() == this->getRows()){
double result(0);
for (int row = 1; row <= v.getRows(); row++){
result += v(row)*this->index(row);
}
return result;
} else {
throw invalid_argument("Scalar multiplication undefined for vectors of dissimilar dimensions");
}
}
ostream & operator<<(ostream &os, Coordinate v){
os << "(" << v(1) << "," << v(2) << ")";
return os;
}