# drawing a line using drawnow

18 views (last 30 days)
cgo on 20 Oct 2015
Edited: Mike Garrity on 20 Oct 2015
I am drawing a line from a point (x_1, y_1) to a terminal point (x_2,y_2).
for t=0:0.01:5
plot(x_1+(x_2-x_1)*t, y_1+(y_2-y_1)*t, 'red')
hold on
drawnow()
end
The problem with the current code is that YES, there a line between the two points. But, it overshoots the terminal point because of the parameter t. Is there a way I can write the code in such a way that the line ends at the terminal point without overshooting it?

Ingrid on 20 Oct 2015
it is not at all clear to me why you are using a for loop for this why not just use
t = 0:0.01:5;
X = x_1+(x_2-x_1)*t;
Y=y_1+(y_2-y_1)*t;
plot(X,Y,'red');
##### 2 CommentsShowHide 1 older comment
Ingrid on 20 Oct 2015
that is because of the discretization of t in so many points, see the answer of Thorsten on how to solve this

Thorsten on 20 Oct 2015
Edited: Thorsten on 20 Oct 2015
axes
axis([x_1-10 x_2+10 y_1-10 y_2+10])
hold on
tmax = 5;
for t=0:0.01:tmax
plot(x_1+(x_2-x_1)*t/tmax, y_1+(y_2-y_1)*t/tmax, 'red')
drawnow()
end
Mike Garrity on 20 Oct 2015
Edited: Mike Garrity on 20 Oct 2015
First, if you want to draw a continuous line, you'll need to give each plot command two points; the end of the previous line segment as well as the new point. That would look something like this:
axes
axis([x_1-10 x_2+10 y_1-10 y_2+10])
hold on
tmax = 5;
x_prev = [];
y_prev = [];
for t=0:0.01:tmax
x_next = x_1+(x_2-x_1)*t/tmax;
y_next = y_1+(y_2-y_1)*t/tmax;
if ~isempty(x_prev)
plot([x_prev x_next], [y_prev y_next], 'red')
end
x_prev = x_next;
y_prev = y_next;
drawnow
end
But there are still some problems with that approach. It is going to create a large number of line objects. These can be hard to manage, and it is going to encounter a lot of performance problems if you scale it up. See this post for details.
A better approach would be to create a single line object and append your points to it. That would looks something like this:
axes
axis([x_1-10 x_2+10 y_1-10 y_2+10])
tmax = 5;
h = plot(nan,nan,'red');
for t=0:0.01:tmax
x_next = x_1+(x_2-x_1)*t/tmax;
y_next = y_1+(y_2-y_1)*t/tmax;
set(h,'XData',[get(h,'XData'), x_next], ...
'YData',[get(h,'YData'), y_next]);
drawnow
end
In some ways that's actually simpler too, because the line object keeps track of the previous point for you. However it has a performance issue as well. Every time you add a point to those XData and YData arrays, they have to get moved between your computer's main memory and the memory on your graphics card.
In R2014b, a new object called animatedline was added which does some clever tricks to avoid that "sloshing". You would use it like this:
axes
axis([x_1-10 x_2+10 y_1-10 y_2+10])
tmax = 5;
h = animatedline('Color','red');
for t=0:0.01:tmax
x_next = x_1+(x_2-x_1)*t/tmax;
y_next = y_1+(y_2-y_1)*t/tmax;
drawnow
end
You probably don't need to worry about these details for the simple case you're working on here, but I'd like to make sure that other people who find this post are aware of these other options.

### Categories

Find more on Animation in Help Center and File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by