SoloCodigo

Programación General => C/C++ => Mensaje iniciado por: amedinadiaz en Viernes 23 de Abril de 2004, 03:15

Título: "paint" Para Ms-dos
Publicado por: amedinadiaz en Viernes 23 de Abril de 2004, 03:15
Weno os lo dejo, aun no esta terminao hay algunos fallos aun pero la base si que funciona. Concretamente falla al borrar algun elemento de la imagen, y un par de cosillas mas, pero weno os lo pongo para el que le interese, cualkier opinion sera bien recibida y posibles mejoras o ayuda aun mas!

PD: Esta en ingles, xq yo studio en Inglaterra.

Código: Text
  1.  
  2. /*****************************************************/
  3. /* Author: Antonio Medina and Fawzan Ghulam          */
  4. /* Date: 10-2-04                                     */
  5. /* EE1E2 FINAL PROJECT                               */
  6. /* DRAWING PACKAGE                                   */
  7. /*****************************************************/
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <malloc.h>
  12. #include <math.h>
  13. #define PI 3.14
  14.  
  15. struct drawing
  16. {
  17.   int width;
  18.   int height;
  19.   char** data;
  20. }A;
  21.  
  22. struct node
  23. {
  24.     char name[6];
  25.     int ID_no;
  26.     int x1;
  27.     int x2;
  28.     int y1;
  29.     int y2;
  30.     int centrex;
  31.     int centrey;
  32.     int radius;
  33.     struct node* next;
  34. };
  35. struct node* mknode(char name[6], int ID_no,int x1,int x2,int y1,int y2,int radius,int centrex,int centrey)
  36. {
  37.     struct node* np;
  38.  
  39.     np = (struct node*)malloc(sizeof(struct node));
  40.  
  41.     if (np)
  42.     {
  43.         strcpy(np->name,name);
  44.         np->ID_no=ID_no;
  45.         np->x1=x1;
  46.         np->x2=x2;
  47.         np->y1=y1;
  48.         np->y2=y2;
  49.         np->centrex=centrex;
  50.         np->centrey=centrey;
  51.         np->radius=radius;
  52.         np->next=NULL;
  53.     }
  54.  
  55.     return np;  
  56. }
  57.  
  58. struct node* append_node(struct node** head, struct node* np)
  59. {
  60.      struct node* n;
  61.  
  62.      if(*head==NULL)
  63.      *head=np;
  64.      else
  65.      {
  66.            for (n=*head; n->next!=NULL; n=n->next);
  67.            n->next=np;
  68.      }
  69.  
  70.  return np;
  71. }
  72.  
  73. void display_list(struct node* head)
  74. {
  75.      struct node* n;
  76.  
  77.      for (n=head; n!=NULL; n=n->next)
  78.      {
  79.           if (strcmp(n->name,"rect")==0)
  80.                 printf("%d: %s from %d %d to %d %d\n", n->ID_no, n->name, n->x1, n->y1, n->x2, n->y2);
  81.                
  82.           else if (strcmp(n->name,"circle")==0)
  83.                 printf("%d: %s, centre %d %d and radius %d\n", n->ID_no, n->name, n->centrex, n->centrey, n->radius);
  84.  
  85.           else if (strcmp(n->name,"line")==0)
  86.                 printf("%d: %s from %d %d to %d %d\n", n->ID_no, n->name, n->x1, n->y1, n->x2, n->y2);
  87.      }
  88. }
  89.  
  90. void commands (void)
  91. {
  92.  
  93.   printf("*********************\n");
  94.   printf("* POSSIBLE COMMANDS *\n");
  95.   printf("*********************\n");
  96.   printf(" -----------------------------------------------\n");
  97.   printf("|  - create (width) (height)                    |\n");
  98.   printf("|  - r                                          |\n");
  99.   printf("|  - clear                                      |\n");
  100.   printf("|  - invert                                     |\n");
  101.   printf("|  - line (from) (to)                           |\n");
  102.   printf("|  - rect (corner1, corner2) (corner3, corner4) |\n");
  103.   printf("|  - circle (centre) (radius)                   |\n");
  104.   printf("|  - fill (x) (y)                               |\n");
  105.   printf("|  - list                                       |\n");
  106.   printf("|  - delete (object_number)                     |\n");
  107.   printf("|  - exit                                       |\n");
  108.   printf(" -----------------------------------------------\n");
  109. }
  110.  
  111. void create(struct drawing *p1)
  112. {
  113.   int width, height;
  114.  
  115.   p1->data=(char**)(malloc(p1->width*sizeof(char*)));
  116.  
  117.   for(width=0; width<p1->width; width++)
  118.     p1->data[width]=(char*)(malloc(p1->height*sizeof(char)));
  119.  
  120.   for(width=0; width<p1->width; width++)
  121.     for(height=0; height<p1->height; height++)
  122.       p1->data[width][height]='.';
  123.  
  124. }
  125. void invert(struct drawing *p1)
  126. {
  127.   int width, height;
  128.  
  129.   for(width=0; width<p1->width; width++)
  130.     for(height=0; height<p1->height; height++)
  131.       if(p1->data[width][height]=='*')
  132.         p1->data[width][height]='.';
  133.       else if(p1->data[width][height]=='.')
  134.         p1->data[width][height]='*';
  135.  
  136. }
  137. void clear(struct drawing *p1)
  138. {
  139.   int width, height;
  140.  
  141.   for(width=0; width<p1->width; width++)
  142.     for(height=0; height<p1->height; height++)
  143.       p1->data[width][height]='.';
  144. }
  145.  
  146. void refresh(struct drawing *p1)
  147. {
  148.   int width, height;
  149.  
  150.   for(height=p1->height-1; height>=0; height--)
  151.   {
  152.     for(width=0; width<p1->width; width++)
  153.       printf("%c", p1->data[width][height]);
  154.       printf("\n");
  155.   }
  156. }
  157.  
  158. void line(struct drawing *p1, int x1, int x2, int y1, int y2, int del)
  159. {
  160.   int dx, dy, i,j;
  161.    
  162.                 if((x1<0)||(x2<0)||(y1<0)||(y2<0))
  163.                     printf("Error. You can't draw a negative line\n");
  164.                else if((x1>p1->width)||(x2>p1->width)||(y1>p1->height)||(y2>p1->height))
  165.                    printf("Error. Your line has excedeed the image limits\n");
  166.              else if ((y1<x1)||(y2<x2))
  167.                     printf("Error. You can't draw that shape.\n");
  168.      else
  169.      {
  170.     dx=x2-x1;
  171.     dy=y2-y1;
  172.  
  173.     if(dx>p1->width||dy>p1->height)
  174.       printf("Your dimensions are too large for the grid");
  175.     else
  176.     {
  177.  
  178.     if(dx>dy)
  179.     {
  180.       if(dx>0)
  181.       {
  182.         for(i=0; i<dx; i++)
  183.         {
  184.            j=(((i*dy)/dx)+0.5);
  185.            p1->data[x1+i][y1+j]='*';
  186.         }
  187.     }
  188.     else
  189.     {
  190.        for(i=0; i<dx; i--)
  191.        {
  192.          j=(((i*dy)/dx)+0.5);
  193.          p1->data[x1+i][y1+j]='*';
  194.        }
  195.     }
  196.        }
  197.        else
  198.        {
  199.       if(dy>0)
  200.       {
  201.         for(j=0; j<dy; j++)
  202.          {
  203.            i=(((j*dx)/dy)+0.5);
  204.            p1->data[x1+i][y1+j]='*';
  205.         }
  206.       }
  207.       else
  208.       {
  209.         for(j=0; j<dy; j--)
  210.          {
  211.           i=(((j*dx)/dy)+0.5);
  212.           p1->data[x1+i][y1+j]='*';
  213.         }
  214.       }
  215.     }
  216.     }
  217.  
  218.   }
  219. }
  220.  
  221. void circle(struct drawing *p1, int x1, int y1, int r, int del)
  222. {
  223.   int i, j;
  224.   float deg, theta;
  225.  
  226.  
  227.  
  228.   if((x1<0)||(y1<0))
  229.     printf("Error. You can't draw a negative circle\n");
  230.   else if((x1>p1->width)||(y1>p1->height))
  231.     printf("Error. Your circle has excedeed the image limits\n");
  232.  
  233.   else
  234.   {
  235.  
  236.     for(deg=0; deg<=360; deg=deg+5)
  237.     {
  238.       theta=((deg*PI)/180);
  239.    
  240.       i=x1+r*cos(theta)+0.5;
  241.       j=y1+r*sin(theta)+0.5;
  242.  
  243.       p1->data[i][j]='*';
  244.     }
  245.   }
  246. }
  247. void rect(struct drawing *p1, int x1, int x2, int y1, int y2, int del)
  248. {
  249.   int dx, dy, i;
  250.  
  251.  
  252.   if((x1<0)||(x2<0)||(y1<0)||(y2<0))
  253.     printf("Error. You can't draw a negative rectangle\n");
  254.   else if((x1>p1->width)||(x2>p1->width)||(y1>p1->height)||(y2>p1->height))
  255.     printf("Error. Your rect has excedeed the image limits\n");
  256.   else if ((y1<x1)||(y2<x2))
  257.     printf("Error. You can't draw that shape.\n");
  258.  
  259.   else
  260.   {
  261.  
  262.   dx=x2-x1;
  263.   dy=y2-y1;
  264.  
  265.   if(dx>p1->height||dy>p1->width)
  266.     printf("Your dimensions are too large for the grid");
  267.   else
  268.   {
  269.  
  270.   if((dy>0)&&(dx>0))
  271.   {
  272.  
  273.     for (i=0; i<dx; i++)
  274.       p1->data[x1+i][y1]='*';
  275.  
  276.         for (i=0; i<dy; i++)
  277.             p1->data[x1][y1+i]='*';
  278.  
  279.         for (i=dx;i>=0; i--)
  280.       p1->data[x2-i][y2]='*';
  281.  
  282.     for (i=dy;i>=0; i--)
  283.       p1->data[x2][y2-i]='*';
  284.  
  285.   }
  286.   else if((dy<0)&&(dx<0))  
  287.    {
  288.       for (i=dx; i<=0; i++)
  289.         p1->data[x1+i][y1]='*';
  290.  
  291.       for (i=dy; i<=0; i++)
  292.         p1->data[x1][y1+i]='*';
  293.  
  294.       for (i=dx;i<=0; i++)
  295.         p1->data[x2-i][y2]='*';
  296.  
  297.       for (i=dy;i<=0; i++)
  298.         p1->data[x2][y2-i]='*';
  299.  
  300.   }
  301.     else if(dy>0&&dx<0)
  302.     {
  303.  
  304.       for (i=dx; i<=0; i++)
  305.         p1->data[x1+i][y1]='*';
  306.  
  307.       for (i=0; i<dy; i++)
  308.         p1->data[x1][y1+i]='*';
  309.  
  310.       for (i=dx;i<=0; i++)
  311.         p1->data[x2-i][y2]='*';
  312.  
  313.       for (i=dy;i>=0; i--)
  314.         p1->data[x2][y2-i]='*';
  315.  
  316.     }
  317.     else if((dy<0)&&(dx>0))
  318.     {
  319.  
  320.       for (i=0; i<=dx; i++)
  321.         p1->data[x1+i][y1]='*';
  322.  
  323.       for (i=dy; i<=0; i++)
  324.         p1->data[x1][y1+i]='*';
  325.  
  326.       for (i=dx;i>=0; i--)
  327.         p1->data[x2-i][y2]='*';
  328.  
  329.       for (i=dy;i<=0; i++)
  330.         p1->data[x2][y2-i]='*';
  331.     }
  332.   }
  333. }
  334.  
  335. }
  336.  
  337. void fill(int x, int y, struct drawing *p1)
  338. {    
  339.  
  340.       p1->data[x][y]='*';
  341.  
  342.       if((x+1>=0)&&(x+1<p1->width))
  343.         if((y>=0)&&(y<p1->height))
  344.           if(p1->data[x+1][y]=='.')
  345.             fill(x+1,y, p1);
  346.  
  347.       if((x-1>=0)&&(x-1<p1->width))
  348.         if((y>=0)&&(y<p1->height))
  349.           if(p1->data[x-1][y]=='.')
  350.             fill(x-1,y, p1);
  351.  
  352.       if((x>=0)&&(x<p1->width))
  353.         if((y+1>=0)&&(y+1<p1->height))
  354.           if(p1->data[x][y+1]=='.')
  355.             fill(x,y+1, p1);
  356.  
  357.       if((x>=0)&&(x<p1->width))
  358.         if((y-1>=0)&&(y-1<p1->height))
  359.           if(p1->data[x][y-1]=='.')
  360.             fill(x,y-1, p1);
  361.  
  362.  
  363. }
  364. int delete_object(struct node** head, int ID_no, struct drawing *p1)
  365. {
  366.     struct node* n;
  367.     struct node* np;
  368.     int del=1;
  369.  
  370.      if (*head==NULL)
  371.            return 0;
  372.      else if (ID_no==(*head)->ID_no)
  373.      {
  374.            np=(*head);
  375.            if(strcmp(np->name,"circle")==0)
  376.                     circle(&A,np->centrex,np->centrey,np->radius,del);
  377.      }
  378.      if(strcmp(np->name,"line")==0)
  379.            line(&A,np->x1,np->y1,np->x2,np->y2,del);
  380.              
  381.      if(strcmp(np->name,"rect")==0)
  382.      {
  383.            rect(&A,np->x1,np->y1,np->x2,np->y2,del);
  384.  
  385.            *head=(*head)->next;
  386.            free(np);
  387.            return 1;
  388.      }
  389.      else
  390.      {
  391.            for(n=*head; (n->ID_no!=ID_no)&&(n->next!=NULL); n=n->next)
  392.                        np=n;
  393.  
  394.            if(n->ID_no==ID_no)
  395.            {
  396.                        if(strcmp(n->name,"circle")==0){
  397.                                  circle(&A,n->centrex,n->centrey,n->radius,del);
  398.            }
  399.            else if(strcmp(n->name,"line")==0)
  400.            {
  401.                        line(&A,n->x1,n->y1,n->x2,n->y2,del);
  402.            }
  403.            else if(strcmp(n->name,"rect")==0)
  404.            {
  405.               rect(&A, n->x1, n->y1,n->x2,n->y2,del);
  406.            }
  407.             np->next=n->next;
  408.             free(n);
  409.             return 1;
  410.       }
  411.  else
  412.  return 0;
  413.  }
  414. }
  415.  
  416. int main (void)
  417. {
  418.   char a[6];
  419.   struct drawing A;
  420.   struct node *n;
  421.   struct node *head=NULL;
  422.   int flag=0, i ,j, k=1, x1=0, x2=0, y1=0, y2=0, r, x=0, y=0, del=0, ID_no=0;
  423.  
  424.   printf("DRAWING PACKAGE FOR MS-DOS 1.0\n");
  425.   printf("------------------------------\n");
  426.   printf("\n");
  427.   commands();
  428.  
  429.   printf("Enter your command:\n");
  430.  
  431.   while(strcmp(a, "exit")!=0)
  432.   {
  433.     scanf("%s", a);
  434.  
  435.     if(strcmp(a, "create")==0)
  436.     {
  437.       if(flag==1)
  438.         printf("Error. You can't create more than one image per session.\n");
  439.    
  440.       else
  441.       {
  442.         printf("Enter width:");
  443.         scanf("%d", &A.width);
  444.         printf("Enter height:");
  445.         scanf("%d", &A.height);
  446.         create(&A);
  447.         flag++;
  448.       }
  449.     }
  450.     else if(strcmp(a, "r")==0)
  451.       refresh(&A);
  452.     else if(strcmp(a, "invert")==0)
  453.       invert(&A);
  454.     else if(strcmp(a, "exit")==0)
  455.     {
  456.       printf("\n");
  457.       printf("Thanks for using this program\n");
  458.       printf("-----------------------------\n");
  459.       return 0;
  460.     }
  461.     else if(strcmp(a, "clear")==0)
  462.       clear(&A);
  463.      
  464.     else if(strcmp(a, "fill")==0)
  465.     {
  466.       printf("Enter starting point:");
  467.       scanf("%d %d", &i, &j);
  468.       fill(i, j, &A);
  469.     }
  470.     else if(strcmp(a, "list")==0)
  471.             display_list(head);
  472.        
  473.         else if(strcmp(a, "delete")==0)
  474.         {
  475.             printf("Enter ID:");
  476.             scanf("%d", &ID_no);
  477.  
  478.             if(delete_object(&head,ID_no,&A))
  479.                         printf("Object no.%d successfully deleted\n", ID_no);
  480.  
  481.         }    
  482.     else if((strcmp(a, "line"))||(strcmp(a, "circle"))||(strcmp(a, "rect"))==0)
  483.     {
  484.         if(strcmp(a, "line")==0)
  485.         {
  486.             printf("Insert point 1\n");
  487.             scanf("%d %d", &x1,&y1);
  488.             printf("Insert point 2\n");
  489.                 scanf("%d %d", &x2,&y2);
  490.                 line(&A, x1, x2, y1, y2, del);
  491.             }
  492.       else if(strcmp(a, "circle")==0)
  493.       {
  494.                printf("Enter Coordinates:");
  495.                scanf("%d %d", &x1, &y1);
  496.                printf("Enter Radius:");
  497.                scanf("%d", &r);
  498.           circle(&A, x1, y1, r, del);
  499.           }
  500.             else if(strcmp(a, "rect")==0)
  501.             {
  502.            printf("Enter x1, y1:");
  503.          scanf("%d %d", &x1, &y1);
  504.          printf("Enter x2, y2:");
  505.          scanf("%d %d", &x2, &y2);
  506.                 rect(&A, x1, x2, y1, y2, del);
  507.             }
  508.            
  509.             n=mknode(a, k, x1, x2, y1, y2, r, x, y);
  510.             append_node(&head, n);
  511.             k++;
  512.     }
  513.     else
  514.       printf("Error. That is not a possible command.\n");
  515.  
  516.   }
  517.  
  518.   return 0;
  519. }
  520.  
