2015-05-13 86 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