Plot a plane based on a normal vector and a point in Matlab or matplotlib

后端 未结 5 1616
生来不讨喜
生来不讨喜 2020-11-27 03:16

How would one go plotting a plane in matlab or matplotlib from a normal vector and a point?

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-27 04:06

    The above answers are good enough. One thing to mention is, they are using the same method that calculate the z value for given (x,y). The draw back comes that they meshgrid the plane and the plane in space may vary (only keeping its projection the same). For example, you cannot get a square in 3D space (but a distorted one).

    To avoid this, there is a different way by using the rotation. If you first generate data in x-y plane (can be any shape), then rotate it by equal amount ([0 0 1] to your vector) , then you will get what you want. Simply run below code for your reference.

    point = [1,2,3];
    normal = [1,2,2];
    t=(0:10:360)';
    circle0=[cosd(t) sind(t) zeros(length(t),1)];
    r=vrrotvec2mat(vrrotvec([0 0 1],normal));
    circle=circle0*r'+repmat(point,length(circle0),1);
    patch(circle(:,1),circle(:,2),circle(:,3),.5);
    axis square; grid on;
    %add line
    line=[point;point+normr(normal)]
    hold on;plot3(line(:,1),line(:,2),line(:,3),'LineWidth',5)
    

    It get a circle in 3D: Resulting picture

提交回复
热议问题