16 bool init(
const std::vector<double> &xCoords,
const std::vector<double> &yCoords);
17 bool init(
const std::vector<std::pair<double, double> > &points);
18 bool 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 Polygon2D::init(
const std::vector<double> &xCoords,
const std::vector<double> &yCoords)
28 if (xCoords.size() != yCoords.size())
34 std::vector<std::pair<double,double> > points(xCoords.size());
36 for (
int i = 0 ; i < xCoords.size() ; i++)
37 points[i] = std::pair<double, double>(xCoords[i], yCoords[i]);
42 inline bool Polygon2D::init(
const std::vector<std::pair<double, double> > &points)
44 if (points.size() < 3)
50 m_numCoords = points.size();
51 m_xyCoords.resize(m_numCoords+1);
53 for (
int i = 0 ; i < m_numCoords ; i++)
54 m_xyCoords[i] = points[i];
56 m_xyCoords[m_numCoords] = points[0];
61 inline bool Polygon2D::init(
const std::vector<Point2D> &points)
63 if (points.size() < 3)
69 m_numCoords = points.size();
70 m_xyCoords.resize(m_numCoords+1);
72 for (
int i = 0 ; i < m_numCoords ; i++)
73 m_xyCoords[i] = std::pair<double, double>(points[i].x, points[i].y);
75 m_xyCoords[m_numCoords] = std::pair<double, double>(points[0].x, points[0].y);
80 inline bool Polygon2D::isInside(
double x,
double y)
const
82 int intersections = 0;
84 for (
int i = 0 ; i < m_numCoords ; i++)
86 double y1 = m_xyCoords[i].second;
87 double y2 = m_xyCoords[i+1].second;
98 if (Y1 < y && y <= Y2)
100 double x1 = m_xyCoords[i].first;
101 double x2 = m_xyCoords[i+1].first;
103 if (x <= std::max(x1, x2))
109 double x0 = ((y - y1)*(x2 - x1))/(y2 - y1) + x1;
117 return (intersections&1)?
true:
false;
120 #endif // GRALE_POLYGON2D_H
Base class which allows an error message to be set.
Definition: errorbase.h:49
void setErrorString(const std::string &str) const
Derived classes can use this member function to store an error message.
Definition: errorbase.h:67
This class can be used to represent a polygon.
Definition: polygon2d.h:10