summaryrefslogtreecommitdiff
path: root/euler102.c
blob: 272a54a250d8c4dedeb16fe939b75db92c15f397 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdio.h>
#include <stdbool.h>

typedef struct
{
	int a[2],b[2],c[2];
} triangle;

static inline void
vsub(int d[], int s[], int t[])
{
	d[0] = s[0] - t[0];
	d[1] = s[1] - t[1];
}

static int
ext_product(int s[], int t[])
{
	return s[0] * t[1] - s[1] * t[0];
}

static int sign(int x)
{
	if (x < 0)
		return -1;
	if (x > 0)
		return 1;
	return 0;
}

bool o_in_tri(triangle *t)
{
	int ab[2], ac[2], bc[2];

	vsub(ab, t->b, t->a);
	vsub(ac, t->c, t->a);
	vsub(bc, t->c, t->b);

	/* sig(ABxAC) = sig(OBxOC) */
	int sabc = sign(ext_product(ab, ac));
	int sobc = sign(ext_product(t->b, t->c));
	if (sabc != sobc)
		return false;

	/* sig(BAxBC) = sig(OAxOC) */
	int sbac = sign(ext_product(bc, ab));
	int soac = sign(ext_product(t->a, t->c));
	if (sbac != soac)
		return false;

	/* sig(CAxCB) = sig(OAxOB) */
	int scab = sign(ext_product(ac, bc));
	int soab = sign(ext_product(t->a, t->b));
	if (scab != soab)
		return false;

	return true;
}

int main()
{
	triangle t;
	int n = 0;
	/* the data uses ',' to split the integers, process it with
	 * sed 's/,/ /g' first. */
	while (scanf("%d%d%d%d%d%d", &t.a[0], &t.a[1], &t.b[0], &t.b[1],
				&t.c[0], &t.c[1]) != EOF) {
		if (o_in_tri(&t))
			n++;
	}
	printf("%d\n", n);
}