2015-03-02 50 views
2

我試圖計算在紅色圖片中顯示的值,即內角。不規則多邊形內角與角度> 180

我已經有了一個點的陣列,這些點相交併試圖使用點積,但它只返回最小的角度。我需要全方位的內部角度(0-359),但似乎找不到符合此標準的很多內容。

arrow

+0

[This question](http://stackoverflow.com/questions/28730855/how-to-compute-directional-angle-between-two-2d-vectors-in-matlab/28732160#28732160)有答案MATLAB語言。不過,我認爲你仍應該能夠遵循它 – eigenchris 2015-03-02 23:28:23

回答

2

假設你的角度爲標準逆時針格式,下面應該工作:

void angles(double points[][2], double angles[], int npoints){ 
    for(int i = 0; i < npoints; i++){ 
     int last = (i - 1 + npoints) % npoints; 
     int next = (i + 1) % npoints; 
     double x1 = points[i][0] - points[last][0]; 
     double y1 = points[i][1] - points[last][1]; 
     double x2 = points[next][0] - points[i][0]; 
     double y2 = points[next][1] - points[i][1]; 
     double theta1 = atan2(y1, x1)*180/3.1415926358979323; 
     double theta2 = atan2(y2, x2)*180/3.1415926358979323; 
     angles[i] = (180 + theta1 - theta2 + 360); 
     while(angles[i]>360)angles[i]-=360; 
    } 
} 

顯然,如果你正在使用某種數據結構來處理你的積分,你將需要替換double points[][2]並且繼續請參考您的數據結構。

1

您可以獲取全角度範圍(-Pi..Pi)與ATAN2函數:

atan2(crossproduct, dotproduct)