forked from plastictown/Engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolygon.cpp
More file actions
160 lines (154 loc) · 3.07 KB
/
Copy pathpolygon.cpp
File metadata and controls
160 lines (154 loc) · 3.07 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* https://github.com/plastictown/Engine
* Copyright (C) 2018 Mikhail Domchenkov
*/
#include <polygon.h>
#include <utility>
#include <algorithm>
Polygon::Polygon ()
:m_vertices{}
{
}
Polygon::Polygon (const Polygon& rhs)try
:m_vertices{}
{
if (this == &rhs)
return;
m_vertices = rhs.m_vertices;
}
catch (...)
{
m_vertices.clear ();
throw;
}
Polygon::Polygon (Polygon && rhs) try
:m_vertices{}
{
m_vertices = std::move (rhs.m_vertices);
}
catch (...)
{
m_vertices.clear ();
throw;
}
Polygon& Polygon::operator= (const Polygon& rhs) try
{
if (this == &rhs)
return *this;
m_vertices = rhs.m_vertices;
return *this;
}
catch (...)
{
m_vertices.clear ();
throw;
}
Polygon& Polygon::operator= (const Polygon&& rhs) try
{
m_vertices = std::move (rhs.m_vertices);
return *this;
}
catch (...)
{
m_vertices.clear ();
throw;
}
Polygon::~Polygon () try
{
m_vertices.clear ();
}
catch (...)
{
//stub
}
bool Polygon::empty () const noexcept
{
return m_vertices.empty ();
}
bool Polygon::valid () const noexcept
{
return m_vertices.size () > 2;
}
bool Polygon::pointInPolygon (const Point2f& p) const try
{
if (!valid ())
return false;
int nvert = m_vertices.size ();
float testx = p.x;
float testy = p.y;
int i, j, c = 0;
for (i = 0, j = nvert - 1; i < nvert; j = i++)
{
if (((m_vertices[i].y > testy) != (m_vertices[j].y > testy))
&& (testx
< (m_vertices[j].x - m_vertices[i].x) * (testy - m_vertices[i].y)
/ (m_vertices[j].y - m_vertices[i].y) + m_vertices[i].x))
c = !c;
}
return static_cast<bool> (c);
}
catch (...)
{
//stub
return false;
}
bool Polygon::pointInPolygon (const Point2f&& p) const try
{
if (!valid ())
return false;
int nvert = m_vertices.size ();
float testx = p.x;
float testy = p.y;
int i, j, c = 0;
for (i = 0, j = nvert - 1; i < nvert; j = i++)
{
if (((m_vertices[i].y > testy) != (m_vertices[j].y > testy))
&& (testx
< (m_vertices[j].x - m_vertices[i].x) * (testy - m_vertices[i].y)
/ (m_vertices[j].y - m_vertices[i].y) + m_vertices[i].x))
c = !c;
}
return static_cast<bool> (c);
}
catch (...)
{
//stub
return false;
}
void Polygon::addPoint (const Point2f& rhs) try
{
m_vertices.push_back (rhs);
}
catch (...)
{
//stub
}
void Polygon::addPoint (const Point2f&& rhs) try
{
m_vertices.emplace_back (rhs);
}
catch (...)
{
//stub
}
void Polygon::fromRect(const Rectf& rect)try
{
m_vertices.resize(4);
m_vertices[0] = {rect.left_top.x, rect.right_bottom.y};
m_vertices[1] = rect.left_top;
m_vertices[2] = {rect.right_bottom.x, rect.left_top.y};
m_vertices[3] = rect.right_bottom;
}
catch(...)
{
//stub
}
size_t Polygon::numPoints() const noexcept
{
return m_vertices.size();
}
bool Polygon::operator == (const Polygon& other) const noexcept
{
if( m_vertices.size() != other.numPoints()) return false;
return std::equal(m_vertices.begin(), m_vertices.end(), other.m_vertices.begin());
}