aboutsummaryrefslogtreecommitdiffstats
path: root/geom.hpp
blob: e9a4512bfcb94f241c0221813854cdbc550f88e9 (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
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
143
144
145
146
147
148
149
150
151

#pragma once

#include <math.h>

struct vec {
    double x, y;

    vec(double xx, double yy) : x(xx), y(yy) {}

    double norm() const {
        return sqrt(x*x + y*y);
    }

    double angle() const {
        double xx = x / norm();
        double a = acos(x);
        return (y > 0 ? a : -a);
    }

    vec normalize() const {
        double n = norm();
        return vec(x / n, y / n);
    }

    bool is_nil() const {
        return x == 0 && y == 0;
    }

    vec operator+ (const vec& o) const {
        return vec(x + o.x, y + o.y);
    }
    vec operator- (const vec& o) const {
        return vec(x - o.x, y - o.y);
    }
    vec operator* (double m) const {
        return vec(m * x, m * y);
    }
    vec operator/ (double m) const {
        return vec(m / x, m / y);
    }

    static vec from_polar(double r, double theta) {
        return vec(r * cos(theta), r * sin(theta));
    }

    static vec dot(vec a, vec b) {      // dot product (produit scalaire)
        return a.x * b.x + a.y * b.y;
    }
    static vec cross(vec a, vec b) {    // cross product (d├ęterminant 2x2)
        return a.x * b.y - a.y * b.x;
    }
    static vec angle(vec a, vec b) {    // oriented angle between two vectors
        if (a.is_nil() || b.is_nil()) return 0;
        float cos = dot(a.normalize(), b.normalize());
        if (cos <= -1) return M_PI;
        float uangle = acos(cos);
        if (cross(a, b) > 0) {
            return uangle;
        } else {
            return -uangle;
        }
    }
};

struct line {
    // Line defined by ax + by + c = 0
    double a, b, c;

    line(double aa, double bb, double cc) : a(aa), b(bb), c(cc) {}

    bool on_line(vec p) const {
        return a * p.x + b * p.y + c == 0;
    }

    double dist(vec p) const {
        // calculate distance from p to the line
        // TODO
        return 1;
    }

    vec proj(vec p) const {
        // calculate orthogonal projection of point p on the line
        // TODO
        return vec(0, 0);
    }

    double angle() const {
        return vec(-b, a).angle();
    }

};

struct segment {
    vec a, b;

    segment(vec pa, vec pb) : a(pa), b(pb) {}

    bool on_segment(vec p) const {
        // TODO
        // does point intersect segment?
        return false;
    }

    double dist(vec p) const {
        // TODO
        return 1;
    }
};

struct circle {
    vec c;
    double r;

    circle(double x, double y, double rr) : c(x, y), r(rr) {}
    circle(vec cc, double rr) : c(cc), r(rr) {}

    bool on_circle(vec.p) const {
        return ((p - c).norm() == r);
    }

    double dist(vec p) const {
        // TODO
        return 1;
    }
};

struct circpoint {
    circle c;
    double theta;

    circpoint(circle cc, double th) : c(cc), theta(th) {}
    circpoint(vec cc, double rr, double th : c(cc, rr), theta(th) {}

    vec pos() const {
        return c.c + vec(c.r * cos(theta), c.r * sin(theta));
    }
};

struct circarc {
    circle c;
    double theta1, theta2;

    circarc(circle cc, double tha, double thb) : c(cc), theta1(tha), theta2(thb) {}

    double dist(vec p) const {
        // TODO
        return 1;
    }
};

/* vim: set ts=4 sw=4 tw=0 noet :*/