使用MATLAB计算连接线段之间的角度

时间:2014-08-18 16:44:19

标签: matlab line points segments angle

我第一次使用MATLAB,而且我对编程经验不足。

我有三个坐标点与线段连接在一起,以创建一种之字形路径。如果从原点到第一点的线段延伸超过第一点,我需要找到从第一点延伸到从第一点延伸到第二点的线的线的角度测量值。这也需要在第二点到第三点进行。我已经阅读了类似问题的解决方案,但我无法根据自己的情况对其进行解释和修改。

enter image description here

1 个答案:

答案 0 :(得分:1)

我们说你的坐标是:

coord = [1 2; 2 4; 1.5 1; 4 2]
coord =
    1.0000    2.0000
    2.0000    4.0000
    1.5000    1.0000
    4.0000    2.0000

这将产生以下锯齿形图案:

enter image description here

要查找每个线段的角度,您可以执行以下操作:

coord_diff = diff(coord) %// Find the difference between each 
                         %// coordinate (i.e. the line between the points)

%// Make use of complex numbers. A vector is 
%// given by x + i*y, where i is the imaginary unit
vector = coord_diff(:,1) + 1i * coord_diff(:,2);

line_angles = angle(vector) * 180/pi; %// Line angles given in degrees  
diff_line_angle = diff(line_angles)   %// The difference in angle between each line segment

这给出了以下角度,在检查图表时似乎是合理的。

line_angles =    
   63.4349
  -99.4623
   21.8014

diff_line_angle =
 -162.8973
  121.2637

评论后更新

coord = [0 0; 3 4; -1 7; 3 10]   
coord =
     0     0
     3     4
    -1     7
     3    10

coord_diff = diff(coord) %// Find the difference between each 
                         %// coordinate (i.e. the line between the points)
coord_diff =
     3     4
    -4     3
     4     3
%// The angles of these lines are approximately 36.86 and 53.13 degrees

%// Make use of complex numbers. A vector is 
%// given by x + i*y, where i is the imaginary unit
vector = coord_diff(:,1) + 1i * coord_diff(:,2);

line_angles = angle(vector) * 180/pi; %// Line angles given in degrees
line_angles =
   53.1301
  143.1301
   36.8699    

我不确定你想如何对待不同的标志等,但这样的事情应该有效:

[90-line_angles(1), arrayfun(@(n) line_angles(n+1)-line_angles(n), ...
    1:numel(line_angles)-1)].'
ans =
   36.8699
   90.0000
 -106.2602

这更简单,但如果您需要更改标志或类似内容,则更难以适应:

[90-line_angles(1); diff(line_angles)]

enter image description here

相关问题