3D-Animation plot with the hgtransform command in Matlab

穿精又带淫゛_ 提交于 2019-12-24 00:56:23

问题


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

回答1:


You have to calculate tx, ty, and tz in your loop as follows:

tx = x(i)-x(1);  %# Note the 1 instead of i-1
ty = y(i)-y(1);
tz = z(i)-z(1);

This is because the transform trans that you apply to the point is an absolute transform. In other words, the transform is applied to the original position of the point on each loop iteration, not to the most recent position.




回答2:


Continuing on @gnovice's answer, you could take that part out of the loop to become something like:

%# calculate translation steps  (absolute with respect to original location)
trans = bsxfun(@minus, [x(:) y(:) z(:)], [x(1) y(1) z(1)]);

%# translate point
for i=1:size(trans,1)
    set(ht, 'Matrix',makehgtform('translate',trans(i,:)));
    pause(0.01);
end



回答3:


As gnovice said, in your code you're setting the absolute transform. You can make your transform relative by multiplying instead of setting. Like this:

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',get(ht,'Matrix')*trans);

Multiplying the current value of the Matrix property by trans lets you accumulate your transform step by step.



来源:https://stackoverflow.com/questions/6362954/3d-animation-plot-with-the-hgtransform-command-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!