Título: Re: "paint" Para Ms-dos
Publicado por: nicokiki en Viernes 23 de Abril de 2004, 03:52
Lo intente usar pero no se como.
Lo q si es q me tiro 7 WARNINGS

Salu2!!!!!
Título: Re: "paint" Para Ms-dos
Publicado por: martin campos quintero en Lunes 26 de Abril de 2004, 21:54
Kompañero, komo ke debes explikar mejor komo usarlo, le meti circle (15,3) y me marko ke la imagen estaba fuera del rango, o algo asi, komo ke das los limites en tu programa no???
Título: Re: "paint" Para Ms-dos
Publicado por: Blag en Lunes 26 de Abril de 2004, 22:41
Muy buen trabajo amedinadiaz  :smartass:  Aunque como mencionas tienes algunos pequeños bugs.....por ejemplo con el comando List, si no le doy ninguno parametro y mando el R a la pantalla, se crea un bucle infinito y hay que cerrar la aplicación a la fuerza......Además, me parece que deberías dar la oportunidad de limpiar toda la pantalla, y de mostrar el menú cada vez que sea necesario, no crees?

Estoy adjuntando un pequeño programa de gráficos que hice utilizando el Spanish Viking 2.6, espero que me puedas dar tu opinión  :hola:
Es bastante simple, pero eso se debe a las limitaciones del lenguaje.

Saludos,

Blag  :devil:
Título: Re: "paint" Para Ms-dos
Publicado por: The Black Boy en Lunes 26 de Abril de 2004, 22:50
:smartass:    buen trabajo    :smartass: