Calculating Angles between connected line segments using MATLAB -
i'm using matlab first time, , have little experience programming.
i have 3 coordinate points connected line segments create sort of zig-zag path. if line segment origin first point extended past first point, need find angle measure line extending first point line extending first point second point. needs done second third point well. i've read solutions of similar questions, wasn't able interpret , modify them situation.
let's coordinates are:
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
this give following zig-zag pattern:
to find angles of each line segment, can following:
coord_diff = diff(coord) %// find difference between each %// coordinate (i.e. line between points) %// make use of complex numbers. vector %// given x + i*y, 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) %// difference in angle between each line segment
this gives following angles, upon inspection of graph seems reasonable.
line_angles = 63.4349 -99.4623 21.8014 diff_line_angle = -162.8973 121.2637
update after comments
coord = [0 0; 3 4; -1 7; 3 10] coord = 0 0 3 4 -1 7 3 10 coord_diff = diff(coord) %// find difference between each %// coordinate (i.e. line between points) coord_diff = 3 4 -4 3 4 3 %// angles of these lines approximately 36.86 , 53.13 degrees %// make use of complex numbers. vector %// given x + i*y, 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
i'm not sure how want treat different signs etc., should work:
[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
this simpler, harder adapt in case need change signs or similar:
[90-line_angles(1); diff(line_angles)]
Comments
Post a Comment