aboutsummaryrefslogtreecommitdiff
path: root/src/vector.h
blob: 55c7663e0be6840a6f9e24cf73719a368b262851 (plain)
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
#include <cmath>

inline double sq(double x) noexcept {
	return x * x;
}

struct Vector {
	double data[2];

	double comp(int x, int y) const {
		return x*data[0] + y*data[1];
	}

	double norm() const {
		return std::sqrt(sq(data[0]) + sq(data[1]));
	}

	double& operator[](std::size_t i) {
		return data[i];
	}

	double operator[](std::size_t i) const {
		return data[i];
	}

	Vector operator*(double scalar) const {
		return Vector{
			data[0] * scalar,
			data[1] * scalar
		};
	}

	Vector& operator+=(const Vector& rhs) {
		data[0] += rhs[0];
		data[1] += rhs[1];
		return *this;
	}
};

Vector operator*(double scalar, const Vector& vec) {
	return Vector{
		vec[0] * scalar,
		vec[1] * scalar
	};
}