summaryrefslogtreecommitdiff
path: root/src/mbgl/layout/clip_lines.cpp
blob: 022c296e190f891786b1bf14e11ddb8f0d0bf897 (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
#include <mbgl/layout/clip_lines.hpp>

#include <cmath>

namespace mbgl {
namespace util {

GeometryCollection clipLines(const GeometryCollection &lines,
        const int16_t x1, const int16_t y1, const int16_t x2, const int16_t y2) {

    GeometryCollection clippedLines;

    for (auto& line : lines) {

        if (line.empty())
            continue;

        auto end = line.end() - 1;
        for (auto it = line.begin(); it != end; it++) {
            GeometryCoordinate p0 = *(it);
            GeometryCoordinate p1 = *(it + 1);

            if (p0.x < x1 && p1.x < x1) {
                continue;
            } else if (p0.x < x1) {
                p0 = {x1,
                      static_cast<int16_t>(
                          std::round(p0.y + (p1.y - p0.y) * (static_cast<float>(x1 - p0.x) / (p1.x - p0.x))))};
            } else if (p1.x < x1) {
                p1 = {x1,
                      static_cast<int16_t>(
                          std::round(p0.y + (p1.y - p0.y) * (static_cast<float>(x1 - p0.x) / (p1.x - p0.x))))};
            }

            if (p0.y < y1 && p1.y < y1) {
                continue;
            } else if (p0.y < y1) {
                p0 = {static_cast<int16_t>(
                          std::round(p0.x + (p1.x - p0.x) * (static_cast<float>(y1 - p0.y) / (p1.y - p0.y)))),
                      y1};
            } else if (p1.y < y1) {
                p1 = {static_cast<int16_t>(
                          std::round(p0.x + (p1.x - p0.x) * (static_cast<float>(y1 - p0.y) / (p1.y - p0.y)))),
                      y1};
            }

            if (p0.x >= x2 && p1.x >= x2) {
                continue;
            } else if (p0.x >= x2) {
                p0 = {x2,
                      static_cast<int16_t>(
                          std::round(p0.y + (p1.y - p0.y) * (static_cast<float>(x2 - p0.x) / (p1.x - p0.x))))};
            } else if (p1.x >= x2) {
                p1 = {x2,
                      static_cast<int16_t>(
                          std::round(p0.y + (p1.y - p0.y) * (static_cast<float>(x2 - p0.x) / (p1.x - p0.x))))};
            }

            if (p0.y >= y2 && p1.y >= y2) {
                continue;
            } else if (p0.y >= y2) {
                p0 = {static_cast<int16_t>(
                          std::round(p0.x + (p1.x - p0.x) * (static_cast<float>(y2 - p0.y) / (p1.y - p0.y)))),
                      y2};
            } else if (p1.y >= y2) {
                p1 = {static_cast<int16_t>(
                          std::round(p0.x + (p1.x - p0.x) * (static_cast<float>(y2 - p0.y) / (p1.y - p0.y)))),
                      y2};
            }

            if (clippedLines.empty() || (!clippedLines.back().empty() && !(p0 == clippedLines.back().back()))) {
                clippedLines.emplace_back();
                clippedLines.back().push_back(p0);
            }

            clippedLines.back().push_back(p1);
        }
    }

    return clippedLines;
}

} // end namespace util
} // end namespace mbgl