Advertisement
Broatlas

vector

Sep 22nd, 2016
2,181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. #define _CRT_SECURE_NO_WARNING
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. //look into too these includes
  7.  
  8. #include <crtdbg.h>
  9.  
  10. #define _CRTDBG_MAP_ALLOC //need this to get the line identification
  11. //_CrtSetDbFlag(_CRTDBG_ALLOC_MEM_DF|_CRTDBG_LEAK_CHECK_DF; // in mian, after local
  12. //NB must b in debug
  13.  
  14. enum{ RUNNING = 1 };
  15.  
  16. struct Point
  17. {
  18.     int x, y;
  19. };
  20.  
  21. struct Line
  22. {
  23.     Point start;
  24.     Point end;
  25. };
  26.  
  27. struct GraphicElement
  28. {
  29.     enum{ SIZE = 256 };
  30.     unsigned int numLines;
  31.     Line* pLines;
  32.     char name[SIZE];
  33. };
  34.  
  35. typedef struct
  36. {
  37.     unsigned int numGraphicElements;
  38.     GraphicElement* pElements;
  39. }VectorGraphic;
  40.  
  41. void InitVectorGraphic(VectorGraphic*);
  42. void AddGraphicElement(VectorGraphic*);
  43. void ReportVectorGraphic(VectorGraphic*);
  44. void CleanUpVectorGraphic(VectorGraphic*);
  45.  
  46. VectorGraphic Image;
  47.  
  48.  
  49. int main()
  50. {
  51.     char response;
  52.     _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
  53.  
  54.     InitVectorGraphic(&Image);
  55.  
  56.     while (RUNNING)
  57.     {
  58.         printf("\nPlease select an option:\n");
  59.         printf("1. Add a Graphic Element\n");
  60.         printf("2. List the Graphic Elements\n");
  61.         printf("q. Quit\n");
  62.         printf("CHOICE: ");
  63.         fflush(stdin);
  64.         scanf("%c", &response);
  65.  
  66.         switch (response)
  67.         {
  68.         case '1':AddGraphicElement(&Image); break;
  69.         case '2':ReportVectorGraphic(&Image); break;
  70.         case 'q':CleanUpVectorGraphic(&Image); return 0;
  71.         default:printf("Please enter a valid option\n");
  72.         }
  73.         printf("\n");
  74.     }
  75. }
  76.  
  77. void InitVectorGraphic(VectorGraphic* pImage)
  78. {
  79.     pImage->pElements = NULL;
  80.     pImage->numGraphicElements = 0;
  81.    
  82.    
  83. }
  84.  
  85. void AddGraphicElement(VectorGraphic* pImage)
  86. {
  87.     pImage->numGraphicElements++;
  88.     pImage->pElements = (GraphicElement*)malloc(pImage->numGraphicElements * sizeof(GraphicElement));
  89.     printf("Please enter the name of the new GraphicElement(<256 characters): ");
  90.     scanf("%s", pImage->pElements->name);
  91.    
  92.     printf("How many lines are there in this new Graphic Element?:");
  93.     scanf("%u", &pImage->pElements->numLines);
  94.  
  95.     pImage->pElements->pLines = (Line *)malloc(pImage->pElements->numLines* sizeof(Line));
  96.     pImage->pElements->pLines->start.x = 0;
  97.     pImage->pElements->pLines->start.y = 0;
  98.     pImage->pElements->pLines->end.x = 0;
  99.     pImage->pElements->pLines->end.y = 0;
  100.  
  101.     for (int i = 0; i < pImage->pElements->numLines; i++)
  102.     {
  103.         printf("\nPlease enter the x coord of the start point of line index %d:", i);
  104.         scanf("%d", &pImage->pElements->pLines[i].start.x);
  105.         printf("Please enter the y coord of the start point of line index %d:", i);
  106.         scanf("%d", &pImage->pElements->pLines[i].start.y);
  107.         printf("Please enter the x coord of the end point of line index %d:", i);
  108.         scanf("%d", &pImage->pElements->pLines[i].end.x);
  109.         printf("Please enter the y coord of the end point of line index %d:", i);
  110.         scanf("%d", &pImage->pElements->pLines[i].end.y);
  111.  
  112.         printf("\n s.X:%d,s.Y: %d, e.X:%d, e.Y:%d ", pImage->pElements->pLines[i].start.x, pImage->pElements->pLines[i].start.y, pImage->pElements->pLines[i].end.x, pImage->pElements->pLines[i].end.y);
  113.  
  114.  
  115.     }
  116.  
  117. }
  118. void ReportVectorGraphic(VectorGraphic* pImage)
  119. {
  120.  
  121.     printf("\nVectorGraphic Report");
  122.     for (int i = 0; i < pImage->numGraphicElements; i++){
  123.         printf("\n Reporting Graphic Element #%d",i);
  124.         printf("\n Graphic Element name: %s", pImage->pElements[i].name);
  125.         /*for (int y = 1; y <= pImage->pElements->numLines; y++){
  126.             pImage->pElements->pLines--;
  127.         }*/
  128.         for (int j = 0; j < pImage->pElements[i].numLines; j++)
  129.         {
  130.             printf("\n Line %d start x:%d", j, pImage->pElements->pLines[j].start.x);
  131.             printf("\n Line %d start y:%d", j, pImage->pElements->pLines[j].start.y);
  132.            
  133.             printf("\n Line %d end x:%d", j, pImage->pElements->pLines[j].end.x);
  134.             printf("\n Line %d end y:%d", j, pImage->pElements->pLines[j].end.y);
  135.            
  136.         }
  137.     }
  138. }
  139.  
  140. void CleanUpVectorGraphic(VectorGraphic* pImage)
  141. {
  142.     free(pImage->pElements);
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement