forked from carzaniga/flying-balls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvec2d.h
142 lines (110 loc) · 3.24 KB
/
vec2d.h
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#ifndef VEC2D_H_INCLUDED
#define VEC2D_H_INCLUDED
#include "cairo.h"
#include "color.h"
#include <cmath>
#include <iostream>
#include <ostream>
class vec2d {
public:
double x;
double y;
vec2d& operator=(const vec2d& other) {
x = other.x;
y = other.y;
return *this;
}
vec2d& operator+=(const vec2d& other) {
x += other.x;
y += other.y;
return *this;
}
vec2d& operator-=(const vec2d& other) {
x -= other.x;
y -= other.y;
return *this;
}
vec2d& operator*=(double l) {
x *= l;
y *= l;
return *this;
}
vec2d operator+(const vec2d& other) const {
return vec2d{x + other.x, y + other.y};
}
vec2d operator-(const vec2d& other) const {
return vec2d{x - other.x, y - other.y};
}
vec2d operator-() const {
return vec2d{-x, -y};
}
vec2d operator*(double l) const {
return vec2d{x * l, y * l};
}
vec2d operator/(double a) const {
return vec2d{x / a, y / a};
}
bool operator==(vec2d& o) const {
return x == o.x && y == o.y;
}
vec2d& rotate(double angle) {
double sin_a = sin(angle);
double cos_a = cos(angle);
double x1 = x;
double y1 = y;
x = cos_a * x1 - sin_a * y1;
y = sin_a * x1 + cos_a * y1;
return *this;
}
vec2d orthogonal() {
return {-y, x};
}
void draw(cairo_t* cr, vec2d p, color_t color) const {
double arrow_lenght_ = 10 * vec2d::norm(*this);
double arrow_degrees_ = .5;
// see
// http://kapo-cpp.blogspot.com/2008/10/drawing-arrows-with-cairo.html
double angle = atan2(y, x) + M_PI;
vec2d end = p + (*this) * 30;
double head_length = fmin(10, arrow_lenght_);
double x1 = end.x + head_length * cos(angle - arrow_degrees_);
double y1 = end.y + head_length * sin(angle - arrow_degrees_);
double x2 = end.x + head_length * cos(angle + arrow_degrees_);
double y2 = end.y + head_length * sin(angle + arrow_degrees_);
cairo_set_source_rgb(cr, color.red, color.green, color.blue);
cairo_move_to(cr, p.x, p.y);
cairo_line_to(cr, end.x, end.y);
cairo_stroke(cr);
cairo_move_to(cr, end.x, end.y);
cairo_line_to(cr, x1, y1);
cairo_stroke(cr);
cairo_move_to(cr, end.x, end.y);
cairo_line_to(cr, x2, y2);
cairo_stroke(cr);
}
static double dot(const vec2d& a, const vec2d& b) {
return a.x * b.x + a.y * b.y;
}
static double cross(const vec2d& a, const vec2d& b) {
return a.x * b.y - a.y * b.x;
}
static vec2d cross(const double omega, const vec2d& v) {
return {-omega * v.y, omega * v.x};
}
static double norm2(const vec2d& v) {
return vec2d::dot(v, v);
}
static double norm(const vec2d& v) {
return std::sqrt(dot(v, v));
}
static vec2d normalize(const vec2d& v) {
return v / norm(v);
}
};
static inline std::ostream& operator<<(std::ostream& os, vec2d& p) {
return os << '(' << p.x << ", " << p.y << ')';
}
static inline vec2d operator*(double l, const vec2d& v) {
return vec2d{v.x * l, v.y * l};
}
#endif