#include#include #include typedef enum { LINE, CIRCLE, RECTANGLE } ShapeKind ; typedef struct { double x, y; } Point; typedef struct { Point p1, p2; } Line; typedef struct { Point centre; int radius; } Circle; typedef struct { Point top_left; double width, height; } Rectangle; typedef struct { ShapeKind kind; int color; union { Line line; Circle circle; Rectangle rectangle; } u; } Shape; typedef struct { Shape shapes[MAX_SHAPE]; int size; } Canvas; Point point(double x, double y) { Point p = {x, y}; return p; } Shape line(Point p1, Point p2, int color) { Line l = {p1, p2}; Shape s = {LINE, color}; s.u.line = l; return s; } Shape circle(Point centre, double radius, int color) { Circle c = {centre, radius}; Shape s = {CIRCLE, color}; s.u.circle = c; return s; } Shape rectangle(Point top_left, double width, double height, int color) { Rectangle r = {top_left, width, height}; Shape s = {RECTANGLE, color}; s.u.rectangle = r; return s; } double area(Shape s) { switch( s.kind ) { case LINE: return 0; case CIRCLE: return M_PI * s.u.circle.radius * s.u.circle.radius; case RECTANGLE: return s.u.rectangle.width * s.u.rectangle.height; default: fprintf(stderr, "ERRO\n"); exit(1); } } int main(void) { Shape c = circle(point(0,0), 1, 99); printf("%f\n", area(c)); return 0; }