RicardasSim

area of a triangle

Jun 13th, 2020
2,332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.92 KB | None | 0 0
  1. /*
  2. -std=c99 -Wall -Wextra -Wpedantic -Wshadow
  3.  
  4. Info, area of a triangle by formula:
  5. https://www.mathopenref.com/coordtrianglearea.html
  6.  
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <math.h>
  12.  
  13. typedef struct{
  14.     double x;
  15.     double y;
  16. }Point2D;
  17.  
  18. double AreaOfTriangle( Point2D a, Point2D b, Point2D c )
  19. {
  20.  
  21.     return fabs( ( a.x * ( b.y - c.y ) + b.x * ( c.y - a.y ) + c.x * ( a.y - b.y ) ) / 2.0 );
  22.  
  23. }
  24.  
  25.  
  26. int main()
  27. {
  28.  
  29.     Point2D p1, p2, p3;
  30.  
  31.     p1.x = 0.0;
  32.     p1.y = 0.0;
  33.  
  34.     p2.x = 10.0;
  35.     p2.y = 0.0;
  36.  
  37.     p3.x = 10.0;
  38.     p3.y = 10.0;
  39.  
  40.     printf("(1) p1: %f %f p2: %f %f p3: %f %f area: %f\n",
  41.             p1.x, p1.y, p2.x, p2.y, p3.x, p3.y,
  42.             AreaOfTriangle( p1, p2, p3 ) );
  43.  
  44.  
  45.  
  46.     p1.x = 71.0;
  47.     p1.y = 8.0;
  48.  
  49.     p2.x = 30.0;
  50.     p2.y = 24.0;
  51.  
  52.     p3.x = 67.0;
  53.     p3.y = 51.0;
  54.  
  55.     printf("(2) p1: %f %f p2: %f %f p3: %f %f area: %f\n",
  56.             p1.x, p1.y, p2.x, p2.y, p3.x, p3.y,
  57.             AreaOfTriangle( p1, p2, p3 ) );
  58.  
  59.  
  60.  
  61.     p1.x = 7.1;
  62.     p1.y = 0.8;
  63.  
  64.     p2.x = 3.0;
  65.     p2.y = 2.4;
  66.  
  67.     p3.x = 6.7;
  68.     p3.y = 5.1;
  69.  
  70.     printf("(3) p1: %f %f p2: %f %f p3: %f %f area: %f\n",
  71.             p1.x, p1.y, p2.x, p2.y, p3.x, p3.y,
  72.             AreaOfTriangle( p1, p2, p3 ) );
  73.  
  74.  
  75.  
  76.     p1.x = 0.71;
  77.     p1.y = 0.08;
  78.  
  79.     p2.x = 0.3;
  80.     p2.y = 0.24;
  81.  
  82.     p3.x = 0.67;
  83.     p3.y = 0.51;
  84.  
  85.     printf("(4) p1: %f %f p2: %f %f p3: %f %f area: %f\n",
  86.             p1.x, p1.y, p2.x, p2.y, p3.x, p3.y,
  87.             AreaOfTriangle( p1, p2, p3 ) );
  88.  
  89.  
  90.  
  91.     return 0;
  92. }
  93.  
  94. /*
  95. output:
  96.  
  97. (1) p1: 0.000000 0.000000 p2: 10.000000 0.000000 p3: 10.000000 10.000000 area: 50.000000
  98. (2) p1: 71.000000 8.000000 p2: 30.000000 24.000000 p3: 67.000000 51.000000 area: 849.500000
  99. (3) p1: 7.100000 0.800000 p2: 3.000000 2.400000 p3: 6.700000 5.100000 area: 8.495000
  100. (4) p1: 0.710000 0.080000 p2: 0.300000 0.240000 p3: 0.670000 0.510000 area: 0.084950
  101.  
  102. */
Advertisement
Add Comment
Please, Sign In to add comment