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
|
//x y coordinates of a square
x, y
1, 1
1, 3
3, 3
3, 1
bool Shape::inPoly(int x,int y)
{
xArray[0] = 1;
xArray[1] = 1;
xArray[2] = 3;
xArray[3] = 3;
yArray[0] = 1;
yArray[1] = 3;
yArray[2] = 3;
yArray[3] = 1;
int i;
int j;
int nvert = 4;
int c = 0;
int testval = 0;
for (i = 0, j = nvert - 1; i < nvert; j = i++)
{
if ((yArray[i]>y) != (yArray[j]>y))
{
testval = (xArray[j]-xArray[i]) * (y-yArray[i]) / (yArray[j]-yArray[i]) + xArray[i];
}
if (x == testval)
{
// point is on boundary!
c = 0; // set indicator to "not inside"
break; // abort loop
}
if (x < testval)
{
// intersection found
c = !c;
}
}
return c;
}
void Shape::pointInShape()
{
std::cout << "results" << std::endl;
std::cout << inPoly(1,1) << std::endl;
std::cout << inPoly(1,2) << std::endl;
std::cout << inPoly(1,3) << std::endl;
std::cout << inPoly(2,1) << std::endl;
std::cout << inPoly(2,2) << std::endl;
std::cout << inPoly(2,3) << std::endl;
std::cout << inPoly(3,1) << std::endl;
std::cout << inPoly(3,2) << std::endl;
std::cout << inPoly(3,3) << std::endl;
}
| |