-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgebra2d.cpp
More file actions
62 lines (47 loc) · 1.36 KB
/
algebra2d.cpp
File metadata and controls
62 lines (47 loc) · 1.36 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
57
58
59
60
61
#include "algebra2d.h"
std::ostream& operator<<(std::ostream& os, const vec2d& vec)
{
os <<'('<<vec.v[0]<<','<<vec.v[1]<<')';
return os;
}
double dist2(vec2d v1, vec2d v2){
return ((v1-v2).pow(2)).sum();
}
double dist(vec2d v1, vec2d v2){
return sqrt((v1-v2).pow(2).sum());
}
double angle(vec2d v1, vec2d v2){// angle from vector v1 to v2
return atan((v2[1]-v1[1])/(v2[0]-v1[0]));
}
double cosangle(vec2d v1, vec2d v2){
return (v1*v2)/(v1.modulus()*v2.modulus());
}
double sinangle(vec2d v1, vec2d v2){
// moving from v1 to v2
return (v1[0]*v2[1]-v2[0]*v1[1])/(v1.modulus()*v2.modulus());
}
double crossprod(vec2d v1, vec2d v2){
return (v1[0]*v2[1]-v2[0]*v1[1]);
}
vec2d dist_linepoint(vec2d line, vec2d point){
vec2d distvec;
// distsance between a line (given by a vector) and a point.
// The result is two numbers: the distance perpendicular to the line, and the distance along the line.
distvec[0] = point.modulus()*sinangle(line,point); // positive when point is at a positive angle from line
distvec[1] = point.modulus()*cosangle(line,point);
return distvec;
}
// }
vec2d vnormal(vec2d v1){
// Rotation +90C
vec2d vn;
vn[0] = -v1[1];
vn[1] = v1[0];
return vn;
}
vec2d rotate(vec2d v1, double angle){
vec2d vecrot;
vecrot[0] = v1[0]*cos(angle) - v1[1]*sin(angle);
vecrot[1] = v1[0]*sin(angle) + v1[1]*cos(angle);
return vecrot;
}