Condivido la soluzione che ho implementato dove si utilizza come poligono un vettore di punti. Grazie ancora per la dritta
/*
#makefile ***
CC=g++
CFLAGS=-g -std=c++11
APPNAME=test
SRCFILES=./main.cpp
$(APPNAME) : $(SRCFILES)
$(CC) $(CFLAGS) $(SRCFILES) -o $(APPNAME)
*/
#include <vector>
#include <iostream>
struct Point {
double x, y;
Point ( ) : Point( 0, 0 ) {}
Point ( double _x, double _y ) : x(_x), y(_y) {}
Point &operator = ( const Point & p) {
x = p.x;
y = p.y;
return *this;
}
};
bool isInBounds ( double x, double y, const std::vector< Point > & c_polig )
{
unsigned short intersections = 0;
for ( auto it = c_polig.begin(); it != c_polig.end(); it++ ) {
Point p1 = *it;
// next point
Point p2;
if ( it + 1 != c_polig.end() )
p2 = *(it + 1);
else
// if array ended than take first point
p2 = *(c_polig.begin());
// if x is out bound not intersect
if ( ( x < p1.x && x < p2.x ) ||
( x > p1.x && x > p2.x ) )
continue;
// where intersect at point x ?
double y_int = p1.y + ( p2.y - p1.y ) * ( x - p1.x) / ( p2.x - p1.x );
// if y_int is higher than y do intersect
if ( y <= y_int )
intersections++;
}
if ( intersections % 2 ) /* is odd */
return true;
else
return false;
}
int main (int /* argc */, char ** /* argv[] */ )
{
std::vector < Point > v;
v.push_back( Point(0,0) );
v.push_back( Point(0,1) );
v.push_back( Point(1,1) );
v.push_back( Point(1,0) );
if ( isInBounds ( 0.9, 0.5, v ) )
std::cout << "in bound" << std::endl;
else
std::cout << "out bound" << std::endl;
return (0);
}