Advertisement
Guest User

Untitled

a guest
Nov 28th, 2015
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. #include<stdlib.h>
  2. #include<assert.h>
  3. #define CALCULATE_COLOR(R,G,B) R + G + B // need to put parentheses on this expression
  4. typedef struct point_t {
  5. double x;
  6. double y;
  7. char* description;
  8. } *Point;
  9. typedef struct polygon_t {
  10. Point* vertices;
  11. int numberOfVertices;
  12. int color;
  13. Point center;
  14. } *Polygon;
  15. Polygon polygonCreate(int cFactor, Point center, Point* vertices, int n, int int G, int B){ // need to add const to vertices and center
  16. Polygon p; // needs to be allocated and checked
  17. assert(vertices != NULL);
  18. p->vertices = malloc(sizeof(Point)*n); // needs to be checked
  19. for(int i = 0 ; i < n ; i ++){
  20. p->vertices[i] = malloc(sizeof(*vertices));
  21. if(p->vertices[i] == NULL){
  22. free(p->vertices); // need to free the contents of the array as well (and remember to free desscription for each p->vertices[i])
  23. return NULL;
  24. } // need to copy the contents of vertices[i] to p->vertices[i] (?) and check if it worked
  25. }
  26. p->color = CALCULATE_COLOR(R,G,B) * cFactor; // need to set p->numofvertices to n
  27. p->center = center; // need to copy point rather than just copying the pointer to it
  28. return p;
  29. }
  30. void polygonDestroy(Polygon p){
  31. free(p->vertices); // need to free the contents of the array as well
  32. free(p); // need to change order of this line and the next line
  33. free(p->center); // need to free p->center->description before this
  34. return;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement