From 1eefd58ca4fdb5d2f51f657bfd70c9a89a4707db Mon Sep 17 00:00:00 2001 From: Iru Cai Date: Thu, 24 May 2018 21:39:58 +0800 Subject: initial commit --- euler102.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 euler102.c (limited to 'euler102.c') diff --git a/euler102.c b/euler102.c new file mode 100644 index 0000000..272a54a --- /dev/null +++ b/euler102.c @@ -0,0 +1,72 @@ +#include +#include + +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); +} -- cgit v1.2.3