2016-08-01 70 views
-1

我正在嘗試使用Java中的agorythm程序查找任何多邊形中的90度角度。如何在任何多邊形中查找90度角度

我有ArrayList的點(雙x,雙y)代表一個多邊形。 最後一點和第一點是一樣的。

結果應該是包含這些頂點的ArrayList,其中90度角。

我試圖通過使用垂直線來計算出某些東西,並計算它們之間的角度,但是這沒有奏效。

你有什麼想法如何做到這一點?

而如何確定我想檢查多邊形內的角度只?

如果我有情況是這樣的:

enter image description here

我想只得到綠色的90度角

+2

利用兩個正交矢量的乘積消失爲零的事實。 –

+3

「這不起作用」:很難相信,這*必須*工作。 (假設你使用容差的概率很高,角度永遠不會精確到90°。) –

+2

也許試圖發佈你的代碼。 – ChatterOne

回答

1

正如在評論中提到由阿克塞爾肯珀,你可以利用這樣一個事實:兩個正交(即彼此成90度)的點積是0.

// set tolerance to some small value to handle floating point errors 
static final int TOLERANCE = 0.01; 
ArrayList<Point> find90degCorners(ArrayList<Point> points) 
{ 
    ArrayList<Point> corners = new ArrayList<Point>(); 
    for(int i = 1; i < points.size() - 1; i++) 
    { 
     Point prev = points.get(i - 1); 
     Point current = points.get(i); 
     Point next = points.get(i + 1); 

     // To get vector, subtract previous point from each point. 
     // vector A = next - current 
     // vector B = current - prev 

     // Multiply element-wise for dot product A.B: 
     double dotProduct = ((next.x - current.x) * (current.x - prev.x)) + 
      ((next.y - current.y) * (current.y - prev.y)); 

     // normal of 2D vector is found by swapping x and y 
     // and flipping sign of second component 

     // to check whether it is an exterior or interior angle, 
     // take the dot product of one vector with the 
     // normal of the other 
     double direction = ((next.x - current.x) * (current.y - prev.y)) + 
      ((next.y - current.y) * (prev.x - current.x)); 

     // check if the product is within the tolerance of zero: 
     if((dotProduct > -TOLERANCE) && (dotProduct < TOLERANCE) && (direction > 0.0)) 
     { 
       corners.add(current); 
     } 
    } 
    return corners; 
} 

您可以區分是內部還是通過使用矢量法線並且比較零來做第二點積,外角爲90度。您的測試是否應該> 0或< 0取決於多邊形繞組(順時針或逆時針)以及正X和Y軸的方向。

+0

我添加了精確圖片我的意思是找到90個角度 – zaqpiotr

+0

我在看到您的照片時更新了我的答案。嘗試代碼,如果它不起作用,請嘗試將(方向<0)測試更改爲(方向> 0)。 – samgak