matlab implementation Planar or spatial area of a triangle formed by three points

Simple principle to explain

We know the area of the triangle = 1/2 × a × b × sinC, where C is the angle between the sides ab.
Using fork by formula (matlab in use () function cross realization) , we can get absinC, but this time the result was a three-dimensional vector with norm () function orientation amount of mold can grow.


Seeking area of ​​a triangle function

function s=area(A,B,C)
if length(A)==2  %输入三点是二维平面坐标,变成三维
    AB=[B-A 0];
    BC=[C-B 0];
elseif length(A)==3  %输入三点是三维空间坐标
    AB=B-A;
    BC=C-B;
end
    Z=cross(AB,BC);  %叉乘
    s=1/2*norm(Z);  %取模
end

test

  • Enter the result of a two-dimensional coordinate
    input:
A=[2 2];
B=[0 0];
C=[2 -2];
s=area(A,B,C)

Output:
Here Insert Picture Description

  • Enter the three-dimensional coordinates results
    Input:
A=[0 0 0];
B=[1 1 1];
C=[0 0 1];
s=area(A,B,C)

Output:
Here Insert Picture Description

Published 26 original articles · won praise 32 · views 10000 +

Guess you like

Origin blog.csdn.net/Clover_pofu/article/details/104885646