필터 지우기
필터 지우기

3D-Animation plot with the hgtransform command

조회 수: 3 (최근 30일)
Julián Francisco
Julián Francisco 2011년 6월 15일
I continue to think of the possibilities for the 3d-animation (moving point along a curve). I have written the following code to try it using the hgtransform command but I do not understand why does not work.
t = 0:pi/50:10*pi;
x = sin(t);
y = cos(t);
z = t;
ah = axes;
set(ah,'XLim',[min(x) max(x)],'YLim',[min(y) max(y)],...
'ZLim',[min(z) max(z)]);
plot3(x,y,z,'Color','red');
hold on;
view(3);
hpoint = line('XData',x(1),'YData',y(1),'ZData',z(1),'Color','black','Marker',...
'o','MarkerSize',10,'MarkerFaceColor','black');
ht = hgtransform('parent',ah);
set(hpoint,'Parent',ht);
for i=2:length(x)
tx = x(i)-x(i-1);
ty = y(i)-y(i-1);
tz = z(i)-z(i-1);
trans = makehgtform('translate',[tx ty tz]),
set(ht,'Matrix',trans);
pause(0.01);
end
  댓글 수: 4
Walter Roberson
Walter Roberson 2011년 6월 16일
It rotates around for me. The only difficulty I observe is that the axis limits keep moving, shifting the plot back and forth.
Julián Francisco
Julián Francisco 2011년 6월 18일
@Walter Roberson: Then I should freeze the axis limits.

댓글을 달려면 로그인하십시오.

채택된 답변

Patrick Kalita
Patrick Kalita 2011년 6월 17일
I think the main reason why this doesn't do what you expect is this part right here:
tx = x(i)-x(i-1);
ty = y(i)-y(i-1);
tz = z(i)-z(i-1);
trans = makehgtform('translate',[tx ty tz]);
I think what you had indented was that at each iteration of the for-loop, you would translate the point from where it was at the last iteration to the next point. However, hgtransform doesn't work like that. The transformation don't accumulate. Once you set the transform's Matrix, that's it -- that's the transform. It doesn't matter what the Matrix used to be. (I think the documentation talks about that somewhere, but I can't seem to find it at the moment.)
So I think what you should do instead is first place the point at (0,0,0). On the first iteration, translate it to the first datapoint, on the next iteration translate to the second, etc. Like so:
t = 0:pi/50:10*pi;
x = sin(t);
y = cos(t);
z = t;
ah = axes;
set(ah,'XLim',[min(x) max(x)],'YLim',[min(y) max(y)],...
'ZLim',[min(z) max(z)]);
plot3(x,y,z,'Color','red');
hold on;
view(3);
hpoint = line('XData', 0,'YData', 0,'ZData', 0,'Color','black','Marker',...
'o','MarkerSize',10,'MarkerFaceColor','black');
ht = hgtransform('parent',ah);
set(hpoint,'Parent',ht);
for i=2:length(x)
tx = x(i);
ty = y(i);
tz = z(i);
trans = makehgtform('translate',[tx ty tz]);
set(ht,'Matrix',trans);
pause(0.01);
end
  댓글 수: 3
Patrick Kalita
Patrick Kalita 2011년 6월 20일
With the code as it is above, the point will never actually be seen at (0,0,0). The first time anything will actually be drawn is at the first call to "pause(0.01)". By that time the point will have already been translated to the first point of the trajectory. "Starting" the point at (0,0,0) is just to make the transformations easier -- it has absolutely no effect on what you would see on screen.
Julián Francisco
Julián Francisco 2011년 11월 5일
@Patrick Kalita: Ok, you are right. Thank you back.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Lighting, Transparency, and Shading에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by