triangular elements
Contents
clear x = [ 0.1 0.9 0.5]; y = [ 0.1 0.1 0.9]; tri = [ 1 2 3 ]; trimesh(tri,x,y); axis([0 1 0 1]);
area via determinant
area
w = max(x)-min(x);
h = max(y)-min(y);
area = 0.5*w*h
% determinant is positive for vertices ordered by right-hand rule
T = 0.5*det([ones(3,1) x' y'])
area = 0.3200 T = 0.3200
linear basis values for point inside triangle
p = [0.5 0.5]; L(1) = 0.5*det([ones(3,1) [p(1) x(2) x(3)]' [p(2) y(2) y(3)]' ])/T; L(2) = 0.5*det([ones(3,1) [x(1) p(1) x(3)]' [y(1) p(2) y(3)]' ])/T; L(3) = 0.5*det([ones(3,1) [x(1) x(2) p(1)]' [y(1) y(2) p(2)]' ])/T; disp('L'); disp(L) disp('sum(L)')
L 0.2500 0.2500 0.5000 sum(L)
area via cross product
M = [x' y' zeros(3,1)]; C = 0.5*cross(M(2,:)-M(1,:),M(3,:)-M(1,:)) A = norm(C)
C = 0 0 0.3200 A = 0.3200
show inside of triangle
[xg yg] = meshgrid(linspace(0,1,128)); yg = flipud(yg); z = polygon(xg,yg,[x' y']); imshow(z);
L1 = 0.5*(x(2)*y(3)-x(3)*y(2)-xg*(y(3)-y(2))+yg*(x(3)-x(2)))/T; imshow(L1.*z);
L2 = 0.5*(x(3)*y(1)-x(1)*y(3)+xg*(y(3)-y(1))-yg*(x(3)-x(1)))/T; imshow(L2.*z);
L3 = 0.5*(x(1)*y(2)-x(2)*y(1)-xg*(y(2)-y(1))+yg*(x(2)-x(1)))/T; imshow(L3.*z);
rgb = cat(3,L1.*z,L2.*z,L3.*z); imshow(rgb);
imshow(L1<0);
imshow(L1>1);
contour(xg,yg,L1);
imshow(~(L1<0 | L2<0 | L3<0));
rgb2 = cat(3,double(L1>0),double(L2>0),double(L3>0)); imshow(rgb2)
xv = L1*x(1)+L2*x(2)+L3*x(3); imshow(xv)
yv = L1*y(1)+L2*y(2)+L3*y(3); imshow(yv);
st = sum(sum(z)); tot1 = sum(sum(xv.*z))/st tot2 = (2/6)*(x(1)+x(2)+x(3))
tot1 = 0.5000 tot2 = 0.5000
L1sq = sum(sum(L1.^2 .*z))/st a=2; tot = 2*factorial(a)/factorial(a+2)
L1sq = 0.1673 tot = 0.1667
L1cub = sum(sum(L1.^3.*z))/st a=3; tot = 2*factorial(a)/factorial(a+2)
L1cub = 0.1007 tot = 0.1000