forked from Alan502/fast-mingle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPoint.h
68 lines (54 loc) · 1.83 KB
/
Point.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
/*
* A point with x-y coordinates.
*/
#ifndef POINT_H_
#define POINT_H_
#include <cmath>
struct Point {
float x, y;
Point operator-(Point &p);
Point operator+(Point &p);
Point operator*(double k);
bool operator==(const Point &other) const;
bool operator<(const Point &other) const;
static Point getUnitVector(Point vector) {
double length = getLengthOfVector(vector);
return {vector.x / (float)length, vector.y / (float)length};
}
static double getDistanceBetweenPoints(Point point1, Point point2) {
double diffX = point1.x - point2.x;
double diffY = point1.y - point2.y;
return sqrt(diffX * diffX + diffY * diffY);
}
static double getDotProductOfVectors(Point point1, Point point2) {
return point1.x * point2.x + point1.y * point2.y;
}
static Point getPerPendicularVector(Point vector) {
return {-vector.y, vector.x};
}
static double getLengthOfVector(Point vector) {
return sqrt(vector.x * vector.x + vector.y * vector.y);
}
/**
* Discouraged, acos() function is slow.
*/
static double getAngleBetweenVectors(Point vector1, Point vector2) {
double dotProd = Point::getDotProductOfVectors(vector1, vector2);
double lengthProduct =
Point::getLengthOfVector(vector1) * Point::getLengthOfVector(vector2);
return acos(dotProd / lengthProduct);
}
static double getCosineOfAngleBetweenVectors(Point vector1, Point vector2) {
double dotProd = Point::getDotProductOfVectors(vector1, vector2);
double lengthProduct =
Point::getLengthOfVector(vector1) * Point::getLengthOfVector(vector2);
return dotProd / lengthProduct;
}
static double getSlopeOfPoints(Point point1, Point point2) {
return (point2.y - point1.y) / (point1.x - point2.x);
}
};
struct PointHasher {
std::size_t operator()(const Point &p) const;
};
#endif /* POINT_H_ */