MCPcopy Create free account
hub / github.com/AngusJohnson/Clipper2 / PIP1

Function PIP1

CPP/BenchMark/PointInPolygonBenchmark.cpp:44–122  ·  view source on GitHub ↗

//////////////////////////////////////////////////// PIP1: This is the current Clipper2 PointInPolygon code ////////////////////////////////////////////////////

Source from the content-addressed store, hash-verified

42// PIP1: This is the current Clipper2 PointInPolygon code
43/////////////////////////////////////////////////////////
44inline PointInPolygonResult PIP1(const Point64& pt, const Path64& polygon)
45{
46 int val = 0;
47 typename Path64::const_iterator cbegin = polygon.cbegin(), first = cbegin, curr, prev;
48 typename Path64::const_iterator cend = polygon.cend();
49
50 while (first != cend && first->y == pt.y) ++first;
51 if (first == cend) // not a proper polygon
52 return PointInPolygonResult::IsOutside;
53
54 bool is_above = first->y < pt.y, starting_above = is_above;
55 curr = first + 1;
56 while (true)
57 {
58 if (curr == cend)
59 {
60 if (cend == first || first == cbegin) break;
61 cend = first;
62 curr = cbegin;
63 }
64
65 if (is_above)
66 {
67 while (curr != cend && curr->y < pt.y) ++curr;
68 if (curr == cend) continue;
69 }
70 else
71 {
72 while (curr != cend && curr->y > pt.y) ++curr;
73 if (curr == cend) continue;
74 }
75
76 if (curr == cbegin)
77 prev = polygon.cend() - 1;
78 else
79 prev = curr - 1;
80
81 if (curr->y == pt.y)
82 {
83 if (curr->x == pt.x ||
84 (curr->y == prev->y &&
85 ((pt.x < prev->x) != (pt.x < curr->x))))
86 return PointInPolygonResult::IsOn;
87 ++curr;
88 if (curr == first) break;
89 continue;
90 }
91
92 if (pt.x < curr->x && pt.x < prev->x)
93 {
94 // we're only interested in edges crossing on the left
95 }
96 else if (pt.x > prev->x && pt.x > curr->x)
97 val = 1 - val; // toggle val
98 else
99 {
100 double d = CrossProduct(*prev, *curr, pt);
101 if (d == 0) return PointInPolygonResult::IsOn;

Callers 1

BM_PIP1Function · 0.85

Calls 1

CrossProductFunction · 0.85

Tested by

no test coverage detected