16 bool_t init(
const std::vector<double> &xCoords,
const std::vector<double> &yCoords);
17 bool_t init(
const std::vector<std::pair<double, double> > &points);
18 bool_t init(
const std::vector<Point2D> &points);
19 bool isInside(
double x,
double y)
const;
20 int getNumberOfPoints()
const {
return m_numCoords; }
22 std::vector<std::pair<double, double> > m_xyCoords;
26 inline bool_t Polygon2D::init(
const std::vector<double> &xCoords,
const std::vector<double> &yCoords)
28 if (xCoords.size() != yCoords.size())
29 return "Number of X and Y coordinates is not the same";
31 std::vector<std::pair<double,double> > points(xCoords.size());
33 for (
int i = 0 ; i < xCoords.size() ; i++)
34 points[i] = std::pair<double, double>(xCoords[i], yCoords[i]);
39 inline bool_t Polygon2D::init(
const std::vector<std::pair<double, double> > &points)
41 if (points.size() < 3)
42 return "Too few points to be a polygon";
44 m_numCoords = points.size();
45 m_xyCoords.resize(m_numCoords+1);
47 for (
int i = 0 ; i < m_numCoords ; i++)
48 m_xyCoords[i] = points[i];
50 m_xyCoords[m_numCoords] = points[0];
55 inline bool_t Polygon2D::init(
const std::vector<Point2D> &points)
57 if (points.size() < 3)
58 return "Too few points to be a polygon";
60 m_numCoords = points.size();
61 m_xyCoords.resize(m_numCoords+1);
63 for (
int i = 0 ; i < m_numCoords ; i++)
64 m_xyCoords[i] = std::pair<double, double>(points[i].x, points[i].y);
66 m_xyCoords[m_numCoords] = std::pair<double, double>(points[0].x, points[0].y);
71 inline bool Polygon2D::isInside(
double x,
double y)
const
73 int intersections = 0;
75 for (
int i = 0 ; i < m_numCoords ; i++)
77 double y1 = m_xyCoords[i].second;
78 double y2 = m_xyCoords[i+1].second;
89 if (Y1 < y && y <= Y2)
91 double x1 = m_xyCoords[i].first;
92 double x2 = m_xyCoords[i+1].first;
94 if (x <= std::max(x1, x2))
100 double x0 = ((y - y1)*(x2 - x1))/(y2 - y1) + x1;
108 return (intersections&1)?
true:
false;
111 #endif // GRALE_POLYGON2D_H
Type to return true/false with error description.
Definition: booltype.h:25
This class can be used to represent a polygon.
Definition: polygon2d.h:10