/* * Allocate an Element and store a duplicate of the data pointed to by * obj in the Element. Modules do not get duplicated. The function needs * to handle each type of object separately in a case statement. */ Element *element_init(ObjectType type, void *obj){ Element *e = malloc(sizeof(Element)); if(!e){ printf("malloc failed in element_init\n"); return NULL; } e->type = type; e->next = NULL; switch (e->type) { case ObjNone: printf("ObjNone not implemented in element_init\n"); break; case ObjLine: line_copy(&(e->obj.line), (Line*)obj); break; case ObjPoint: point_copy(&(e->obj.point), (Point*)obj); break; case ObjPolyline: polyline_init(&(e->obj.polyline)); polyline_copy(&(e->obj.polyline), (Polyline*)obj); break; case ObjPolygon: polygon_init(&(e->obj.polygon)); polygon_copy(&(e->obj.polygon), (Polygon*)obj); break; case ObjIdentity: break; case ObjMatrix: matrix_copy(&(e->obj.matrix), (Matrix*)obj); break; case ObjColor: case ObjBodyColor: case ObjSurfaceColor: color_copy(&(e->obj.color), (Color*)obj); break; case ObjSurfaceCoeff: e->obj.coeff = *(float*)obj; break; case ObjLight: printf("ObjLight not implemented in element_init\n"); break; case ObjModule: e->obj.module = obj; break; default: printf("ObjectType %d is not handled in element_init\n",type); } return e; }
// draw some random lines, then two boxes int main(int argc, char *argv[]) { const int nPoints = 1000; Polyline thing1; Polyline *thing2; Point p[nPoints]; int i; int rows = 500; int cols = 500; Image *src; Color Blue; Color Red; Color Green; Color_set( &Blue, 0.1, 0.15, 0.7 ); Color_set( &Red, 0.8, 0.2, 0.1 ); Color_set( &Green, 0.2, 0.6, 0.2 ); printf("Startup\n"); for(i=0;i<nPoints;i++) { point_set2D( &(p[i]), drand48()*cols/2, drand48()*rows/2 ); } // init is necessary, because otherwise the fields are full of garbage printf("Polyline init and set\n"); polyline_init( &thing1 ); polyline_set( &thing1, 20, &( p[42] ) ); printf("Polyline create\n"); thing2 = polyline_createp( 10, &(p[105] ) ); printf("Creating image\n"); src = image_create( rows, cols ); printf("Drawing poly-lines\n"); // draw some random lines polyline_draw( &thing1, src, Blue ); polyline_draw( thing2, src, Red ); // draw a box going counter-clockwise, should be ok point_set2D( &(p[500]), 50, 300 ); point_set2D( &(p[501]), 50, 350 ); point_set2D( &(p[502]), 100, 350 ); point_set2D( &(p[503]), 100, 300 ); point_set2D( &(p[504]), 50, 300 ); printf("Counter-clockwise box\n"); polyline_set( thing2, 5, &(p[500]) ); polyline_draw( thing2, src, Green ); // draw a box going clockwise, should not be ok point_set2D( &(p[500]), 350, 300 ); point_set2D( &(p[501]), 400, 300 ); point_set2D( &(p[502]), 400, 350 ); point_set2D( &(p[503]), 350, 350 ); point_set2D( &(p[504]), 350, 300 ); printf("Clockwise box\n"); polyline_set( thing2, 5, &(p[500]) ); polyline_draw( thing2, src, Green ); image_write( src, "test3c.ppm"); printf("Cleanup\n"); polyline_clear( &thing1 ); printf("clear\n"); polyline_free( thing2 ); printf("free\n"); image_free( src ); return(0); }