Advertisement
RicardasSim

centroid of a triangle

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