summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/geom2d.h
blob: b52d4176382017065e58071da1ecf456d22a6417 (plain) (blame)
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
#ifndef GEOM2D_H
#define GEOM2D_H

namespace geom {

struct vector
{
    double x;
    double y;

    vector(double _x = 0.0, double _y = 0.0)
        : x(_x), y(_y) {}

    const vector& operator+=(const vector &v);
    vector operator+(const vector &v) const;
    vector operator-() const;
    const vector& operator-=(const vector &v);
    vector operator-(const vector &v) const;
};

vector operator*(double t, const vector &v);

struct form
{
    double a;
    double b;

    form(double _a = 0.0, double _b = 0.0)
        : a(_a), b(_b) {}

    double operator()(const vector &v) const;
};

// A ray in two-dimensional space given by starting point
// and direction vector.
// The points of R are start + t*dir.
struct grid;
struct ray
{
    vector start;
    vector dir;

    ray() {}
    ray(double x0, double y0, double xd, double yd)
        : start(x0, y0), dir(xd, yd) {}

    vector shoot(double t) const;
    void advance(double t);
    bool to_grid(const grid& g, bool half);
    bool to_next_cell(const grid& g);
};

// A line in two-dimensional space as the preimage of a number
// under a linear form. L = form^{-1}(val).
struct line
{
    form f;
    double val;

    line() {}
    line(double a, double b, double v)
        : f(a,b), val(v) {}
};

// A sequence of evenly spaced parallel lines, like the
// horizontal or vertical lines in a grid.
// Lines are f^{-1}(offset + k*dist) for integers k.
struct lineseq
{
    form f;
    double offset;
    double dist;

    lineseq() {}
    lineseq(double a, double b, double o, double d)
        : f(a,b), offset(o), dist(d) {}

    double index(const vector &v) const;
};

struct grid
{
    lineseq ls1;
    lineseq ls2;

    grid(lineseq l1, lineseq l2) : ls1(l1), ls2(l2) {}
};

double intersect(const ray &r, const line &l);
double nextintersect(const ray &r, const lineseq &ls);
bool parallel(const vector& v, const form &f);
vector reflect(const vector& v, const form &f);

vector degree_to_vector(const double d);
double degrees(const vector &v);
}

#endif