home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / xfedor.zip / XFree86 / src / xfedor / ed_rotate.c < prev    next >
C/C++ Source or Header  |  1999-03-02  |  7KB  |  223 lines

  1. /* Copyright 1989 GROUPE BULL -- See licence conditions in file COPYRIGHT */
  2. #include <stdio.h>
  3. #include "couche.h"
  4. #include "clientimage.h"
  5. #include "fedor.h"
  6. #include "style.h"    /* NORMAL .. */
  7.  
  8. extern fedchar cartrav;
  9. extern ClientImage * bitmapsave ;
  10. extern Xleft,Baseliney ;
  11. extern BackColor ;
  12.  
  13. static int dx,dy,alf ;
  14.  
  15. #define PASROT 5     /* pas de la rotation */
  16.  
  17.      /* TABLEAUX CONTENANT 1024 * LE SINUS ET LE COSINUS DES ANGLES DE
  18.         0 A 45 DEGRES PAR PAS DE 5 DEGRES */
  19. static int Sin[] = {0, 89, 178, 265, 350, 433, 512, 587, 658, 724} ;
  20. static int Cos[] = {1024,1020,1008,989,962,928,887,839,784,724} ;
  21.  
  22. /*********************** procedure CHOIX_COSSIN **********************/
  23. /* On passe un angle compris entre -180 et 180 degres et la procedure
  24.    rend le cosinus et le sinus de l'angle */
  25.  
  26. static choix_cossin(angle, cosinus, sinus)
  27.     short angle ;     /* angle de le rotation : -180 < angle < 180 */
  28.     int *cosinus ;     /* cosinus de la rotation */ 
  29.     int *sinus ;       /* sinus de la rotation */
  30.  
  31.    { int indtab ; 
  32.    
  33.      if (angle < 0) indtab = (360 + angle) / PASROT;
  34.                else indtab = angle / PASROT;
  35.              /* ANGLE COMPRIS ENTRE 0 ET 49 */
  36.      if (indtab < 10) { *cosinus = Cos[indtab] ;
  37.                         *sinus = Sin[indtab] ;
  38.                       }
  39.              /* ANGLE COMPRIS ENTRE 50 ET 94 */
  40.       else if (indtab < 19) { *cosinus = Sin[18 - indtab] ;
  41.                               *sinus = Cos[18 - indtab] ;
  42.                            }
  43.              /* ANGLE COMPRIS ENTRE 95 ET 139 */
  44.       else if (indtab < 28) { *cosinus = - Sin[indtab - 18] ;
  45.                               *sinus = Cos[indtab - 18] ;
  46.                            }
  47.              /* ANGLE COMPRIS ENTRE 140 ET 184 */
  48.       else if (indtab < 37) { *cosinus = - Cos[36 - indtab] ;
  49.                               *sinus = Sin[36 - indtab] ;
  50.                             }
  51.              /* ANGLE COMPRIS ENTRE 185 ET 229 */
  52.       else if (indtab < 46) { *cosinus = - Cos[indtab - 36] ;
  53.                               *sinus = - Sin[indtab - 36] ;
  54.                             }
  55.              /* ANGLE COMPRIS ENTRE 230 ET 274 */
  56.       else if (indtab < 55) { *cosinus = - Sin[54 - indtab] ;
  57.                               *sinus = - Cos[54 - indtab] ;
  58.                             }
  59.              /* ANGLE COMPRIS ENTRE 275 ET 319 */
  60.       else if (indtab < 64) { *cosinus = Sin[indtab - 54] ; 
  61.                               *sinus = - Cos[indtab - 54] ;
  62.                             }
  63.              /* ANGLE COMPRIS ENTRE 320 ET 359 */
  64.       else { *cosinus = Cos[72 - indtab] ;
  65.              *sinus = - Sin[72 - indtab] ;
  66.            }
  67. }
  68.  
  69. static Rotate(xcenter,ycenter)
  70.     int xcenter,ycenter ;
  71.  
  72. {    
  73.     int cosinus,sinus ;
  74.     int color ;
  75.     register short x,y,x1,y1,x2,y2,x3,y3,x4,y4,xa,xb,ya,yb ;
  76.     register short     supgx = Xleft,
  77.             supgy = Baseliney + cartrav.up,
  78.             infdx = Xleft + cartrav.hsize,
  79.             infdy = Baseliney + cartrav.down ;
  80.     
  81.     choix_cossin(alf,&cosinus,&sinus);
  82.  
  83.     Rast_Off(&bitmapsave,BackColor) ;
  84.  
  85.     for(x = supgx; x< infdx ; x++) 
  86.         for(y= supgy; y < infdy ; y++) {
  87.           color = Rast_Inq(cartrav.image,x,y) ;
  88.           if (color != BackColor){
  89.         x2 = x - xcenter ;
  90.         y2 = y - ycenter ;
  91.         x1 = ((x2*cosinus - y2*sinus)>>10) + xcenter;
  92.         y1 = ((x2*sinus + y2*cosinus)>>10) + ycenter;
  93.         Rast_Pix(bitmapsave,x1,y1,color);
  94.           }                
  95.         }   
  96.          
  97.  
  98.     Rast_Copy(bitmapsave,&cartrav.image);
  99.     
  100.     /* il faut recalculer la boundingbox */
  101.     /* on calcule les limites theoriques */
  102.     x1 = (((supgx-xcenter)*cosinus - 
  103.         (supgy-ycenter)*sinus)>>10) + xcenter;
  104.     y1 = (((supgx-xcenter)*sinus +  
  105.         (supgy-ycenter)*cosinus)>>10) + ycenter;
  106.     x2 = (((infdx-xcenter)*cosinus - 
  107.         (supgy-ycenter)*sinus)>>10) + xcenter;
  108.     y2 = (((infdx-xcenter)*sinus +  
  109.         (supgy-ycenter)*cosinus)>>10) + ycenter;
  110.     x3 = (((infdx-xcenter)*cosinus - 
  111.         (infdy-ycenter)*sinus)>>10) + xcenter;
  112.     y3 = (((infdx-xcenter)*sinus +  
  113.         (infdy-ycenter)*cosinus)>>10) + ycenter;
  114.     x4 = (((supgx-xcenter)*cosinus - 
  115.         (infdy-ycenter)*sinus)>>10) + xcenter;
  116.     y4 = (((supgx-xcenter)*sinus +  
  117.         (infdy-ycenter)*cosinus)>>10) + ycenter;
  118.      
  119.     xa = Min(Min(x1,x2),Min(x3,x4)); if (xa<0) xa=0;
  120.     xb = Max(Max(x1,x2),Max(x3,x4)); if (xb>128) xb=128 ;
  121.     ya = Min(Min(y1,y2),Min(y3,y4)); if (ya<0) ya=0;
  122.     yb = Max(Max(y1,y2),Max(y3,y4)); if (yb>128) yb=128 ;
  123.  
  124.     /* on verifie le contenu de cette bbox(xa,ya,xb,yb) */
  125.     x = xa ;
  126.     while ( Colonne_color(cartrav.image,x,ya,yb,BackColor) 
  127.            && (x <=xb)) x++ ;
  128.     if (x > xb) {
  129.             cartrav.hsize = 0 ;
  130.             cartrav.up = 0 ;
  131.             cartrav.down = 0 ;
  132.             Xleft = 0 ;
  133.             return ;
  134.     }
  135.     Xleft = x ;
  136.     x = xb ;
  137.     while ( Colonne_color(cartrav.image,x,ya,yb,BackColor) ) x-- ;
  138.     cartrav.hsize = x - Xleft + 1;
  139.  
  140.     y = ya ;
  141.     while ( Ligne_color(cartrav.image,y,xa,xb,BackColor) ) y ++ ;
  142.     cartrav.up = y - Baseliney;
  143.  
  144.     y = yb ;
  145.     while ( Ligne_color(cartrav.image,y,xa,xb,BackColor) ) y -- ;
  146.     cartrav.down = y - Baseliney + 1 ;
  147.  
  148.     MontrerCarTrav() ;
  149. }
  150.  
  151. Autom_rotate (pev)
  152.     myEvent * pev ; 
  153. {  static int x,y ;
  154.    static short etat ;
  155.    float tang ;    
  156.    char s[5] ;
  157.  
  158.     switch (pev->type) {
  159.     case EnterZone : Afficher_boutons("CENTER","CENTER");
  160.              x = -1 ;
  161.              break ;
  162.     case ButtonPressed : if (x == -1 ) {
  163.                 Afficher_boutons("ANGLE","ANGLE");
  164.                     x = pev->x ; 
  165.                     y = pev->y ; 
  166.                      stylesouris(LIGNE,x,y);
  167.                  }
  168.                  etat = 1 ;
  169.                  break ;
  170.     case ButtonReleased : stylesouris(NORMAL,0,0);
  171.                   Afficher_boutons("CENTER","CENTER");
  172.                   if (x != -1) {Dodo() ;
  173.                         Rotate(convert(x),
  174.                                convert(y));
  175.                         /* alf global */
  176.                   }
  177.                   x = -1 ;
  178.                   etat = 0 ;
  179.                   break ;
  180.     case MoveMouse : if (etat) { /* enfonce */
  181.                  dx = pev->x - x ;
  182.                  if (dx == 0) alf = 90 ;
  183.                 else {
  184.                        dy = pev->y - y ;
  185.                   tang = (float) (dy) / dx ;
  186.  
  187.                   if (tang < 0) tang = -tang ;
  188.                   if (tang < 0.043658) alf = 00; else
  189.                   if (tang < 0.131643) alf = 05; else
  190.                   if (tang < 0.221678) alf = 10; else
  191.                   if (tang < 0.315274) alf = 15; else
  192.                   if (tang < 0.414179) alf = 20; else
  193.                   if (tang < 0.520522) alf = 25; else
  194.                   if (tang < 0.637011) alf = 30; else
  195.                   if (tang < 0.767250) alf = 35; else
  196.                   if (tang < 0.916230) alf = 40; else
  197.                   if (tang < 1.091174) alf = 45; else
  198.                   if (tang < 1.303042) alf = 50; else
  199.                   if (tang < 1.569428) alf = 55; else
  200.                   if (tang < 1.920603) alf = 60; else
  201.                   if (tang < 2.413618) alf = 65; else
  202.                   if (tang < 3.170559) alf = 70; else
  203.                   if (tang < 4.508571) alf = 75; else
  204.                   if (tang < 7.589500) alf = 80; else
  205.                   if (tang < 22.844478)alf = 85; else
  206.                                alf = 90; 
  207.                 }
  208.                      if (dx < 0) alf = 180 - alf ;
  209.                 if (dy < 0) alf = (-alf) ;
  210.                 sprintf(s,"%4d",alf) ;
  211.                 Afficher_sos(s) ;
  212.              }    
  213.              break ;                
  214.     case LeaveZone : etat = 0 ;
  215.                  Afficher_numview(-1,-1) ; /* restaure le gris */
  216.              stylesouris(NORMAL,0,0);
  217.              Afficher_boutons("","");
  218.              Afficher_sos("SOS");
  219.              break ;
  220.     case CloseWindow : break ;                
  221.     }
  222.