/* CSE303, Fall 2005, Homework 3, Problem 1 */ /* simple 2D-point manipulations */ #include #include struct Point2D { double x; double y; }; typedef struct Point2D point_t; struct Line2D { double m; // slope double b; // intercept }; typedef struct Line2D line_t; void init_point(point_t * pt, double x, double y) { pt->x = x; pt->y = y; } void print_point(point_t * pt) { printf("\n(%g,%g)",pt->x,pt->y); } void init_line(line_t * line, double m, double b) { line->m = m; line->b = b; } void print_line(line_t * line) { printf("\ny = %g x + %g", line->m, line->b); } /* PUT YOUR FUNCTIONS HERE */ void points_test() { point_t points[15]; line_t lines[4]; init_point(&points[0], 0.0, 0.0); init_point(&points[1], 5.0, 5.0); init_point(&points[2], 0.0, 1.0); init_point(&points[3], 5.0, 6.0); init_line(&lines[0], -1.0, -3.0); init_line(&lines[1], 0.0, 3.0); lines[2] = line_containing(points[0], points[1]); lines[3] = line_containing(points[2], points[3]); points[4] = intersect1(lines[0],lines[1]); points[5] = intersect1(lines[2],lines[3]); {point_t * new_point1 = intersect2(&lines[0],&lines[1]); point_t * new_point2 = intersect2(&lines[2],&lines[3]); point_t * new_point3 = intersect3(&lines[0],&lines[1]); point_t * new_point4 = intersect3(&lines[2],&lines[3]); points[6] = *new_point1; points[7] = *new_point2; points[8] = *new_point3; points[9] = *new_point4; free(new_point1); free(new_point2); free(new_point3); free(new_point4); intersect4(&points[10],&lines[0],&lines[1]); intersect4(&points[11],&lines[2],&lines[3]); {int ans1 = intersect5(&points[12],&lines[0],&lines[1]); int ans2 = intersect5(&points[13],&lines[2],&lines[3]); int i; for(i=0; i < 4; ++i) print_line(&lines[i]); for(i=0; i < 15; ++i) print_point(&points[i]); printf("\nans1=%d, ans2=%d\n",ans1,ans2); }} } int main(int argc, char**argv) { points_test(); return 0; }