t_shape* shape_intersect(const t_shape* shape1, const t_shape* shape2) { t_shape* result; t_frect frect1; t_frect frect2; result = shape_alloc(SHAPE_UNKNOWN, NULL, 0, 0); if (shape1->type == SHAPE_DISK && shape2->type == SHAPE_DISK && disk_intersect(shape1->data, &shape1->pos, shape2->data, &shape2->pos)) return (result); if (shape1->type == SHAPE_RECT && shape2->type == SHAPE_RECT) { frect1 = srect_tofrect(*(t_srect*)shape1->data, shape1->pos.x, shape1->pos.y); frect2 = srect_tofrect(*(t_srect*)shape2->data, shape2->pos.x, shape2->pos.y); if (frect_intersect(&frect1, &frect2)) return (result); } if (shape1->type == SHAPE_RECT && shape2->type == SHAPE_DISK) { frect1 = srect_tofrect(*(t_srect*)shape1->data, shape1->pos.x, shape1->pos.y); if (diskrectintersect(&frect1, shape2->data, &shape2->pos)) return (result); } if (shape2->type == SHAPE_RECT && shape1->type == SHAPE_DISK) { frect2 = srect_tofrect(*(t_srect*)shape2->data, shape2->pos.x, shape2->pos.y); if (diskrectintersect(&frect2, shape1->data, &shape1->pos)) return (result); } shape_destroy(&result); return (NULL); }
t_shape* shape_collide(const t_shape* shape1, const t_shape* shape2, t_fpoint* newpos) { t_shape* result; t_frect frect1; t_frect frect2; result = shape_alloc(SHAPE_UNKNOWN, NULL, 0, 0); if (shape1->type == SHAPE_DISK && shape2->type == SHAPE_DISK && disk_collide(shape1->data, &shape1->pos, shape2->data, &shape2->pos, newpos)) return (result); if (shape1->type == SHAPE_RECT && shape2->type == SHAPE_RECT) { frect1 = srect_tofrect(*(t_srect*)shape1->data, shape1->pos.x, shape1->pos.y); frect2 = srect_tofrect(*(t_srect*)shape2->data, shape2->pos.x, shape2->pos.y); if (frect_collide(&frect1, &frect2, newpos)) return (result); } if (shape1->type == SHAPE_RECT && shape2->type == SHAPE_DISK) { frect1 = srect_tofrect(*(t_srect*)shape1->data, shape1->pos.x, shape1->pos.y); if (diskrectcollide(&frect1, shape2->data, &shape2->pos, newpos)) return (result); } if (shape2->type == SHAPE_RECT && shape1->type == SHAPE_DISK) { /*frect2 = srect_tofrect(*(t_srect*)shape2->data, shape2->pos.x, shape2->pos.y); if (rectdiskcollide(&frect2, shape1->data, &shape1->pos, newpos)) return (result);*/ assert("FAIL" == NULL); } shape_destroy(&result); return (NULL); }
int main() { struct table *shapes; int loops, planecount, pointcount, x, y, z; /* Get number of bulks to process. */ scanf("%d", &loops); while(loops--) { /* allocate the shapes table and create the * initial shape. */ shapes = table_new(shape_comparator); struct shape *newshape = shape_alloc(); table_add(shapes, newshape); /* Get the plane count. */ scanf("%d", &planecount); while(planecount--) { /* Create a new plane and add it to the plane table inside * the shape. */ struct plane *newplane = plane_alloc(); table_add(newshape->planes, newplane); /* Get the point count. */ scanf("%d", &pointcount); while(pointcount--) { /* Add point to the shape. */ scanf("%d %d %d", &x, &y, &z); struct point *newpoint = point_alloc(x, y, z); if (!table_add(newshape->points, newpoint)) { int loc; table_find(newshape->points, newpoint, &loc); free(newpoint); newpoint = table_get(newshape->points, loc); } /* Add mapping between plane and point. */ table_add(newshape->ppmaps, ppmap_alloc(newplane, newpoint)); } /* Add the plane to the planes table inside shape. */ table_add(newshape->planes, newplane); } resolve_planes(shapes); resolve_shapes(shapes); coallece_planes(shapes); } }