summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/2geom/src/2geom/parallelogram.cpp
blob: b477a012e649a9883ef00e4c7942fcdba396f92b (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * Authors:
 *   Thomas Holder
 *   Sergei Izmailov
 *
 * Copyright 2020 Authors
 *
 * SPDX-License-Identifier: LGPL-2.1 or MPL-1.1
 */

#include <2geom/basic-intersection.h>
#include <2geom/parallelogram.h>

#include <cassert>

namespace Geom {

namespace {
/// Return true if `p` is inside a unit rectangle
inline bool unit_rect_contains(Point const &p)
{
    return 0 <= p.x() && p.x() <= 1 && //
           0 <= p.y() && p.y() <= 1;
}

/// N'th corner of a unit rectangle
inline Point unit_rect_corner(unsigned i)
{
    assert(i < 4);
    unsigned const y = i >> 1;
    unsigned const x = (i & 1) ^ y;
    return Point(x, y);
}
} // namespace

Point Parallelogram::corner(unsigned i) const
{
    assert(i < 4);
    return unit_rect_corner(i) * m_affine;
}

Rect Parallelogram::bounds() const
{
    Rect rect(corner(0), corner(2));
    rect.expandTo(corner(1));
    rect.expandTo(corner(3));
    return rect;
}

bool Parallelogram::intersects(Parallelogram const &other) const
{
    if (m_affine.isSingular() || other.m_affine.isSingular()) {
        return false;
    }

    auto const affine1 = other.m_affine * m_affine.inverse();
    auto const affine2 = affine1.inverse();

    // case 1: any corner inside the other rectangle
    for (unsigned i = 0; i != 4; ++i) {
        auto const p = unit_rect_corner(i);
        if (unit_rect_contains(p * affine1) || //
            unit_rect_contains(p * affine2)) {
            return true;
        }
    }

    // case 2: any sides intersect (check diagonals)
    for (unsigned i = 0; i != 2; ++i) {
        auto const A = corner(i);
        auto const B = corner((i + 2) % 4);
        for (unsigned j = 0; j != 2; ++j) {
            auto const C = other.corner(j);
            auto const D = other.corner((j + 2) % 4);
            if (non_collinear_segments_intersect(A, B, C, D)) {
                return true;
            }
        }
    }

    return false;
}

bool Parallelogram::contains(Point const &p) const
{
    return !m_affine.isSingular() && //
           unit_rect_contains(p * m_affine.inverse());
}

bool Parallelogram::contains(Parallelogram const &other) const
{
    if (m_affine.isSingular()) {
        return false;
    }

    auto const inv = m_affine.inverse();

    for (unsigned i = 0; i != 4; ++i) {
        if (!unit_rect_contains(other.corner(i) * inv)) {
            return false;
        }
    }

    return true;
}

Coord Parallelogram::minExtent() const
{
    return std::min(m_affine.expansionX(), //
                    m_affine.expansionY());
}

Coord Parallelogram::maxExtent() const
{
    return std::max(m_affine.expansionX(), //
                    m_affine.expansionY());
}

bool Parallelogram::isSheared(Coord eps) const
{
    return !are_near(dot(m_affine.xAxis(), m_affine.yAxis()), //
                     0.0, eps);
}

} // namespace Geom

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :