#include <stdio.h>
#include <math.h>
#define AANTAL 5
int main() {
	int rooster[AANTAL][AANTAL];
   void invoerDriehoek(int c[AANTAL][AANTAL]);
   double omtrekDriehoek(int c[AANTAL][AANTAL]);

   invoerDriehoek(rooster);
   printf("De omtrek van de ingevoerde driehoek is: %.2lf", omtrekDriehoek(rooster));

   fflush(stdin);
	getchar();
	return 0;
}

void invoerDriehoek(int c[AANTAL][AANTAL]) {
   int i, x, y;
   for (y=0; y<AANTAL; ++y) {
	   for (x=0; x<AANTAL; ++x) {
      	c[y][x]=0;
      }
   }
   i=0;
	while (i<3) {
   	do {
         printf("Geef de x coordinaat (0 t/m 4) van punt %d: ", i);
         while (scanf("%d", &x)!=1) {
	      	printf("Je moet een getal invoeren!\n");
            fflush(stdin);
         }
      }
      while (x<0 || x>4);
   	do {
         printf("Geef de y coordinaat (0 t/m 4) van punt %d: ", i);
         while (scanf("%d", &y)!=1) {
	      	printf("Je moet een getal invoeren!\n");
            fflush(stdin);
         }
      }
      while (y<0 || y>4);
		if (c[y][x]==0) {
         c[y][x]=1;
         ++i;
      }
      else {
      	printf("Dit punt is al ingevoerd. Je moet 3 orginele punten invoeren!\n");
      }
   }
}

double omtrekDriehoek(int c[AANTAL][AANTAL]) {
   int xindex, yindex, pindex;
   struct {
   	int x;
      int y;
   } punt[3];

	pindex=0;
   for (yindex=0; yindex<AANTAL; ++yindex) {
	   for (xindex=0; xindex<AANTAL; ++xindex) {
      	if (c[yindex][xindex]==1) {
         	punt[pindex].x=xindex;
         	punt[pindex].y=yindex;
            ++pindex;
         }
      }
   }

  	return sqrt(pow(punt[0].x-punt[1].x, 2.0)+pow(punt[0].y-punt[1].y, 2.0))+
			 sqrt(pow(punt[1].x-punt[2].x, 2.0)+pow(punt[1].y-punt[2].y, 2.0))+
   		 sqrt(pow(punt[2].x-punt[0].x, 2.0)+pow(punt[2].y-punt[0].y, 2.0));
}
