Answer T20
/* 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 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");
}
/* function to initialize a Triangle */
void setTriangle( Triangle *t, Point p0, Point p1, Point p2, Color c )
{
t->p0 = p0;
t->p1 = p1;
t->p2 = p2;
t->color = c;
}
/* are two Points equal? */
int equalPoint( Point *ptA, Point *ptB )
{
return ptA->x==ptB->x && ptA->y==ptB->y ;
}
int equalTriangle( Triangle *a, Triangle *b )
{
if ( equalPoint( &a->p0, &b->p0 ) && equalPoint( &a->p1, &b->p1 ) && equalPoint( &a->p2, &b->p2 ) )
return 1;
else if ( equalPoint( &a->p0, &b->p0 ) && equalPoint( &a->p1, &b->p2 ) && equalPoint( &a->p2, &b->p1 ) )
return 1;
else if ( equalPoint( &a->p0, &b->p1 ) && equalPoint( &a->p1, &b->p0 ) && equalPoint( &a->p2, &b->p2 ) )
return 1;
else if ( equalPoint( &a->p0, &b->p1 ) && equalPoint( &a->p1, &b->p2 ) && equalPoint( &a->p2, &b->p0 ) )
return 1;
else if ( equalPoint( &a->p0, &b->p2 ) && equalPoint( &a->p1, &b->p0 ) && equalPoint( &a->p2, &b->p1 ) )
return 1;
else if ( equalPoint( &a->p0, &b->p2 ) && equalPoint( &a->p1, &b->p1 ) && equalPoint( &a->p2, &b->p0 ) )
return 1;
else
return 0;
}
int main()
{
Point p1 = { 12, 45 };
Point p2 = { 12, 92 };
Point p3 = { 6, 56 };
Color c1 = {255, 123, 100 };
Color c2 = { 90, 3, 133 };
Triangle tA, tB;
setTriangle( &tA, p2, p1, p3, c2 );
setTriangle( &tB, p3, p1, p2, c2 );
if ( equalTriangle( &tA, &tB ) )
printf("Equal Triangles:\n" );
else
printf("Not Equal Triangles:\n");
printf("\t"); printTriangle( &tA );
printf("\n");
printf("\t"); printTriangle( &tB );
printf("\n");
return 0;
}