[ create a new paste ] login | about

Link: http://codepad.org/h5LB1ast    [ raw code | output | fork ]

C, pasted on Sep 26:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<float.h>

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

int congruence(struct triangle *a, struct triangle *b);
int dbl_comp(const void* c1, const void* c2);
void congruenceTest(int a1, int a2, int a3, int a4, int a5, int a6, 
		int b1, int b2, int b3, int b4, int b5, int b6);

int dbl_comp(const void* c1, const void* c2) {
	double a = *(double*)c1;
	double b = *(double*)c2;
	if (fabs(a - b) <= DBL_EPSILON) {
		return 0;
	} else {
		if (a < b) {
			return -1;
		} else {
			return 1;
		}
	}
}

int congruence(struct triangle *a, struct triangle *b) {
	double a1 = sqrt(pow(a->p1[0] - a->p2[0], 2) + pow(a->p1[1] - a->p2[1], 2));
	double a2 = sqrt(pow(a->p1[0] - a->p3[0], 2) + pow(a->p1[1] - a->p3[1], 2));
	double a3 = sqrt(pow(a->p2[0] - a->p3[0], 2) + pow(a->p2[1] - a->p3[1], 2));
	double b1 = sqrt(pow(b->p1[0] - b->p2[0], 2) + pow(b->p1[1] - b->p2[1], 2));
	double b2 = sqrt(pow(b->p1[0] - b->p3[0], 2) + pow(b->p1[1] - b->p3[1], 2));
	double b3 = sqrt(pow(b->p2[0] - b->p3[0], 2) + pow(b->p2[1] - b->p3[1], 2));
	double as[3];
	double bs[3];
	int i;
	as[0] = a1;
	as[1] = a2;
	as[2] = a3;
	bs[0] = b1;
	bs[1] = b2;
	bs[2] = b3;
	qsort(as, 3, sizeof(double), dbl_comp);
	qsort(bs, 3, sizeof(double), dbl_comp);

	for (i = 0; i < 3; i++) {
		if (dbl_comp(&as[i], &bs[i]) != 0) {
			return 0;
		}
	}
	return 1;
}

void congruenceTest(int a1, int a2, int a3, int a4, int a5, int a6, 
		int b1, int b2, int b3, int b4, int b5, int b6) {
	struct triangle a;
	struct triangle b;
	a.p1[0] = a1;
	a.p1[1] = a2;
	a.p2[0] = a3;
	a.p2[1] = a4;
	a.p3[0] = a5;
	a.p3[1] = a6;

	b.p1[0] = b1;
	b.p1[1] = b2;
	b.p2[0] = b3;
	b.p2[1] = b4;
	b.p3[0] = b5;
	b.p3[1] = b6;
	printf("%d\n", congruence(&a, &b));
}

int main(void) {
	/* valid */
	congruenceTest(1,1,2,3,4,1, 1,1,2,3,4,1);
	congruenceTest(1,1,2,3,4,1, 4,1,2,3,1,1);
	congruenceTest(1,1,2,3,4,1, 4,4,5,6,7,4);
	congruenceTest(1,1,2,3,4,1, 7,4,5,6,4,4);

	/* invalid */
	congruenceTest(1,1,2,3,4,1, 2,1,2,3,4,1);
	congruenceTest(1,1,2,3,4,1, 4,1,2,4,1,1);
	congruenceTest(1,1,2,3,4,1, 4,4,5,6,8,4);
	congruenceTest(1,1,2,3,4,1, 7,5,5,6,4,4);

	return 0;
}


Output:
1
2
3
4
5
6
7
8
1
1
1
1
0
0
0
0


Create a new paste based on this one


Comments: