home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / graphics / gl.pak / XFORM.C < prev   
Encoding:
C/C++ Source or Header  |  1989-03-31  |  4.0 KB  |  145 lines

  1. #ifndef lint
  2. static char sccsid[] = "@(#) xform.c 5.1 89/02/20";
  3. #endif
  4.  
  5. /*
  6.  *    Copyright (c) David T. Lewis 1988
  7.  *    All rights reserved.
  8.  *
  9.  *    Permission is granted to use this for any personal noncommercial use.
  10.  *    You may not distribute source or executable code for profit, nor
  11.  *    may you distribute it with a commercial product without the written
  12.  *    consent of the author.  Please send modifications to the author for
  13.  *    inclusion in updates to the program.  Thanks.
  14.  */
  15.  
  16. /* Various geometric transformations.                    */
  17.  
  18. #include "config.h"
  19. #if MIX_C
  20. #else
  21. #include <math.h>
  22. #endif /* MIX_C */
  23. #include "bitmaps.h"
  24. #include "graphics.h"
  25.  
  26. extern struct GL_graphics graphics;
  27. extern long longsin(), longcos();
  28.  
  29. /* I_rot_2d_s rotates a point about the origin in two dimensions.    */
  30. /* It operates on integer values, and is used used for text rotations    */
  31. /* in normalized 2-D coordinate space.                    */
  32. /* Since this is intended for text, the y values will be scaled        */
  33. /* according to the value of graphics.aspect_ratio.            */
  34. /* Note that in this coordinate system, the angle is "backwards",    */
  35. /* since the origin is in the upper left of the screen.  Since we    */
  36. /* normally want a positive angle to appear counterclockwise on the    */
  37. /* screen, the angle parameter will normally be negated prior to    */
  38. /* calling this routine.                        */
  39.  
  40. int i_rot_2d_s(x, y, theta)
  41. int *x, *y;
  42. double theta;
  43. {
  44.  
  45. #if NOFLOAT
  46.     long x_temp = (long)(*x);
  47.     long y_temp = (long)(*y);
  48.  
  49.     /* Temporary variable is used to force explicit order of    */
  50.     /* integer calculations.  This probably isn't really needed    */
  51.     /* with most compilers, but better to be safe than sorry.    */
  52.  
  53.     long ltemp;
  54.  
  55.     /* Scale the (floating point) aspect ratio up to a reasonable    */
  56.     /* integer value.                        */
  57.  
  58.     long lratio = (long)(graphics.aspect_ratio * 16.0);
  59.  
  60.     /* Pre-calculate the sin and cos values (save calls to trig    */
  61.     /* routines.)                            */
  62.  
  63.     long lsinval = longsin(theta);
  64.     long lcosval = longcos(theta);
  65.  
  66.     ltemp = y_temp * lsinval;
  67.     ltemp /= 16;    /* Scale down first to avoid overflow.        */
  68.     ltemp *= lratio;
  69.  
  70.     *x = (int)((x_temp * lcosval - ltemp) / TRIG_SCALE);
  71.  
  72.     ltemp = x_temp * lsinval;
  73.     ltemp /= lratio;
  74.     ltemp *= 16;    /* Scale it back.    */
  75.  
  76.     *y = (int)((ltemp + y_temp * lcosval) / TRIG_SCALE);
  77.  
  78. #else
  79.     double x_temp = (double)(*x);
  80.     double y_temp = (double)(*y);
  81.  
  82.     /* Pre-calculate the sin and cos values (save calls to trig    */
  83.     /* routines.)                            */
  84.  
  85.     double sinval = sin(theta);
  86.     double cosval = cos(theta);
  87.  
  88.     *x = (int)(x_temp * cosval
  89.         - y_temp * sinval * graphics.aspect_ratio);
  90.     *y = (int)(x_temp * sinval / graphics.aspect_ratio
  91.         + y_temp * cosval);
  92.  
  93. #endif /* NOFLOAT */
  94.  
  95.     return(0);
  96. }
  97.  
  98. /* I_rot_2d rotates a point about the origin in two dimensions.    */
  99. /* It operates on integer values, and is used used for rotations in    */ 
  100. /* normalized 2-D coordinate space.                    */
  101. /* Note that in this coordinate system, the angle is "backwards",    */
  102. /* since the origin is in the upper left of the screen.  Since we    */
  103. /* normally want a positive angle to appear counterclockwise on the    */
  104. /* screen, the angle parameter will normally be negated prior to    */
  105. /* calling this routine.                        */
  106. /* This routine does not account for aspect ratio.            */
  107.  
  108. int i_rot_2d(x, y, theta)
  109. int *x, *y;
  110. double theta;
  111. {
  112.  
  113. #if NOFLOAT
  114.     long x_temp = (long)(*x);
  115.     long y_temp = (long)(*y);
  116.  
  117.     /* Pre-calculate the sin and cos values (save calls to trig    */
  118.     /* routines.)                            */
  119.  
  120.     long lsinval = longsin(theta);
  121.     long lcosval = longcos(theta);
  122.  
  123.     *x = (int)((x_temp * lcosval - y_temp * lsinval)
  124.         /TRIG_SCALE);
  125.     *y = (int)((x_temp * lsinval + y_temp * lcosval)
  126.         /TRIG_SCALE);
  127. #else
  128.     double x_temp = (double)(*x);
  129.     double y_temp = (double)(*y);
  130.  
  131.     /* Pre-calculate the sin and cos values (save calls to trig    */
  132.     /* routines.)                            */
  133.  
  134.     double sinval = sin(theta);
  135.     double cosval = cos(theta);
  136.  
  137.     *x = (int)(x_temp * cosval - y_temp * sinval);
  138.     *y = (int)(x_temp * sinval + y_temp * cosval);
  139. #endif /* NOFLOAT */
  140.  
  141.     return(0);
  142. }
  143.  
  144.  
  145.