垂直于平面和平面上离原点最近的点 - matlab

normal to plane & closest point on a plane to the origin - matlab

我试图在平面上找到离原点最近的点。当我绘制法线时,不知何故它不垂直于平面!此外,平面上离原点最近的点从图中看并不正确。我不知道出了什么问题。有任何想法吗?

c = 2;
x1 = [1, 0, 0] * c;
x2 = [0, 1, 0] * c;
x3 = [0, 0, 1] * c;

x = [x1(1), x2(1), x3(1)];
y = [x1(2), x2(2), x3(2)];
z = [x1(3), x2(3), x3(3)];
figure(1); plot3(x,y,z,'*r'); hold on; grid on;

normal = cross(x1-x2, x1-x3);
% Find all coefficients of plane equation
D = -dot(normal, x1);
range = [-10 10]; [X, Z] = meshgrid(range, range);
Y = (-normal(1) * X - normal(3) * Z - D)/ normal(2);
order = [1 2  4 3]; patch(X(order),Y(order),Z(order),'b');
alpha(0.3); 

plot3([x(1), x(3)], [y(1), y(3)], [z(1), z(3)]);
plot3([x(1), x(2)], [y(1), y(2)], [z(1), z(2)]);
x1=x1-x3;
plot3([normal(1), x1(1)], [normal(2), x1(2)], [normal(3), x1(3)], 'k');

%% Get the point on the plane closest point to (0,0,0)
p = normal * (-D / sum(normal.^2,2));
plot3(p(1), p(2), p(3), '*g');

感谢您的帮助。

正常化你的正常:

normal = normal /norm(normal);

并正确绘制法线:

plot3([x1(1)+normal(1), x1(1)], [x1(2)+normal(2), x1(2)], [x1(3)+normal(3), x1(3)], 'k');

为了正确的可视化,轴应该具有相同的比例:

axis equal

看起来不错,是吗?