-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvec2.h
More file actions
37 lines (31 loc) · 993 Bytes
/
vec2.h
File metadata and controls
37 lines (31 loc) · 993 Bytes
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
#pragma once
#include "vec.h"
// 2D vector with named x/y accessors.
//
// Accessors are functions rather than pointer members so that the type keeps
// clean value semantics (copying/moving a vec2 does not leave dangling
// references into another object's storage).
template <class T>
class vec2 : public Vector<T, 2>
{
using Base = Vector<T, 2>;
public:
vec2() = default;
vec2(T x, T y)
{
Base::a[0] = x;
Base::a[1] = y;
}
// Allow a base-class result (e.g. from operator+) to be used as a vec2.
vec2(const Base& v) : Base(v) {}
T& x() { return Base::a[0]; }
T& y() { return Base::a[1]; }
const T& x() const { return Base::a[0]; }
const T& y() const { return Base::a[1]; }
// 2D cross product: returns the scalar z-component of the 3D cross product,
// i.e. the signed area of the parallelogram spanned by the two vectors.
T cross(const vec2<T>& v) const
{
return x() * v.y() - y() * v.x();
}
};