-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLinear Algebra.h
More file actions
56 lines (41 loc) · 1.22 KB
/
Copy pathLinear Algebra.h
File metadata and controls
56 lines (41 loc) · 1.22 KB
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
namespace Infinity
{
namespace LinearAlgebra
{
class Vector
{
typedef long double Real;
public:
Real x;
Real y;
Vector(Real x, Real y) : x(x), y(y)
{}
Real length() const
{ return sqrtl(x * x + y * y); }
Vector unit() const
{ return Vector(x / length(), y / length()); }
Vector transpose() const
{ return Vector(y, x); }
Vector left() const
{ return Vector(-y, x); }
Vector right() const
{ return Vector(y, -x); }
Vector operator -() const
{ return Vector(-x, -y); }
Vector operator +(const Vector &v) const
{ return Vector(x + v.x, y + v.y); }
Vector operator -(const Vector &v) const
{ return *this + -v; }
Vector operator *(Real r) const
{ return Vector(x * r, y * r); }
Vector operator /(Real r) const
{ assert(r != 0); return *this * (1 / r); }
Real dot(const Vector &v) const
{ return x * v.x + y * v.y; }
Real cross(const Vector &v) const
{ return x * v.y - y * v.x; }
friend ostream &operator <<(ostream &os, const Vector& v)
{ return os << v.x << " " << v.y; }
};
} // namespace Infinity::LinearAlgebra
} // namespace Infinity