/*
-std=c99 -Wall -Wextra -Wpedantic -Wshadow
Info, area of a triangle by formula:
https://www.mathopenref.com/coordtrianglearea.html
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct{
double x;
double y;
}Point2D;
double AreaOfTriangle( Point2D a, Point2D b, Point2D c )
{
return fabs( ( a.x * ( b.y - c.y ) + b.x * ( c.y - a.y ) + c.x * ( a.y - b.y ) ) / 2.0 );
}
int main()
{
Point2D p1, p2, p3;
p1.x = 0.0;
p1.y = 0.0;
p2.x = 10.0;
p2.y = 0.0;
p3.x = 10.0;
p3.y = 10.0;
printf("(1) p1: %f %f p2: %f %f p3: %f %f area: %f\n",
p1.x, p1.y, p2.x, p2.y, p3.x, p3.y,
AreaOfTriangle( p1, p2, p3 ) );
p1.x = 71.0;
p1.y = 8.0;
p2.x = 30.0;
p2.y = 24.0;
p3.x = 67.0;
p3.y = 51.0;
printf("(2) p1: %f %f p2: %f %f p3: %f %f area: %f\n",
p1.x, p1.y, p2.x, p2.y, p3.x, p3.y,
AreaOfTriangle( p1, p2, p3 ) );
p1.x = 7.1;
p1.y = 0.8;
p2.x = 3.0;
p2.y = 2.4;
p3.x = 6.7;
p3.y = 5.1;
printf("(3) p1: %f %f p2: %f %f p3: %f %f area: %f\n",
p1.x, p1.y, p2.x, p2.y, p3.x, p3.y,
AreaOfTriangle( p1, p2, p3 ) );
p1.x = 0.71;
p1.y = 0.08;
p2.x = 0.3;
p2.y = 0.24;
p3.x = 0.67;
p3.y = 0.51;
printf("(4) p1: %f %f p2: %f %f p3: %f %f area: %f\n",
p1.x, p1.y, p2.x, p2.y, p3.x, p3.y,
AreaOfTriangle( p1, p2, p3 ) );
return 0;
}
/*
output:
(1) p1: 0.000000 0.000000 p2: 10.000000 0.000000 p3: 10.000000 10.000000 area: 50.000000
(2) p1: 71.000000 8.000000 p2: 30.000000 24.000000 p3: 67.000000 51.000000 area: 849.500000
(3) p1: 7.100000 0.800000 p2: 3.000000 2.400000 p3: 6.700000 5.100000 area: 8.495000
(4) p1: 0.710000 0.080000 p2: 0.300000 0.240000 p3: 0.670000 0.510000 area: 0.084950
*/
Comments