I have a little problem in MATLAB: I have an array of points (named X) with x, y and z-coordiantes, given as a matrix 20 by 3. Also I have a normalized normal vector NV of a plane and chose the midpoint from array X as Point on the plane.
I tried the follwing way to plot the plane, the point array and the midpoint from the data. I expected the midpoint as a part of the plane but the plane is displaced. I just don't see the mistake and would really appreciate your help.
X =[0.8176, 0.2277, 0.4242;
0.7948, 0.4357, 0.5079;
0.6443, 0.3111, 0.0855;
0.3786, 0.9234, 0.2625;
0.8116, 0.4302, 0.8010;
0.5328, 0.1848, 0.0292;
0.3507, 0.9049, 0.9289;
0.9390, 0.9797, 0.7303;
0.8759, 0.4389, 0.4886;
0.5502, 0.1111, 0.5785;
0.6225, 0.2581, 0.2373;
0.5870, 0.4087, 0.4588;
0.2077, 0.5949, 0.9631;
0.3012, 0.2622, 0.5468;
0.4709, 0.6028, 0.5211;
0.2305, 0.7112, 0.2316;
0.8443, 0.2217, 0.4889;
0.1948, 0.1174, 0.6241;
0.2259, 0.2967, 0.6791;
0.1707, 0.3188, 0.3955];
MidP = mean(X);
NV = [0.1815, -0.6091, 0.7721];
x_min = min(X(:,1));
x_max = max(X(:,1));
z_min = min(X(:,3));
z_max = max(X(:,3));
Y1 = ((((z_min+MidP(3))*NV(3))+(x_min+MidP(1))*NV(1))/NV(2))-MidP(2);
Y2 = ((((z_min+MidP(3))*NV(3))+(x_max+MidP(1))*NV(1))/NV(2))-MidP(2);
Y3 = ((((z_max+MidP(3))*NV(3))+(x_min+MidP(1))*NV(1))/NV(2))-MidP(2);
Y4 = ((((z_max+MidP(3))*NV(3))+(x_max+MidP(1))*NV(1))/NV(2))-MidP(2);
figure('Name','Plane, Points and Midpoint')
hold on
surf([x_min x_max; x_min x_max],[Y1 Y2; Y3 Y4],[z_max z_max; z_min z_min]); %Plane
plot3(X(:,1), X(:,2), X(:,3),'.'); %Points
plot3(MidP(1),MidP(2),MidP(3),'*'); %Midpoint
xlabel('X (m)');
ylabel('Y (m)');
zlabel('Z (m)');
hold off
I think you should subtract the midpoint in your plane equation, instead of adding it:
(x-MidP(1))*NV(1) + (y-MidP(2))*NV(2) + (z-MidP(3))*NV(3) = 0
And then solve Y1 to Y4 accordingly (mind the signs)
Y1 = -(((z_min-MidP(3))*NV(3))+(x_min-MidP(1))*NV(1))/NV(2)+MidP(2);
Y2 = -(((z_min-MidP(3))*NV(3))+(x_max-MidP(1))*NV(1))/NV(2)+MidP(2);
Y3 = -(((z_max-MidP(3))*NV(3))+(x_min-MidP(1))*NV(1))/NV(2)+MidP(2);
Y4 = -(((z_max-MidP(3))*NV(3))+(x_max-MidP(1))*NV(1))/NV(2)+MidP(2);
That way you will see the plane touching the midpoint