Puzzle T20

Triangle Equality

Write a function that determines if two Triangles are equal. Two Triangless are equal if they each have the same three points. But the points in one triangle might be listed in a different order than the other triangle, so the equality function may take some work. Ignore color in determining triangle equality.

/* declare the Point type */
typedef struct
{
  int x, y;
} Point;

void printPoint( Point *p ){}
int equalPoint( Point *ptA, Point *ptB ){}

/* declare the Triangle type */
typedef struct
{
  Point p0, p1, p2;
  Color color;
} Triangle;

void setTriangle( Triangle *t, Point p0, Point p1, Point p2, Color c ){}
void printTriangle( Triangle *t ){}
int  equalTriangle( Triangle *a, Triangle *b ){}

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, p1, p2, p3, c1 );
  setTriangle( &tA, p2, p1, p3, c2 );
  
  if ( equalTriangle( &tA, &tB ) )
    printf("Equal: " );
  else
    printf("Not Equal: ");

  printTriangle( &tA );
  printf("\t");
  printTriangle( &tB );
  printf("\n");


  return 0;
}

This may take some work, because for each point in one triangle you need to find a matching point among the points in the other triangle. But a given point in other triangle may be used to match just one point in the first triangle. The problem is easier if you assume that each point of a triangle is unique, but this might not be the case.



Previous Page        Answer         Next Page         Home