我是第一次使用MATLAB,编程经验很少。
我将三个坐标点与线段连接在一起,以创建一种锯齿形路径。如果从原点到第一个点的线段延伸超过第一个点,我需要找到从第一个点延伸的线到从第一个点延伸到第二个点的线的角度度量。对于第二点到第三点也需要这样做。我已经阅读了类似问题的解决方案,但我无法根据我的情况解释和修改它们。
假设您的坐标是:
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
这将给出以下之字形图案:
要查找每个线段的角度,您可以执行以下操作:
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)]