summaryrefslogtreecommitdiff
path: root/src/geometry/interpolate.cpp
blob: cb4165cf2ba32088e218c92d65a4b342ec76aa2c (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
#include <llmr/geometry/interpolate.hpp>

namespace llmr {

Anchors interpolate(const std::vector<Coordinate> &vertices, float spacing,
                    float minScale, int start) {
    float distance = 0.0f;
    float markedDistance = 0.0f;
    int added = start;

    Anchors points;

    auto end = vertices.end() - 1;
    int i = 0;
    for (auto it = vertices.begin(); it != end; it++, i++) {
        const Coordinate &a = *(it), b = *(it + 1);

        float segmentDist = util::dist<float>(a, b);
        float angle = util::angle_to(b, a);

        while (markedDistance + spacing < distance + segmentDist) {
            markedDistance += spacing;

            float t = (markedDistance - distance) / segmentDist,
                  x = util::interp(a.x, b.x, t), y = util::interp(a.y, b.y, t),
                  s = added % 8 == 0 ? minScale : added % 4 == 0
                                                      ? 2
                                                      : added % 2 == 0 ? 4 : 8;

            if (x >= 0 && x < 4096 && y >= 0 && y < 4096) {
                points.emplace_back(x, y, angle, s, i);
            }

            added++;
        }

        distance += segmentDist;
    }

    return points;
}
}