//* Using functions - revisited to demo structures *

#include <stdio.h> #include <math.h> typedef struct POINT { // a point on plains double x, y; } POINT; double sqr (double x); double DIST(const POINT *a, const POINT *b); double AREA(double a, double b, double c); int main() { POINT p, q, r; double d1, d2, d3, area; printf("Please enter the coordinates of the triangle vertices and press ENTER.\n"); scanf("%lf %lf %lf %lf %lf %lf", &p.x, &p.y, &q.x, &q.y, &r.x, &r.y); d1=DIST(&p,&q); d2=DIST(&p,&r); d3=DIST(&q,&r); area=AREA(d1, d2, d3); area=AREA(d1, d2, d3); printf("The area of the triangle is %lf\n", area); return(0); } double sqr (double x) { return(x*x); } double DIST(const POINT *a, const POINT *b) { return( sqrt( sqr(a->x - b->x) + sqr(a->y - b->y) ) ); // OR // return( sqrt( pow( fabs(a->x - b->x),2)+pow( fabs(a->y - b->y),2) ) ); // warning: C library requires that the base in pow is >0 !!! } double AREA(double a, double b, double c) { // Hero's formula for the triangle area double p=(a+b+c)/2.0; double ar=sqrt(p*(p-a)*(p-b)*(p-c)); return(ar); }