Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdlib.h>
- #include<assert.h>
- #define CALCULATE_COLOR(R,G,B) R + G + B // need to put parentheses on this expression
- typedef struct point_t {
- double x;
- double y;
- char* description;
- } *Point;
- typedef struct polygon_t {
- Point* vertices;
- int numberOfVertices;
- int color;
- Point center;
- } *Polygon;
- Polygon polygonCreate(int cFactor, Point center, Point* vertices, int n, int int G, int B){ // need to add const to vertices and center
- Polygon p; // needs to be allocated and checked
- assert(vertices != NULL);
- p->vertices = malloc(sizeof(Point)*n); // needs to be checked
- for(int i = 0 ; i < n ; i ++){
- p->vertices[i] = malloc(sizeof(*vertices));
- if(p->vertices[i] == NULL){
- free(p->vertices); // need to free the contents of the array as well (and remember to free desscription for each p->vertices[i])
- return NULL;
- } // need to copy the contents of vertices[i] to p->vertices[i] (?) and check if it worked
- }
- p->color = CALCULATE_COLOR(R,G,B) * cFactor; // need to set p->numofvertices to n
- p->center = center; // need to copy point rather than just copying the pointer to it
- return p;
- }
- void polygonDestroy(Polygon p){
- free(p->vertices); // need to free the contents of the array as well
- free(p); // need to change order of this line and the next line
- free(p->center); // need to free p->center->description before this
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement