Circle* triangle_circumcircle(Triangle *t) { if (t->circumcircle->radius) { Point *center = triangle_circumcenter(t); t->circumcircle = circle_new(center, triangle_circumcircle_radius(t, center)); free(center); } return t->circumcircle; }
static void _test_circle(void) { int x, y; double rad; const void *clazz; void *circle; const struct _propinfo props[] = { { MUME_TYPE_DOUBLE, "radian", _CIRCLE_PROP_RADIAN, MUME_PROP_READWRITE } }; const struct _property props1[] = { { "name", "world" }, { "coord", "40, 30" }, { "radian", "3.140000000000" }, }; const struct _property props2[] = { { "name", "diff world" }, { "coord", "0, -1024" }, { "radian", "-1.110000000000" }, }; _test_properties(circle_class(), props, COUNT_OF(props)); circle = circle_new("world", 40, 30, 3.14); test_assert(strcmp(point_get_name(circle), "world") == 0); _test_get_properties(circle, props1, COUNT_OF(props1)); point_get_coord(circle, &x, &y); test_assert(40 == x && 30 == y); point_move(circle, 50, 60); point_get_coord(circle, &x, &y); test_assert(50 == x && 60 == y); rad = circle_get_radian(circle); test_assert(fabs(rad - 3.14) < 0.001); _test_set_properties(circle, props2, COUNT_OF(props2)); _test_get_properties(circle, props2, COUNT_OF(props2)); draw_result = 0; point_draw(circle); test_assert((DRAW_POINT | DRAW_CIRCLE) == draw_result); clazz = mume_class_of(circle); test_assert(circle_class() == clazz); test_assert(0 == strcmp(mume_class_name(clazz), "circle")); test_assert(mume_super_class(clazz) == point_class()); test_assert(mume_size_of(circle) == sizeof(struct _circle)); test_assert(mume_size_of(circle_class()) == sizeof(struct _circle_class)); test_assert(mume_is_a(circle, circle_class())); test_assert(!mume_is_a(circle, point_class())); test_assert(mume_is_of(circle, point_class())); mume_delete(circle); }
//GUI calls this method to try to add a new shape drawn by user cpShape *core_add_new_shape ( cpSpace *space, const int type, const double p1x, const double p1y, const double p2x, const double p2y, Color *color, const double orientation, const double friction, const double elasticity, const double density, const int index ) { //if the new shape is a circle, store the values in a circle struct, make the circle shape, and destroy the struct if ( type == CIRCLE_TYPE ) { Circle *circ = circle_new(); //set radius if ( abs ( p1x - p2x ) > abs ( p1y - p2y ) ){ circ->radius = abs ( p2y - p1y ) / 2; } else { circ->radius = abs ( p2x - p1x ) / 2; } //set centerx if ( p1x > p2x ) { circ->x = p1x - circ->radius; } else { circ->x = p1x + circ->radius; } //set centery if ( p1y > p2y ) { circ->y = p1y - circ->radius; } else { circ->y = p1y + circ->radius; } circ->color->r = color->r; circ->color->g = color->g; circ->color->b = color->b; circ->orientation = orientation; circ->friction = friction; circ->elasticity = elasticity; circ->density = density; cpShape * circ_shape = core_add_circle_shape ( space, circ, index ); circle_destroy ( circ ); return circ_shape; } //if the new shape is a box, store the values in a box struct, make the box shape, and destroy the struct if ( type == BOX_TYPE ) { Box *box = box_new(); box->x = p1x + ( p2x - p1x ) / 2; box->y = p1y + ( p2y - p1y) / 2; box->width = abs ( p2x - p1x ); box->height = abs ( p2y - p1y ); box->color->r = color->r; box->color->g = color->g; box->color->b = color->b; box->orientation = orientation; box->friction = friction; box->elasticity = elasticity; box->density = density; if ( box->width < .001 || box->height < .001 ){ box_destroy ( box ); return NULL; } cpShape *box_shape = core_add_box_shape ( space, box, index ); box_destroy ( box ); return box_shape; } return NULL; }
// core_load_level // reads contents of file, adds corresponding cpShapes to cpSpace // returns EXIT_SUCCESS if level loaded successfully // returns EXIT_FAILURE if level loaded unsuccessfully (bad syntax) static int core_load_level_parse ( cpSpace *space, FILE * fp ) { int max_line_size = 40; // maximum length of line that we want to read in // array to hold contents of each line char line [max_line_size]; char * line_place = line; // holds address of start of line fgets ( line, max_line_size, fp ); // check to see that first line has correct content if( strncmp ( line, "gravity ", 8 ) != 0 ) return EXIT_FAILURE; line_place = &line[8]; // advance line to next chars to be read double gravity = atof ( line_place ); // saves gravity input into a double core_set_gravity ( space, gravity ); // set gravity // check that next line is blank fgets ( line, max_line_size, fp ); int exit = 1; // keeps track of whether to continue reading lines while ( exit == 1 ) { if ( fgets ( line, max_line_size, fp ) == 0 ) return EXIT_SUCCESS; // get next line. If EOF, return if ( strncmp ( line, "box STATIC", 10 ) == 0 ) { // static box information to follow Box * box = box_new(); if ( parse_box ( fp, &(box->x), &(box->y), &(box->width), &(box->height), &(box->angle), (box->color), &(box->friction), &(box->elasticity), &(box->density)) == EXIT_FAILURE ) { fprintf ( stderr, "Parsing error in box STATIC\n" ); box_destroy ( box ); return EXIT_FAILURE; }; // add static box to space and destroy temporary box data core_add_static_box_shape ( space, box ); box_destroy ( box ); } else if ( strncmp ( line, "box NONSTATIC", 13 ) == 0 ) { Box * box = box_new(); if( parse_box ( fp, &(box->x), &(box->y), &(box->width), &(box->height), &(box->angle), (box->color), &(box->friction), &(box->elasticity), &(box->density) ) == EXIT_FAILURE ) { fprintf ( stderr, "Parsing error in box NONSTATIC\n" ); box_destroy ( box ); return EXIT_FAILURE; }; // need to add mass, inertia since body moves core_add_box_shape ( space, box, 0 ); box_destroy ( box ); } else if ( strncmp ( line, "ball NONSTATIC", 14 ) == 0 ) { Circle * circ = circle_new (); if ( parse_ball ( fp, &(circ->x), &(circ->y), &(circ->radius), &(circ->angle), circ->color, &(circ->friction), &(circ->elasticity), &(circ->density)) == EXIT_FAILURE ) { fprintf ( stderr, "Parsing error in ball NONSTATIC\n" ); circle_destroy ( circ ); return EXIT_FAILURE; } // need to add mass, inertia since body moves core_add_circle_shape ( space, circ, 0 ); circle_destroy ( circ ); } else if ( strncmp ( line, "ball STATIC", 11) == 0 ) { Circle * circ = circle_new(); if ( parse_ball ( fp, &(circ->x), &(circ->y), &(circ->radius), &(circ->angle), circ->color, &(circ->friction), &(circ->elasticity), &(circ->density) ) == EXIT_FAILURE ) { fprintf ( stderr, "Parsing error in ball STATIC\n" ); circle_destroy ( circ ); return EXIT_FAILURE; } core_add_static_circle_shape ( space, circ ); circle_destroy ( circ ); } else // invalid input return EXIT_FAILURE; if ( fgets ( line, max_line_size, fp ) == NULL ) exit = 0; // get next line. If EOF, get out } cpSpaceEachBody ( space, &core_update_body, (void *) NULL ); return EXIT_SUCCESS; }
// make a homebase void core_make_homebase ( cpSpace *space, const double x, const double y, const int start_index, const int planet_num){ Box *defense[4]; double r = (double)rand()/(double)RAND_MAX; double g = (double)rand()/(double)RAND_MAX; double b = (double)rand()/(double)RAND_MAX; for( int i = 0; i < 4; i++){ defense[i] = box_new(); defense[i]->color->r = r; defense[i]->color->g = g; defense[i]->color->b = b; defense[i]->orientation = 0; defense[i]->friction = 0; defense[i]->elasticity = 1; defense[i]->density = 20; defense[i]->width = SIDE_WIDTH; defense[i]->height = SIDE_HEIGHT; } defense[0]->x = x-SIDE_HEIGHT/2; defense[0]->y = y-SIDE_OFFSET; defense[1]->x = x-SIDE_OFFSET; defense[1]->y = y+SIDE_HEIGHT/2; defense[2]->x = x+SIDE_HEIGHT/2; defense[2]->y = y+SIDE_OFFSET; defense[3]->x = x+SIDE_OFFSET; defense[3]->y = y-SIDE_HEIGHT/2; cpShape *side; BodyInfo *sidebi; for(int i = 0; i< 4; i++){ side = core_add_box_shape (space, defense[i], start_index+i); cpBodySetAngle(side->body, PI*i/2); sidebi = (BodyInfo *) (side->body->data); sidebi->space_shape_type = -1; // make it so sides don't timeout } for(int i = 0; i< 4; i++){ box_destroy (defense[i]); } Circle *planet; planet = circle_new(); planet->color->r = r; planet->color->g = g; planet->color->b = b; planet->orientation = 0; planet->friction = 0; planet->elasticity = 1; planet->density = 200; planet->radius = CORE_RADIUS; planet->x = x; planet->y = y; cpShape *core = core_add_circle_shape (space, planet, start_index+4); cpShapeSetCollisionType ( core, GOAL_COLLISION_TYPE ); //pjw midnite DrawShapeInfo *info = (DrawShapeInfo *)cpShapeGetUserData(core); info->originx = x; info->originy = y; BodyInfo *bi = (BodyInfo *)core->body->data; bi->originx = x; bi->originy = y; bi->space_shape_type = planet_num; info->space_shape_type = planet_num; circle_destroy (planet); }