Francelmo

Desenho vetorial II

Nov 26th, 2020
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.04 KB | None | 0 0
  1. //Desenho vetorial II.
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5.  
  6. #define M_PI 3.14159265358979323846
  7.  
  8. typedef enum Figure_t {tri, ret, reg} Figure;
  9.  
  10. typedef struct Point_t {
  11.     float x, y;
  12. } Point;
  13.  
  14. typedef struct Object_t {
  15.     int numPoints;
  16.     Point *points;
  17. } Object;
  18.  
  19. typedef struct ObjectArray_t {
  20.     int num;
  21.     int size;
  22.     Object **objs;
  23. } ObjectArray;
  24.  
  25. ObjectArray* createObjectArray () {
  26.     ObjectArray *obja = calloc (sizeof (Object), 1);
  27.     obja->objs = calloc (sizeof (obja->size), 10);
  28.    
  29.     obja->size = 10;
  30.     obja->num = 0;
  31.    
  32.     return obja;
  33. }
  34.  
  35. void reCalloc (ObjectArray *obja) {
  36.     obja->size *= 2;
  37.    
  38.     obja->objs = realloc (obja->objs, obja->size * sizeof (obja->size));
  39. }
  40.  
  41. Object* createTriangle (Point p1, Point p2, Point p3) {
  42.     Object *ntri;
  43.     ntri = calloc (sizeof (Object), 1);
  44.    
  45.     ntri->numPoints = 3;
  46.    
  47.     ntri->points = calloc (sizeof (Point), ntri->numPoints);
  48.    
  49.     ntri->points[0] = p1;
  50.     ntri->points[1] = p2;
  51.     ntri->points[2] = p3;
  52.    
  53.     return ntri;
  54. }
  55.  
  56. Object* createRect (Point topleft, float width, float height) {
  57.     Object *nret;
  58.    
  59.     nret = calloc (sizeof (Object), 1);
  60.    
  61.     nret->numPoints = 4;
  62.    
  63.     nret->points = calloc (sizeof (Point), nret->numPoints);
  64.    
  65.     nret->points[0] = topleft;
  66.    
  67.     nret->points[1].x = topleft.x + width;
  68.     nret->points[1].y = topleft.y;
  69.    
  70.     nret->points[2].x = topleft.x + width;
  71.     nret->points[2].y = topleft.y - height;
  72.    
  73.     nret->points[3].x = topleft.x;
  74.     nret->points[3].y = topleft.y - height;
  75.    
  76.     return nret;
  77. }
  78.  
  79. Object* createRegularFigure (Point center, int num, float distance) {
  80.     float ang = 0;
  81.     Object *nreg;
  82.    
  83.     nreg = calloc (sizeof (Object), 1);
  84.    
  85.     nreg->numPoints = num;
  86.    
  87.     nreg->points = calloc (sizeof (Point), nreg->numPoints);
  88.  
  89.     for (int i=0; i<num; i++) {
  90.         nreg->points[i].x = distance * cos (ang) + center.x;
  91.         nreg->points[i].y = distance * sin (ang) + center.y;
  92.         ang += (2*M_PI) / num;
  93.     }
  94.  
  95.     return nreg;
  96. }
  97.  
  98. Object* selectObj (int obj) {
  99.     int op = obj;
  100.  
  101.     switch (op) {
  102.         case tri: {
  103.             Point p[3];
  104.            
  105.             for (int i=0; i<3; i++)
  106.                 scanf ("%f %f", &p[i].x, &p[i].y);
  107.            
  108.             Object *tri = createTriangle (p[0], p[1], p[2]);
  109.            
  110.             return tri;
  111.         }
  112.         case ret: {
  113.             Point p;
  114.             float w, h;
  115.            
  116.             scanf("%f %f %f %f", &p.x, &p.y, &w, &h);
  117.            
  118.             Object *ret = createRect (p, w, h);
  119.            
  120.             return ret;
  121.         }
  122.         case reg: {
  123.             Point p;
  124.             float d;
  125.             int l;
  126.            
  127.             scanf("%f %f %f %d", &p.x, &p.y, &d, &l);
  128.            
  129.             Object *reg = createRegularFigure (p, l, d);
  130.            
  131.             return reg;
  132.         }
  133.     }
  134. }
  135.  
  136. void addObjects (ObjectArray *obja, int obj) {
  137.     if (obja->num == obja->size)
  138.         reCalloc (obja);
  139.    
  140.     obja->objs[obja->num] = selectObj (obj);
  141.    
  142.     obja->num += 1;
  143. }
  144.  
  145. void readObjects (ObjectArray *obja, int n) {
  146.     int obj;
  147.    
  148.     for (int i=0; i<n; i++) {
  149.         scanf ("%d", &obj);
  150.        
  151.         addObjects (obja, n);
  152.     }
  153. }
  154.  
  155. void printObjectArray (ObjectArray *obja, int n) {
  156.     for (int i=0; i<n; i++) {
  157.         for (int j=0; j<obja->objs[i]->numPoints; j++)
  158.             printf ("(%.2f,%.2f)", obja->objs[i]->points[j].x, obja->objs[i]->points[j].y);
  159.    
  160.         printf ("\n");
  161.     }
  162. }
  163.  
  164. void destroyObjectArray (ObjectArray *obja, int n) {
  165.     for (int i=0; i<n; i++)
  166.         free (obja->objs[i]->points);
  167.        
  168.     free (obja->objs);
  169.     free (obja);
  170. }
  171.  
  172. int main () {
  173.     ObjectArray *objar;
  174.     int num;
  175.    
  176.     scanf ("%d", &num);
  177.    
  178.     objar = createObjectArray ();
  179.    
  180.     readObjects (objar, num);
  181.    
  182.     printObjectArray (objar, num);
  183.    
  184.     destroyObjectArray (objar, num);
  185.  
  186.   return 0;
  187. }
Advertisement
Add Comment
Please, Sign In to add comment