How to draw a line at a bearing angle in matlab?
I'm trying to simulate the movement of a target in Matlab, whose initial x and y co-ordinates, true bearing and speed (in m/s) are specified. I am wondering if there is a way to simply draw a straight line, at the specified bearing angle to show the path taken by the target (as shown in the image below)
Thanks in advance!
Your best bet is to rely on one of the built-in polar plotting functions to do this. I think the one that is most similar to your needs would be
compass. It essentially plots an arrow pointing from the center to a point (defined in cartesian coordinates) on a polar plot.
theta = deg2rad(130); % Your speed in m/s speed = 5; hax = axes(); c = compass(hax, speed * cos(theta), speed * sin(theta)); % Change the view to orient the axes the way you've drawn view([90 -90])
Then in order to change the bearing and speed, you simply call the
compass function again with your new bearing/speed.
new_theta = deg2rad(new_angle_degrees); c = compass(hax, new_speed * cos(new_theta), new_speed * sin(new_theta));
Other polar plotting options include
polarplot which accept polar coordinates but don't have an arrow head. If you don't like the polar plot you could always go with
quiver on a cartesian axes (making sure you specify the same axes).
Edit Based on your feedback and request, below is an example of a polar plot of the distance traveled.
% Speed in m/s speed = 5; % Time in seconds time = 1.5; % Bearing in degrees theta = 130; hax = axes(); % Specify polar line from origin (0,0) to target position (bearing, distance) hpolar = polar(hax, [0 deg2rad(theta)], [0 speed * time], '-o'); % Ensure the axis looks as you mentioned in your question view([90 -90]);
Now to update this plot with a new bearing, speed, time you would simply call
polar again specifying the axes.
hpolar = polar(hax, [0 theta], [0 speed], '-o');