#include <stdio.h>
#include <stdlib.h>

struct triangle {
  double p1[2];
  double p2[2];
  double p3[2];
};

int congruence (struct triangle *, struct triangle *);
int congruence (struct triangle *a, struct triangle *b)
{
  double w[2][3],t;
  int i,j,k;

  /* 各辺の長さの2乗を計算 */
  t=a->p1[0]-a->p2[0];
  w[0][0]=t*t;
  t=a->p1[1]-a->p2[1];
  w[0][0]+=t*t;

  t=a->p2[0]-a->p3[0];
  w[0][1]=t*t;
  t=a->p2[1]-a->p3[1];
  w[0][1]+=t*t;

  t=a->p3[0]-a->p1[0];
  w[0][2]=t*t;
  t=a->p3[1]-a->p1[1];
  w[0][2]+=t*t;

  t=b->p1[0]-b->p2[0];
  w[1][0]=t*t;
  t=b->p1[1]-b->p2[1];
  w[1][0]+=t*t;

  t=b->p2[0]-b->p3[0];
  w[1][1]=t*t;
  t=b->p2[1]-b->p3[1];
  w[1][1]+=t*t;

  t=b->p3[0]-b->p1[0];
  w[1][2]=t*t;
  t=b->p3[1]-b->p1[1];
  w[1][2]+=t*t;

  /* sort */
  for(k=0; k<2; k++) {
    for(i=1; i<3; i++) {
      for(j=0; j<i; j++) {
        if(w[k][j]>w[k][i]) {
          t=w[k][i];
          w[k][i]=w[k][j];
          w[k][j]=t;
        }
      }
    }
  }

  /* 一致すれば合同 */
  return ((w[0][0]==w[1][0])&&
          (w[0][1]==w[1][1])&&
          (w[0][2]==w[1][2])
         );

}

int main()
{
  struct triangle a,b;

  a.p1[0]=0;
  a.p1[1]=0;
  a.p2[0]=0;
  a.p2[1]=3.6;
  a.p3[0]=4.7;
  a.p3[1]=0;

  b.p1[0]=2.1;
  b.p1[1]=3.2;
  b.p2[0]=2.1;
  b.p2[1]=7.9;
  b.p3[0]=5.7;
  b.p3[1]=3.2;

  printf("input a = ");
  printf("(%g, %g), ", a.p1[0],a.p1[1]);
  printf("(%g, %g), ", a.p2[0],a.p2[1]);
  printf("(%g, %g)\n",a.p3[0],a.p3[1]);
  printf("input b = ");
  printf("(%g, %g), ", b.p1[0],b.p1[1]);
  printf("(%g, %g), ", b.p2[0],b.p2[1]);
  printf("(%g, %g)\n",b.p3[0],b.p3[1]);
  if(congruence(&a,&b))
    printf("合同です\n");
  else
    printf("合同ではありません\n");

  a.p1[0]=0;
  a.p1[1]=0;
  a.p2[0]=0;
  a.p2[1]=3;
  a.p3[0]=4;
  a.p3[1]=0;

  b.p1[0]=0;
  b.p1[1]=0;
  b.p2[0]=0;
  b.p2[1]=6;
  b.p3[0]=8;
  b.p3[1]=0;

  printf("input a = ");
  printf("(%g, %g), ", a.p1[0],a.p1[1]);
  printf("(%g, %g), ", a.p2[0],a.p2[1]);
  printf("(%g, %g)\n",a.p3[0],a.p3[1]);
  printf("input b = ");
  printf("(%g, %g), ", b.p1[0],b.p1[1]);
  printf("(%g, %g), ", b.p2[0],b.p2[1]);
  printf("(%g, %g)\n",b.p3[0],b.p3[1]);
  if(congruence(&a,&b))
    printf("合同です\n");
  else
    printf("合同ではありません\n");

  return 0;
}