Answer T16
#include <stdio.h>
/* declare the Point type */
typedef struct
{
int x, y;
} Point;
/* declare the Color type */
typedef struct
{
int red, green, blue;
} Color;
/* declare the Triangle type */
typedef struct
{
Point p0, p1, p2;
Color color;
} Triangle;
/* function to print a Color */
void printColor( Color *c )
{
printf("%3d red, %3d grn, %3d blu", c->red, c->green, c->blue );
}
/* function to construct a Color */
void setColor( Color *c, int r, int g, int b )
{
if ( r<0 || r>255 ) r = 0;
if ( g<0 || g>255 ) g = 0;
if ( b<0 || b>255 ) b = 0;
c->red = r;
c->green = g;
c->blue = b;
}
/* function to print a Point */
void printPoint( Point *p )
{
printf("(%d, %d) ", p->x, p->y );
}
/* function to print a Triangle */
void printTriangle( Triangle *t )
{
printf("Points: ");
printPoint( &t->p0 ); printPoint( &t->p1 ); printPoint( &t->p2 );
printf("\nColor: ");
printColor( &t->color );
printf("\n");
}
int main()
{
/* declare and initialize three Points and a Color */
Point p0={-32,77}, p1={345, 490}, p2={140, 389};
Color c={230, 120, 54};
/* declare and initialize a Triangle */
Triangle tri = {p0, p1, p2, c};
/* print the Triangle */
printTriangle( &tri);
return 0;
}