2015-05-13 44 views
-6

我需要找到形状和形状上的点的纵坐标,我看到一个网页有一个公式来获取它们,但我的结果此刻是错误的。关于编辑方法的建议

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; 

} 

这里是我的结果

results 0 <-- (means that 1,1 is not within polygon) 0 <-- (means that 1,2 is not within polygon) 0 <-- (means that 1,3 is not within polygon) 1 <-- (2,1 is is within polygon)// this should be 0 1 <-- (2,2 is is within polygon) 0 <-- (means that 2,3 is is not within polygon) 0 <-- (means that 3,1 is is not within polygon) 0 <-- (means that 3,2 is is not within polygon) 0 <-- (means that 3,3 is is not within polygon)

我似乎无法弄清楚如何调整公式,需要对这个

+2

最后“)”是任何“(” – Amit

+0

语法错误无法比拟的...... – ErasmoOliveira

+0

你确定你检查这个?我建议上双击错误信息,并看到它指向你 – Javia1492

回答

4
(xArray[j]-xArray[i]) * (y-yArray[i])/(yArray[j]-yArray[i]) + xArray[i]); 

一些建议张数插入语。每)应该有一个(,同样每(应该有一个)。如果你没有相同的号码,那么你做错了什么。也许你在某处添加了一个附件,或者在复制/粘贴/移动代码时忘记删除一个。

1
if ((yArray[i]>y) != (yArray[j]>y)) 
{ 

     testval = (xArray[j]-xArray[i]) * (y-yArray[i])/(yArray[j]-yArray[i]) + xArray[i]); 
} 

你不需要);

+0

或他需要打开'('/(yArray [j] – ErasmoOliveira