Pangolin
Header-only C++20 plane computational geometry library
Loading...
Searching...
No Matches
duality.hpp
Go to the documentation of this file.
1#pragma once
2
4
12
13
14namespace pgl {
15
16// -----------------------------------------------------------------------------
17// Point
18
25template <class Number, class Label>
26template <class ResultNumber>
30
39template <class Number, class Label>
40template <class ResultNumber>
42 assert(x() != 0 || y() != 0);
43 // Build two lines through this point that do not contain the origin.
45 Point<ResultNumber, Label> q1{x() + 1, y() + 1};
46 if (crossSign(p, q1) == 0) {
47 q1.y() = y() - 1;
48 }
49 Point<ResultNumber, Label> q2{x() + 1, y()};
50 if (crossSign(p, q2) == 0) {
51 q2.y() = y() - 1;
52 }
53 Line<Point<ResultNumber, Label>> l1{p, q1}, l2{p, q2};
55 l2.template polar<ResultNumber>());
56}
57
58// -----------------------------------------------------------------------------
59// Line
60
67template <class PointType, class LabelType>
68template <class ResultNumber>
70 ResultNumber qx = static_cast<ResultNumber>(min().x());
71 ResultNumber qy = static_cast<ResultNumber>(min().y());
72 ResultNumber px = static_cast<ResultNumber>(max().x());
73 ResultNumber py = static_cast<ResultNumber>(max().y());
74
75 ResultNumber den = px - qx;
76 ResultNumber anum = py - qy;
77 ResultNumber bnum = anum * px - py * den;
78 return std::make_tuple(anum, bnum, den);
79}
80
87template <class PointType, class LabelType>
88template <class ResultNumber>
89constexpr auto Line<PointType, LabelType>::polarCoordinates() const {
90 ResultNumber qx = static_cast<ResultNumber>(min().x());
91 ResultNumber qy = static_cast<ResultNumber>(min().y());
92 ResultNumber px = static_cast<ResultNumber>(max().x());
93 ResultNumber py = static_cast<ResultNumber>(max().y());
94
95 ResultNumber den = px * qy - py * qx;
96 ResultNumber anum = qy - py;
97 ResultNumber bnum = px - qx;
98 return std::make_tuple(anum, bnum, den);
99}
100
107template <class PointType, class LabelType>
108template <class ResultNumber>
110 assert(!isVertical());
111
112 // Line equation: y = anum/den * x - bnum/den.
113 const auto [anum, bnum, den] = dualCoordinates<ResultNumber>();
114 return Point<ResultNumber, typename PointType::LabelType>(anum / den, bnum / den);
115}
116
123template <class PointType, class LabelType>
124template <class ResultNumber>
126 // Line equation: anum/den * x + bnum/den y = 1.
127 const auto [anum, bnum, den] = polarCoordinates<ResultNumber>();
128 assert(den != 0);
129 return Point<ResultNumber, typename PointType::LabelType>(anum / den, bnum / den);
130}
131
132} // namespace pgl
Bounding-box and rectangle-boundary operations.
Definition arrangement.hpp:67
@ y
Definition intervaltree.hpp:24
@ x
Definition intervaltree.hpp:24
Point() -> Point< int >
constexpr std::partial_ordering crossSign(const Point< UNumber, ULabel > &u, const Point< VNumber, VLabel > &v)
Classifies the turn from one vector to another.
Definition orientation.hpp:583
Unoriented infinite line.
Definition line.hpp:52
constexpr auto dualCoordinates() const
Returns normalized dual-line coordinates for the supporting line.
Definition duality.hpp:69
constexpr const PointType & max() const
Returns the largest stored defining point.
Definition line.hpp:189
constexpr bool isVertical() const
Returns whether the line is vertical.
Definition predicates.hpp:461
constexpr const PointType & min() const
Returns the smallest stored defining point.
Definition line.hpp:180
constexpr Point< ResultNumber, typename PointType::LabelType > polar() const
Returns the polar point.
Definition duality.hpp:125
constexpr Point< ResultNumber, typename PointType::LabelType > dual() const
Returns the dual point.
Definition duality.hpp:109
Two-dimensional point with optional label payload.
Definition point.hpp:129
constexpr Point()=default
constexpr Line< Point< ResultNumber, LabelType > > dual() const
Returns the dual Line.
constexpr const NumberType & y() const
Returns the y coordinate.
Definition point.hpp:205
constexpr Line< Point< ResultNumber, LabelType > > polar() const
Returns the polar Line.