Guest User

postion boxes optimally with fix gaps

a guest
Jun 27th, 2021
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.30 KB | None | 0 0
  1. #define POSITION_BOXES
  2.  
  3. #ifdef POSITION_BOXES
  4.  
  5. #include <stdio.h>
  6. #include <fcntl.h>
  7.  
  8. struct
  9. {
  10.  int x,y,xdim,ydim;
  11.  unsigned char text;
  12. }boxes[100];
  13.  
  14. signed int boxptr=0;
  15.  
  16. int main(void)
  17. {
  18.   enum
  19.   {
  20.    up,down,left,right,grewx,grewy
  21.   }LASTSTEP;
  22.  
  23.  unsigned char screen[60][40],collide[60][40];
  24.   int x,y;
  25.    int n=0;
  26.    y=0;
  27.    while ( y<40 )
  28.    {
  29.     x=0;
  30.     while ( x<60 ) screen[x][y]=collide[x][y]=0,x++;     y++;
  31.    }
  32.  
  33.    boxes[n].x=boxes[n].y=boxes[n].xdim=boxes[n].ydim=0;  
  34.  
  35.    while (1)
  36.    {
  37.     if ( kbhit())
  38.     {
  39.      switch(getch())
  40.      {
  41.       case 'x': if ( boxes[boxptr].xdim<15)
  42.         boxes[boxptr].xdim++; LASTSTEP=grewx; break;
  43.       case 'y':if ( boxes[boxptr].ydim<15)
  44.         boxes[boxptr].ydim++;  LASTSTEP=grewy;break;
  45.       case 's': if ( boxes[boxptr].xdim>0)
  46.         boxes[boxptr].xdim--;  break;
  47.    case 'a': if ( boxes[boxptr].ydim>0)
  48.         boxes[boxptr].ydim--;  break;
  49.    case 'u': if ( boxes[boxptr].y>0)
  50.          boxes[boxptr].y--; LASTSTEP=up;break;
  51.   case 'j': if ( boxes[boxptr].y+boxes[boxptr].ydim<39)
  52.          boxes[boxptr].y++;LASTSTEP=down; break;
  53.   case 'h': if ( boxes[boxptr].x>0)
  54.          boxes[boxptr].x--; LASTSTEP=left;break;
  55.   case 'k': if ( boxes[boxptr].x+boxes[boxptr].xdim<59)
  56.          boxes[boxptr].x++; LASTSTEP=right;break;
  57.   case 'n': boxptr++;   boxes[n].x=boxes[n].y=boxes    [n].xdim=boxes[n].ydim=0; break;
  58.  
  59.   case 'q': goto writeout;
  60.      }
  61.     }
  62.  
  63.         y=0;
  64.    while ( y<40 )
  65.    {
  66.     x=0;
  67.     while ( x<60 ) screen[x][y]=' ',collide[x][y]=255,x++;
  68.     y++;
  69.    }
  70.  
  71.    n=0;
  72.    while ( n < boxptr+1)
  73.    {
  74.     int dimc_x,dimc_y;
  75.      
  76.      dimc_y=boxes[n].ydim;
  77.  
  78.      while ( dimc_y>=0)
  79.      {      
  80.        dimc_x=boxes[n].xdim;      
  81.  while ( dimc_x >= 0 )
  82.  {
  83.  
  84.     if ( collide[boxes[n].x+dimc_x][boxes[n].y+dimc_y]==255||collide[boxes[n].x+dimc_x][boxes[n].y+dimc_y]==n)
  85.      screen[boxes[n].x+dimc_x][boxes[n].y+dimc_y]=177,
  86.      collide[boxes[n].x+dimc_x][boxes[n].y+dimc_y]=n;
  87.      else
  88.      {
  89.      printf("\a");
  90.       switch(LASTSTEP)
  91.       {
  92.        case up: boxes[n].y++;break;
  93.        case down:boxes[n].y--;break;
  94.        case left:boxes[n].x++;break;
  95.        case right:boxes[n].x--;break;
  96.        case grewx: boxes[n].xdim--; break;
  97.        case grewy: boxes[n].ydim--; break;
  98.       }
  99.       dimc_x=0;
  100.       dimc_y=boxes[n].ydim+1;
  101.       goto repeat;
  102.      }
  103.  
  104.     dimc_x--;
  105.     }
  106.      repeat:
  107.        dimc_y--;
  108.       }    
  109.  
  110.     n++;  
  111.    }      
  112.  
  113.    system("cls");
  114.    y=0;
  115.    while ( y < 40)
  116.    {
  117.     x=0;
  118.     while ( x<60) printf("%c%c%c%c",screen[x][y],screen[x+1][y],screen[x+2][y],screen[x+3][y]),x+=4;
  119.      printf("\n");
  120.     y++;
  121.    }
  122.    
  123.    
  124.    }
  125.  
  126.   writeout:
  127.    
  128.     (0);
  129.     FILE *output=fopen("posdata","wb");
  130.        fwrite(&boxptr,sizeof(signed int),1,output);
  131.       fwrite(boxes, sizeof(boxes),1,output);
  132.     fclose(output);
  133.  
  134. }
  135.  
  136. //#Else
  137. #else
  138.  
  139.  
  140. #include <stdio.h>
  141. #include <fcntl.h>
  142.  
  143.  
  144. #define XSPACING 10
  145. #define YSPACING 10
  146.  
  147. struct
  148. {
  149.  int x,y,xdim,ydim;
  150.  unsigned char text;
  151. }boxes[100],swap;
  152.  
  153. signed int boxc=0;
  154.  
  155.  
  156. void sort_boxes(int num)
  157. {
  158.  int n=0,n2=0;
  159.  
  160.   n=1;
  161.  while ( n<num-1)
  162.  {
  163.     n2=n;
  164.     while ( n2>n)
  165.     {
  166.      if ( boxes[n2].x+boxes[n2].y*60 < boxes[n2-1].x+boxes[n2-1].y*60 )
  167.       swap=boxes[n2];
  168.       boxes[n2]=boxes[n2-1];
  169.       boxes[n2-1]=swap;
  170.         n2--;
  171.      }
  172.      n++;  
  173.      } 
  174.    
  175.    
  176. }
  177.  
  178.  
  179. int main(void)
  180. {
  181.     int x_off=0,y_off=0;
  182.     int old_x=0,old_y=0;
  183.  
  184.     FILE *input=fopen("posdata","rb");
  185.        fread(&boxc,sizeof(signed int),1,input);
  186.       fread(boxes, sizeof(boxes[0]),boxc,input);
  187.     fclose(input);
  188.      
  189.            
  190.      int n=0;
  191.      sort_boxes(boxc);
  192.  
  193.      while ( n < boxc)
  194.      {
  195.       int x=0,y=0;
  196.      while (y<40)
  197.      {
  198.       x=0;
  199.       while(x<60)
  200.       {
  201.         if ( x==boxes[n].x&& y==boxes[n].y)
  202.         {
  203.             while ( (x+x_off-old_x)/XSPACING >0)old_x+=XSPACING,printf("%d nach rechts\n",XSPACING);
  204.             while ( (y+y_off-old_y)/XSPACING >0)old_y+=YSPACING;printf("%d nach unten\n",YSPACING);
  205.          printf("Kiste mit der Groesse %d %d\n",boxes[n].xdim,boxes[n].ydim);
  206.             old_x=boxes[n].x,old_y=boxes[n].y;
  207.            
  208.             x_off+=x-old_x-boxes[n].xdim%XSPACING;
  209.             y_off+=x-old_y-boxes[n].ydim%YSPACING;
  210.            
  211.           }
  212.         x++;
  213.       }
  214.      
  215.       y++; 
  216.      }
  217.      n++;
  218.    }
  219. }
  220. #endif
  221.  
Advertisement
Add Comment
Please, Sign In to add comment