user3500261
user3500261

Reputation: 11

MATLAB: Math Models for Computer Science

I have question that needs to be done in MATLAB but before that I need to find the solution of the question mathematically, The question is:

Five points A,B,C,D1,D2 in R^3 are given.

(a) Determine whether three points A,B,C are collinear (lie in same line)

(b) If A,B,C are not collinear, find the plane that contains that three points

(c) Determine whether D1 and D2 are in the A,B,C plane

(d) If D1 AND D2 are not in A,B,C plane, find the point of intersection of A,B,C-plane and D1D2-line

Upvotes: 0

Views: 72

Answers (1)

Drake
Drake

Reputation: 857

If you spend a few minutes to understand the following code snippet, you will understand the "mathematical" solution as well.

// The five points, assumed all distinct.
A = [1 0 0]';
B = [0 1 0]';
C = [0 0  1]';

D1 = [0 0 0]';
D2 = [0.5 0.5 0.5]';

// Not necessary to form the matrix M. Used for convenient plotting.
M = [A B C];

hold on
grid on

// Plot all the points and the line.
for i = 1:3
    plot3(M(1,i), M(2,i), M(3,i), '.', 'MarkerSize', 24)
end
plot3(D1(1), D1(2), D1(3), 'r.', 'MarkerSize', 24)
plot3(D2(1), D2(2), D2(3), 'r.', 'MarkerSize', 24)
plot3([D1(1) D2(1)], [D1(2) D2(2)], [D1(3) D2(3)], 'k', 'LineWidth', 2)
view(120, 20)

// Cross product of B-A and C-A
cp = cross(B-A, C-A);

// Are A, B, and C collinear?
if norm(cp) == 0
    disp('The points are collinear')
else
    disp('The points are not collinear')
    // In this case, find the plane defined by the three points.
    syms x  y z
    plane_eq = [x y z]*cp;
    offset = subs(plane_eq, {x, y, z}, {A(1), A(2), A(3)});
    disp(['The equation of the plane is ' char(plane_eq) ' = ' num2str(offset)])
    u = get(gca, 'XLim');
    v = get(gca, 'YLim');
    h = ezmesh(char(solve(plane_eq - offset, z)), [u v]);
    set(h, 'EdgeColor', [0 0 0], 'FaceColor', 'none')
    if norm((D1-A)'*cp) ~= 0
        disp('D1 does not lie in the plane.')
    end

    if norm((D2-A)'*cp) ~= 0
        disp('D2 does not lie in the plane.')
    end

    M = [D1-D2 B-A C-A];
    b = D1 - A;
    t = inv(M)*b;
    ip = D1 + t(1)*(D2 - D1);
    disp(['The intersection point is [' num2str(ip') '].'])
    plot3(ip(1), ip(2), ip(3), 'g.', 'MarkerSize', 24)

end

And some visualisation

enter image description here

Upvotes: 1

Related Questions