% Triangle:  Draw a triangle and show its area
% Points (px,py), (qx,qy), (rx,ry) are the vertices of a triangle.
% Diagram shows coordinates in the space (1,10)-by-(1,10).

close all  % Close all previous figure windows

%---------------------------------------------------------------
% Make your modifications below

L= 1;  U= 10;  % Random coordinates will be in (L,U)

px= randDouble(L, U);
py= randDouble(L, U); 
qx= randDouble(L, U);
qy= randDouble(L, U);
rx= randDouble(L, U);
ry= randDouble(L, U);

% Calculate the lengths of the sides using the distance formula
% (essentially this is the same as the Pytagorean theorem). 
a= sqrt ( (px-qx)^2 + (py-qy)^2 );
b= sqrt ( (qx-rx)^2 + (qy-ry)^2 );
c= sqrt ( (rx-px)^2 + (ry-py)^2 ); 

% Calculate the half perimeter from the definition 
k = (a+b+c)/2;

area= sqrt(k*(k-a)*(k-b)*(k-c));  % Area of the triangle

% Make your modifications above
%---------------------------------------------------------------

% Draw the triangle
hold on
plot([px,qx], [py,qy])
plot([qx,rx], [qy,ry])
plot([rx,px], [ry,py])
message= sprintf('Random triangle has area %.1f', area);
title(message)  % Show message as the title of the plot
axis('equal')
axis([1 10 1 10])

 