2012-11-29 47 views
0

給定一個用鼠標指針順時針方向標記4點的交互式輸入,我需要用Matlab檢查繪製的圖形是否是四邊形的凸形。我看到一些人提出了禮品包裝算法。我的想法只是使用tan,如果我有一個大於180度的角度,形狀不是凸面。 你能提出一個更好的方法來做到這一點嗎?我會感激你參考下面的代碼:凸平方 - matlab

showImage(imageA) 
hold on 
% Initially, the list of points is empty. 
xy = []; 
n = 0; 
% Loop, picking up the points. 
disp('Please enter corners of place to insert image in clockwise order.') 

for i = 1:4 
[xi,yi] = ginput(1); 
plot(xi,yi,'yo') 
xy(:,i) = [xi;yi]; 
end 

%check if this is a convex quadrillateral 
a1 = (xy(2,2) - xy(2,1))/(xy(1,2) - xy(1,1)); 
a2 = (xy(2,3) - xy(2,2))/(xy(1,3) - xy(1,2)); 
a3 = (xy(2,4) - xy(2,3))/(xy(1,4) - xy(1,3)); 
a4 = (xy(2,1) - xy(2,4))/(xy(1,1) - xy(1,4)); 

tan1 = abs(atand((a2-a1) /(1+a1*a2))); 
tan2 = abs(atand((a3-a2)/(1+a3*a2))); 
tan3 = abs(atand((a4-a3)/(1+a4*a3))); 
tan4 = abs(atand((a1-a4)/(1+a1*a4))); 

if ((tan1 > 180) | (tan2 > 180) | (tan3 > 180) | (tan4 > 180)) 
disp('this is not a convex quadrillateral!!') 
end 
+0

你的意思是說更好?不應該很難確定2D平面上的四個點是否爲凸形。 –

+0

我的想法是使用tan()= m2-m1 /(1 + m1 * m2)。因爲我有4個座標,我認爲這將是最簡單的方法。 – Gilad

+0

任何人都可以告訴我我的代碼有什麼問題嗎?我已經嘗試過非凸四極管,我得到一個答案,它是一個凸。我不會超過180 – Gilad

回答

0

這是一個非常簡單的方法來做到這一點:

  • 取3分所有組合(有4個)。
  • 檢查第四個點是否在使用這些點作爲拐角定義的三角形中。

如果任何第四點在三角形中,它不是凸的,否則它是。

我認爲如果您準備進行n + 1次檢查,這通常適用於n點。

+0

如何檢查點是否在三角形內? – Gilad

+0

您可以通過做一些基本的交叉乘積計算來檢查點是否在三角形內。 http://math.stackexchange.com/questions/51326/determining-if-an-arbitrary-point-lies-inside-a-triangle-defined-by-three-points – FakeDIY