1 #pragma once
2 
3 #include <cmath>
4 
5 #include <mapbox/geometry/point.hpp>
6 #include <mapbox/geometry/polygon.hpp>
7 #include <mapbox/geometry/wagyu/point.hpp>
8 
9 namespace mapbox {
10 namespace geometry {
11 namespace wagyu {
12 
13 template <typename T>
area(mapbox::geometry::linear_ring<T> const & poly)14 double area(mapbox::geometry::linear_ring<T> const& poly) {
15     std::size_t size = poly.size();
16     if (size < 3) {
17         return 0.0;
18     }
19 
20     double a = 0.0;
21     auto itr = poly.begin();
22     auto itr_prev = poly.end();
23     --itr_prev;
24     a += static_cast<double>(itr_prev->x + itr->x) * static_cast<double>(itr_prev->y - itr->y);
25     ++itr;
26     itr_prev = poly.begin();
27     for (; itr != poly.end(); ++itr, ++itr_prev) {
28         a += static_cast<double>(itr_prev->x + itr->x) * static_cast<double>(itr_prev->y - itr->y);
29     }
30     return -a * 0.5;
31 }
32 
value_is_zero(double val)33 inline bool value_is_zero(double val) {
34     return std::fabs(val) < (5.0 * std::numeric_limits<double>::epsilon());
35 }
36 
values_are_equal(double x,double y)37 inline bool values_are_equal(double x, double y) {
38     return value_is_zero(x - y);
39 }
40 
greater_than_or_equal(double x,double y)41 inline bool greater_than_or_equal(double x, double y) {
42     return x > y || values_are_equal(x, y);
43 }
44 
45 template <typename T>
slopes_equal(mapbox::geometry::point<T> const & pt1,mapbox::geometry::point<T> const & pt2,mapbox::geometry::point<T> const & pt3)46 bool slopes_equal(mapbox::geometry::point<T> const& pt1,
47                   mapbox::geometry::point<T> const& pt2,
48                   mapbox::geometry::point<T> const& pt3) {
49     return (pt1.y - pt2.y) * (pt2.x - pt3.x) == (pt1.x - pt2.x) * (pt2.y - pt3.y);
50 }
51 
52 template <typename T>
slopes_equal(mapbox::geometry::wagyu::point<T> const & pt1,mapbox::geometry::wagyu::point<T> const & pt2,mapbox::geometry::point<T> const & pt3)53 bool slopes_equal(mapbox::geometry::wagyu::point<T> const& pt1,
54                   mapbox::geometry::wagyu::point<T> const& pt2,
55                   mapbox::geometry::point<T> const& pt3) {
56     return (pt1.y - pt2.y) * (pt2.x - pt3.x) == (pt1.x - pt2.x) * (pt2.y - pt3.y);
57 }
58 
59 template <typename T>
slopes_equal(mapbox::geometry::wagyu::point<T> const & pt1,mapbox::geometry::wagyu::point<T> const & pt2,mapbox::geometry::wagyu::point<T> const & pt3)60 bool slopes_equal(mapbox::geometry::wagyu::point<T> const& pt1,
61                   mapbox::geometry::wagyu::point<T> const& pt2,
62                   mapbox::geometry::wagyu::point<T> const& pt3) {
63     return (pt1.y - pt2.y) * (pt2.x - pt3.x) == (pt1.x - pt2.x) * (pt2.y - pt3.y);
64 }
65 
66 template <typename T>
slopes_equal(mapbox::geometry::point<T> const & pt1,mapbox::geometry::point<T> const & pt2,mapbox::geometry::point<T> const & pt3,mapbox::geometry::point<T> const & pt4)67 bool slopes_equal(mapbox::geometry::point<T> const& pt1,
68                   mapbox::geometry::point<T> const& pt2,
69                   mapbox::geometry::point<T> const& pt3,
70                   mapbox::geometry::point<T> const& pt4) {
71     return (pt1.y - pt2.y) * (pt3.x - pt4.x) == (pt1.x - pt2.x) * (pt3.y - pt4.y);
72 }
73 
74 template <typename T>
wround(double value)75 inline T wround(double value) {
76     return static_cast<T>(::llround(value));
77 }
78 
79 template <>
wround(double value)80 inline std::int64_t wround<std::int64_t>(double value) {
81     return ::llround(value);
82 }
83 }
84 }
85 }
86