/* Define a struct to describe a point and a triangle in the 2D plane, and implement methods to display and find perimeter of an arbitrary triangle Optional exercise: write a function to implement an area of a triangle Emad Arasteh November 21 , 2019 */ #include #include typedef struct { int x; int y; } point_t; typedef enum {scalene, equilateral, isosceles, right} triangle_type; typedef struct { point_t vertices[3]; triangle_type type; } triangle_t; void display(triangle_t triangle) { int i; for (i = 0; i< 3; i++) { printf("(X,Y) coordinates of a triangle: (%0d, %0d)\n", triangle.vertices[i].x, triangle.vertices[i].y); } printf("Type of a triangle is "); switch(triangle.type) { case scalene: printf("scalene.\n"); break; case equilateral: printf("equilateral.\n"); break; case isosceles: printf("isosceles.\n"); break; case right: printf("right.\n"); break; default: break; } } double perimeter(point_t vertices[3]) { double side_a, side_b, side_c, perimeter = 0; side_a = sqrt(pow(vertices[0].x - vertices[1].x, 2) + pow(vertices[0].y - vertices[1].y, 2)); side_b = sqrt(pow(vertices[1].x - vertices[2].x, 2) + pow(vertices[1].y - vertices[2].y, 2)); side_c = sqrt(pow(vertices[2].x - vertices[0].x, 2) + pow(vertices[2].y - vertices[0].y, 2)); perimeter = side_a + side_b + side_c; return perimeter; } int main() { triangle_t triangle = { { {0, 0}, {6, 0}, {6, 8} } , right }; display(triangle); printf("Perimeter of the triangle is %f \n", perimeter(triangle.vertices)); return 0; }