HW3

Problem 1

Problem 2

#include <stdio.h> #define true 1 #define false 0 typedef int tPointi[2]; int Area2(tPointi a, tPointi b, tPointi c) { return (b[0] - a[0]) * (c[1] - a[1]) - (c[0] - a[0]) * (b[1] - a[1]); } int Collinear(tPointi a, tPointi b, tPointi c) { return Area2(a, b, c) == 0; } int Between(tPointi a, tPointi b, tPointi c) { if (!Collinear(a, b, c)) return false; if (a[0] != b[0]) return ((a[0] < c[0] && b[0] > c[0]) || (a[0] > c[0] && b[0] < c[0])); else return ((a[1] < c[1] && b[1] > c[1]) || (a[1] > c[1] && b[1] < c[1])); } int TIntersect(tPointi a, tPointi b, tPointi c, tPointi d) { if (Between(a, b, c) && !Collinear(a, b, d)) return true; if (Between(a, b, d) && !Collinear(a, b, c)) return true; if (Between(c, d, a) && !Collinear(c, d, b)) return true; if (Between(c, d, b) && !Collinear(c, d, a)) return true; return false; } int main() { tPointi a = {0, 0}; tPointi b = {0, 2}; tPointi c = {0, 1}; tPointi d = {1, 1}; printf("TIntersect is %s", TIntersect(a, b, c, d)?"true":"false"); }