G18 Answer


int isRightTriangle( Triangle *t )
{
  int d01 = (t->p0.x - t->p1.x)*(t->p0.x - t->p1.x) +
            (t->p0.y - t->p1.y)*(t->p0.y - t->p1.y) ;
  int d12 = (t->p1.x - t->p2.x)*(t->p1.x - t->p2.x) +
            (t->p1.y - t->p2.y)*(t->p1.y - t->p2.y) ;
  int d02 = (t->p0.x - t->p2.x)*(t->p0.x - t->p2.x) +
            (t->p0.y - t->p2.y)*(t->p0.y - t->p2.y) ;

  int isRight = 0;
  if ( d01 == d12+d02 ) isRight = 1;
  if ( d12 == d01+d02 ) isRight = 1;
  if ( d02 == d01+d12 ) isRight = 1;
  
  return isRight;
}

Comments: If you don't like the ugliness of the int initializations, you could use a macro:

#define square(x) ((x)*(x))

int d01 = square(t->p0.x - t->p1.x) + square(t->p0.y - t->p1.y);

.. etc ...

However, it would be a big mistake to use the pow() function of the standard math library because that function uses double precision floating point and logarithms, and gives only approximate answers (as is generally true for floating point.)

Using the text preprocessor should usually be avoided, so you may wish to use some intermediate variables to show the computation:

int x01 = t->p0.x - t->p1.x;
int y01 = t->p0.y - t->p1.y;
int d01 = x01*x01 + y01*y01;

... etc ...

These extra variables might look like a waste, but an optimizing compiler will not actually implement them when it compiles the expressions into machine language.