如何用正投影投影3D绘制参数曲线?

时间:2019-07-24 00:31:00

标签: matlab animation matlab-figure

this video所示,如何生成在3D图的内壁上具有正交投影的参数曲线的3D图?

我可以使用plot3函数生成曲线的3D图,但是我不知道如何在立方体的内壁上创建蓝色的正投影,而在红色的虚线上创建

enter image description here

P.S。
另外,如何生成3个单独的“坐标函数”图(上图中未显示,但在视频中显示)...以及如何在动画过程中将它们同步在一起?

1 个答案:

答案 0 :(得分:3)

嗯,这花了很多时间写。

clear;clc;close all
t = 0:0.01:6;

x = cos(5*t);
y = sin(5*t);
z = t;

%for x,y,z as as general function of t
xmin = min(x);
xmax = max(x);

ymin = min(y);
ymax = max(y);

zmin = min(z);
zmax = max(z);

orangeColorRGB = [0.8500 0.3250 0.0980];

%3D plot
plot3DSubplot = subplot(3,2,[1 3 5]);
view(3)
grid on
title('Parametric curve in R3')
xlabel('x(t)')
ylabel('y(t)')
zlabel('z(t)')

xyProjectionOffset = -0.5;
xzProjectionOffset = 1.5;
yzProjectionOffset = 1.5;

xlim([xmin max(xmax, yzProjectionOffset)])
ylim([ymin max(ymax, xzProjectionOffset)])
zlim([min(zmin ,xyProjectionOffset) zmax])

curve3D_marker = animatedline(plot3DSubplot,x(1),y(1),z(1),'Marker','o','Color','magenta'); %marker in 3D
curve3D = animatedline(plot3DSubplot, x(1),y(1),z(1),'Color','magenta','LineStyle','-','LineWidth',2); %3D curve

curve3D_projectedToXY = animatedline(plot3DSubplot,x(1),y(1),xyProjectionOffset,'Color','blue','LineStyle','-','LineWidth',2); %projection on xy
curve3D_projectionStraightLineToXY = animatedline(plot3DSubplot,x(1),y(1),z(1),'Color',orangeColorRGB,'LineStyle','--','LineWidth',2); %projection on xy 

curve3D_projectedToXZ = animatedline(plot3DSubplot,x(1),xzProjectionOffset,z(1),'Color','blue','LineStyle','-','LineWidth',2); %projection on xz
curve3D_projectionStraightLineToXZ = animatedline(plot3DSubplot,x(1),y(1),z(1),'Color',orangeColorRGB,'LineStyle','--','LineWidth',2); %projection on xy 

curve3D_projectedToYZ = animatedline(plot3DSubplot,yzProjectionOffset,y(1),z(1),'Color','blue','LineStyle','-','LineWidth',2); %projection on yz
curve3D_projectionStraightLineToYZ = animatedline(plot3DSubplot,x(1),y(1),z(1),'Color',orangeColorRGB,'LineStyle','--','LineWidth',2); %projection on xy 

%x plot
xPlot = subplot(3,2,2);
grid on
title('Coordinates')
ylabel('cos(5t)')
xlim([t(1) t(end)])
ylim([xmin xmax])
xCoordinate = animatedline(xPlot,t(1),x(1),'Color','blue');
xCoordinate_projectedValue = animatedline(xPlot,t(1),x(1),'Color',orangeColorRGB,'LineStyle','--');
xCoordinateMarker = animatedline(xPlot,t(1),x(1),'Color','blue','Marker','o');

%y plot
yPlot = subplot(3,2,4);
grid on
ylabel('sin(5t)')
xlim([t(1) t(end)])
ylim([ymin ymax])
yCoordinate = animatedline(yPlot,t(1),y(1),'Color','blue');
yCoordinate_projectedValue = animatedline(yPlot,t(1),y(1),'Color',orangeColorRGB,'LineStyle','--');
yCoordinateMarker = animatedline(yPlot,t(1),y(1),'Color','blue','Marker','o');

%z plot
zPlot = subplot(3,2,6);
grid on
ylabel('t')
xlim([t(1) t(end)])
ylim([zmin zmax])
zCoordinate = animatedline(zPlot,t(1),z(1),'Color','blue');
zCoordinate_projectedValue = animatedline(zPlot,t(1),z(1),'Color',orangeColorRGB,'LineStyle','--');
zCoordinateMarker = animatedline(zPlot,t(1),z(1),'Color','blue','Marker','o');

for i=2:length(t)

    % 3D plot & projections
    addpoints(curve3D,x(i),y(i),z(i))
    clearpoints(curve3D_marker)
    addpoints(curve3D_marker,x(i),y(i),z(i))

    %XY projection
    addpoints(curve3D_projectedToXY,x(i),y(i),xyProjectionOffset)
    clearpoints(curve3D_projectionStraightLineToXY)
    addpoints(curve3D_projectionStraightLineToXY,[x(i) x(i)],[y(i) y(i)],[xyProjectionOffset z(i)])

    %XZ projection
    addpoints(curve3D_projectedToXZ,x(i),xzProjectionOffset,z(i))
    clearpoints(curve3D_projectionStraightLineToXZ)
    addpoints(curve3D_projectionStraightLineToXZ,[x(i) x(i)],[xzProjectionOffset y(i)],[z(i) z(i)])

    %YZ projection
    addpoints(curve3D_projectedToYZ,1.5,y(i),z(i))
    clearpoints(curve3D_projectionStraightLineToYZ)
    addpoints(curve3D_projectionStraightLineToYZ,[yzProjectionOffset x(i)],[y(i) y(i)],[z(i) z(i)])

    %2D x plot
    addpoints(xCoordinate,t(i),x(i))
    clearpoints(xCoordinate_projectedValue)
    addpoints(xCoordinate_projectedValue,[t(1) t(i)],[x(i) x(i)])
    clearpoints(xCoordinateMarker)
    addpoints(xCoordinateMarker,t(i),x(i))

    %2D y plot
    addpoints(yCoordinate,t(i),y(i))
    clearpoints(yCoordinate_projectedValue)
    addpoints(yCoordinate_projectedValue,[t(1) t(i)],[y(i) y(i)])
    clearpoints(yCoordinateMarker)
    addpoints(yCoordinateMarker,t(i),y(i))

    %2D z plot
    addpoints(zCoordinate,t(i),z(i))
    clearpoints(zCoordinate_projectedValue)
    addpoints(zCoordinate_projectedValue,[t(1) t(i)],[z(i) z(i)])
    clearpoints(zCoordinateMarker)
    addpoints(zCoordinateMarker,t(i),z(i))

    drawnow
end

for循环之外:
1)定义您的子图。

2)用某种面向对象的方法定义行。用animatedline定义行,包括其属性(颜色,线宽等)。

3)用animatedline定义行,以仅使用标记。

在for循环内:
1)使用addpoints将新点添加到这些行中。

2)关于xy,yz,xz平面的投影:
由z = -2定义的点(x0,y0,z0)到xy平面的投影为(x0,y0,-2)。因此,当您有一个点(x,y,z)时,您将以相同的方式投影到正确的平面。

3)对于将当前点连接到投影的垂直线,在每次更新绘图时,请删除先前的点并重绘它们。这就是为什么我首先调用clearpoints删除所有先前的点,然后addpoints为从投影到实际(x,y,z)点的直线添加2个点的原因。

4)对于“标记线”,再次clearpoints,然后添加新点。

5)将点添加到所有行之后,请在下一次循环迭代之前调用drawnow,以便同时更新所有行(并使更新显示为同步状态) )。