-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgeom.h
More file actions
73 lines (61 loc) · 1.38 KB
/
geom.h
File metadata and controls
73 lines (61 loc) · 1.38 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
62
63
64
65
66
67
68
69
70
71
72
#pragma once
#include <limits>
#include <cmath>
struct vec
{
float x, y;
vec() {}
vec(float a, float b) : x(a), y(b) {}
vec(float a) : x(a), y(a) {}
vec(float a, float b, float c) : x(a), y(b) {}
vec &sub(const vec &v)
{
x -= v.x;
y -= v.y;
return *this;
}
vec &add(const vec &v)
{
x += v.x;
y += v.y;
return *this;
}
vec &mul(const float &v)
{
x *= v;
y *= v;
return *this;
}
vec &div(const float &v)
{
x /= v;
y /= v;
return *this;
}
float squaredlen() const
{
return x*x + y*y;
}
float squaredist(const vec &v) const
{
return vec(*this).sub(v).squaredlen();
}
static void line(const vec &p, const vec &q, float &a, float &b, float &c)
{
// Line AB represented as a*x + b*y = c
a = p.y - q.y;
b = q.x - p.x;
c = (p.x - q.x)*p.y + (q.y - p.y)*p.x;
}
static float lineDist(float a, float b, float c, const vec &p)
{
if (a + b == 0.0f) return std::numeric_limits<float>::max();;
return fabs(a * p.x + b * p.y + c) / sqrtf(a * a + b * b);
}
static float lineDist(const vec &la, const vec &lb, const vec &p)
{
float a, b, c;
line(la, lb, a, b, c);
return lineDist(a, b, c, p);
}
};