aboutsummaryrefslogtreecommitdiff
path: root/geometry.cpp
blob: 1b514f0887bd63e08c6f0a8c56e1aecbd7824b1b (plain)
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
#include "geometry.h"

#include <cmath>

static const float PI = 3.1415f;

RectF::RectF(float x, float y, float width, float height)
    : m_x(x)
    , m_y(y)
    , m_width(width)
    , m_height(height)
{}

RectF::RectF(const RectF &other)
    : m_x(other.m_x)
    , m_y(other.m_y)
    , m_width(other.m_width)
    , m_height(other.m_height)
{}

RectF::RectF(const vec2 &p1, const vec2 &p2)
    : m_x(fmin(p1.x, p2.x))
    , m_y(fmin(p1.y, p2.y))
    , m_width(fabs(p1.x - p2.x))
    , m_height(fabs(p1.y - p2.y))
{}

bool RectF::contains(float x, float y) const
{
    return (x > m_x && x < m_x + m_width)
        && (y > m_y && y < m_y + m_height);
}

bool RectF::contains(const vec2 &p) const
{
    return contains(p.x, p.y);
}

bool RectF::intersects(const RectF &other) const
{
    // TODO
    return false;
}

Geometry::Geometry()
{}

int Geometry::vertexCount() const
{
    return 0;
}

vec2 *Geometry::vertexDataAsPoint2D()
{
    return nullptr;
}

int calculateCircleVertexCount(float diameter)
{
    // 10 * sqrt(r) \approx 2*pi / acos(1 - 1 / (4*r))
    return int(10.0 * sqrt(diameter / 2));
}

void updateCircleGeometry(Geometry *geometry, float diameter, float cx, float cy)
{
    int vertexCount = geometry->vertexCount();

    float theta = 2 * PI / float(vertexCount);
    float c = cosf(theta);
    float s = sinf(theta);
    float x = diameter / 2;
    float y = 0;

    vec2 *vertexData = geometry->vertexDataAsPoint2D();
    for (int i = 0; i < vertexCount; i++) {
        vertexData[i].set(x + cx, y + cy);

        float t = x;
        x = c*x - s*y;
        y = s*t + c*y;
    }
}

void updateRectGeometry(Geometry *geometry, float x, float y, float w, float h)
{
    vec2 *vertexData = geometry->vertexDataAsPoint2D();

    vertexData[0].set(x, y);
    vertexData[1].set(x + w, y);
    vertexData[2].set(x + w, y + h);
    vertexData[3].set(x, y + h);
}

void updateCrossHairGeometry(Geometry *geometry,
                             float x,
                             float y,
                             float thickness,
                             float length)
{
    vec2 *vertexData = geometry->vertexDataAsPoint2D();
    if (geometry->vertexCount() != 12) {
        return;
    }

    vertexData[ 0].set(x + thickness, y - thickness);
    vertexData[ 1].set(x + length,    y - thickness);
    vertexData[ 2].set(x + length,    y + thickness);
    vertexData[ 3].set(x + thickness, y + thickness);
    vertexData[ 4].set(x + thickness, y + length);
    vertexData[ 5].set(x - thickness, y + length);
    vertexData[ 6].set(x - thickness, y + thickness);
    vertexData[ 7].set(x - length,    y + thickness);
    vertexData[ 8].set(x - length,    y - thickness);
    vertexData[ 9].set(x - thickness, y - thickness);
    vertexData[10].set(x - thickness, y - length);
    vertexData[11].set(x + thickness, y - length);
}