drawing a line using drawnow

11 次查看(过去 30 天)
cgo
cgo 2015-10-20
编辑: Mike Garrity 2015-10-20
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?

回答(2 个)

Ingrid
Ingrid 2015-10-20
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 个评论
cgo
cgo 2015-10-20
Hi, I was watching it draw the line 'in action' as a movie. Also, I want the line to end at the terminal point (x_2,y_2). In this case, the line still overshoots the terminal point.
Ingrid
Ingrid 2015-10-20
that is because of the discretization of t in so many points, see the answer of Thorsten on how to solve this

请先登录,再进行评论。


Thorsten
Thorsten 2015-10-20
编辑:Thorsten 2015-10-20
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
  1 个评论
Mike Garrity
Mike Garrity 2015-10-20
编辑:Mike Garrity 2015-10-20
That's correct, but I'd like to add a few comments.
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;
addpoints(h,x_next,y_next)
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Graphics Performance 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by