Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- -std=c99 -Wall -Wextra -Wpedantic -Wshadow
- centroid of a triangle
- */
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct{
- double x;
- double y;
- }Point2D;
- Point2D CentroidOfTriangle( Point2D a, Point2D b, Point2D c )
- {
- //Point2D ret;
- //ret.x = (a.x + b.x + c.x) / 3.0;
- //ret.y = (a.y + b.y + c.y) / 3.0;
- //return ret;
- return (Point2D) { (a.x + b.x + c.x) / 3.0, (a.y + b.y + c.y) / 3.0 };
- }
- int main()
- {
- Point2D p1, p2, p3, ret_v;
- p1.x = 0.0;
- p1.y = 0.0;
- p2.x = 10.0;
- p2.y = 0.0;
- p3.x = 10.0;
- p3.y = 10.0;
- ret_v = CentroidOfTriangle( p1, p2, p3 );
- printf("(1) p1: %f %f p2: %f %f p3: %f %f centroid: %f %f\n",
- p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, ret_v.x, ret_v.y );
- p1.x = 71.0;
- p1.y = 8.0;
- p2.x = 30.0;
- p2.y = 24.0;
- p3.x = 67.0;
- p3.y = 51.0;
- ret_v = CentroidOfTriangle( p1, p2, p3 );
- printf("(2) p1: %f %f p2: %f %f p3: %f %f centroid: %f %f\n",
- p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, ret_v.x, ret_v.y );
- p1.x = 7.1;
- p1.y = 0.8;
- p2.x = 3.0;
- p2.y = 2.4;
- p3.x = 6.7;
- p3.y = 5.1;
- ret_v = CentroidOfTriangle( p1, p2, p3 );
- printf("(3) p1: %f %f p2: %f %f p3: %f %f centroid: %f %f\n",
- p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, ret_v.x, ret_v.y );
- p1.x = 0.71;
- p1.y = 0.08;
- p2.x = 0.3;
- p2.y = 0.24;
- p3.x = 0.67;
- p3.y = 0.51;
- ret_v = CentroidOfTriangle( p1, p2, p3 );
- printf("(4) p1: %f %f p2: %f %f p3: %f %f centroid: %f %f\n",
- p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, ret_v.x, ret_v.y );
- return 0;
- }
- /*
- output:
- (1) p1: 0.000000 0.000000 p2: 10.000000 0.000000 p3: 10.000000 10.000000 centroid: 6.666667 3.333333
- (2) p1: 71.000000 8.000000 p2: 30.000000 24.000000 p3: 67.000000 51.000000 centroid: 56.000000 27.666667
- (3) p1: 7.100000 0.800000 p2: 3.000000 2.400000 p3: 6.700000 5.100000 centroid: 5.600000 2.766667
- (4) p1: 0.710000 0.080000 p2: 0.300000 0.240000 p3: 0.670000 0.510000 centroid: 0.560000 0.276667
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement