diff options
author | Iru Cai <mytbk920423@gmail.com> | 2018-05-24 21:39:58 +0800 |
---|---|---|
committer | Iru Cai <mytbk920423@gmail.com> | 2018-05-24 21:39:58 +0800 |
commit | 1eefd58ca4fdb5d2f51f657bfd70c9a89a4707db (patch) | |
tree | abde0e4da3c7fe138f3874a94d8eb7d0e44c3224 /euler102.c | |
download | project_euler-1eefd58ca4fdb5d2f51f657bfd70c9a89a4707db.tar.xz |
initial commit
Diffstat (limited to 'euler102.c')
-rw-r--r-- | euler102.c | 72 |
1 files changed, 72 insertions, 0 deletions
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 <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); +} |