home *** CD-ROM | disk | FTP | other *** search
/ MS DOS Archives 1 / MS-DOS_Archives_Volume_One_Walnut_Creek.iso / msdos / graphics / grafxlib.arc / HALOFAKE.C < prev    next >
C/C++ Source or Header  |  1987-08-31  |  6KB  |  294 lines

  1. /*
  2.  * grafix --- halofake.c
  3.  *
  4.  * implement halo-like transformations and calls
  5.  *
  6.  * Written 4/87 by Scott Snyder (ssnyder@romeo.caltech.edu or @citromeo.bitnet)
  7.  *
  8.  */
  9.  
  10. #include "macros.h"
  11. #include "graf.h"
  12. #include <math.h>
  13.  
  14. /* graphics parameters */
  15.  
  16. STATIC unsigned h_xsize, h_ysize, h_colormax;
  17.  
  18. /* transformation parameters */
  19.  
  20. STATIC float w_x_offs = 0;
  21. STATIC float w_y_offs = 0;
  22. STATIC float w_x_mult = 1;
  23. STATIC float w_y_mult = 1;
  24. STATIC float v_x_offs = 0;
  25. STATIC float v_y_offs = 0;
  26. STATIC float v_x_mult = 1;
  27. STATIC float v_y_mult = 1;
  28.  
  29. /* state variables */
  30.  
  31. STATIC float cur_wx, cur_wy;
  32. STATIC unsigned cur_c;
  33.  
  34. #define smashcolor(c) ((c) > h_colormax ? h_colormax : c)
  35. #define swap(a, b) {int tmp; tmp=a; a=b; b=tmp;}
  36.  
  37. /*****************************************************************************
  38.  *                          transformation macros                            *
  39.  *****************************************************************************/
  40.  
  41. #define tr_x_wtov(x) (((x) + w_x_offs) * w_x_mult)
  42. #define tr_x_vton(x) ((x) * v_x_mult + v_x_offs)
  43. #define tr_x_ntod(x) ((x) * (h_xsize-1))
  44.  
  45. #define tr_x_dton(x) (((float)(x)) / (h_xsize-1))
  46. #define tr_x_ntov(x) (((x) - v_x_offs) / v_x_mult)
  47. #define tr_x_vtow(x) ((x) / w_x_mult - w_x_offs)
  48.  
  49. #define tr_x_wton(x) tr_x_vton( tr_x_wtov (x))
  50. #define tr_x_ntow(x) tr_x_vtow( tr_x_ntov (x))
  51. #define tr_x_dtow(x) tr_x_ntow( tr_x_dton (x))
  52.  
  53.  
  54. #define tr_y_wtov(y) (((y) + w_y_offs) * w_y_mult)
  55. #define tr_y_vton(y) ((y) * v_y_mult + v_y_offs)
  56. #define tr_y_ntod(y) ((y) * (h_ysize-1))
  57.  
  58. #define tr_y_dton(y) (((float)(y)) / (h_ysize-1))
  59. #define tr_y_ntov(y) (((y) - v_y_offs) / v_y_mult)
  60. #define tr_y_vtow(y) ((y) / w_y_mult - w_y_offs)
  61.  
  62. #define tr_y_wton(y) tr_y_vton( tr_y_wtov (y))
  63. #define tr_y_ntow(y) tr_y_vtow( tr_y_ntov (y))
  64. #define tr_y_dtow(y) tr_y_ntow( tr_y_dton (y))
  65.  
  66.  
  67. #define tr_r_wtov(r) ((r) * w_x_mult)
  68. #define tr_r_vton(r) ((r) * v_x_mult)
  69. #define tr_r_ntod(r) tr_x_ntod(r)
  70.  
  71. #define tr_r_wton(r) tr_r_vton( tr_r_wtov (r))
  72.  
  73. /*****************************************************************************
  74.  *                        private utility functions                          *
  75.  *****************************************************************************/
  76.  
  77. int tr_x(float);
  78. STATIC int tr_x(x)
  79. float x;
  80. {
  81.   return( tr_x_ntod( tr_x_wton (x)));
  82. }
  83.  
  84. int tr_y(float);
  85. STATIC int tr_y(y)
  86. float y;
  87. {
  88.   return( tr_y_ntod( tr_y_wton (y)));
  89. }
  90.  
  91. int tr_r(float);
  92. STATIC int tr_r(r)
  93. float r;
  94. {
  95.   return( tr_r_ntod( tr_r_wton (r)));
  96. }
  97.  
  98. /*****************************************************************************
  99.  *                          user-callable routines                           *
  100.  *****************************************************************************/
  101.  
  102. /* coordinate transforms */
  103.  
  104. void mapdton(dx, dy, nx, ny)
  105. int   *dx, *dy;
  106. float *nx, *ny;
  107. {
  108.   *nx = tr_x_dton(*dx);
  109.   *ny = tr_y_dton(*dy);
  110. }
  111.  
  112. void mapdtow(dx, dy, wx, wy)
  113. int   *dx, *dy;
  114. float *wx, *wy;
  115. {
  116.   *wx = tr_x_dtow(*dx);
  117.   *wy = tr_y_dtow(*dy);
  118. }
  119.  
  120. void mapntod(nx, ny, dx, dy)
  121. float *nx, *ny;
  122. int   *dx, *dy;
  123. {
  124.   *dx = tr_x_ntod(*nx);
  125.   *dy = tr_y_ntod(*ny);
  126. }
  127.  
  128. void mapntow(nx, ny, wx, wy)
  129. float *nx, *ny;
  130. float *wx, *wy;
  131. {
  132.   *wx = tr_x_ntow(*nx);
  133.   *wy = tr_y_ntow(*ny);
  134. }
  135.  
  136. void mapwtod(wx, wy, dx, dy)
  137. float *wx, *wy;
  138. int   *dx, *dy;
  139. {
  140.   *dx = tr_x(*wx);
  141.   *dy = tr_y(*wy);
  142. }
  143.  
  144. void mapwton(wx, wy, nx, ny)
  145. float *wx, *wy;
  146. float *nx, *ny;
  147. {
  148.   *nx = tr_x_wton(*wx);
  149.   *ny = tr_y_wton(*wy);
  150. }
  151.  
  152. /* routines controlling transformations */
  153.  
  154. void setworld(x1,y1,x2,y2)
  155. float *x1,*y1,*x2,*y2;
  156. {
  157.   w_x_offs = -*x1;
  158.   w_y_offs = -*y2;
  159.  
  160.   if (*x2 - *x1 != 0.0)
  161.     w_x_mult = 1.0/(*x2 - *x1);
  162.   else
  163.     w_x_mult = 1e15;
  164.  
  165.   if (*y1 - *y2 != 0.0)
  166.     w_y_mult = 1.0/(*y1 - *y2);
  167.   else
  168.     w_y_mult = 1e15;
  169. }
  170.  
  171. void setviewport(x1, y1, x2, y2, bdr, bck)
  172. float *x1,*x2,*y1,*y2;
  173. int *bdr,*bck;
  174. {
  175.   int dx1, dx2, dy1, dy2;
  176.  
  177.   v_x_offs = *x1;
  178.   v_y_offs = *y1;
  179.  
  180.   if (*x2 - *x1 != 0.0)
  181.     v_x_mult = (*x2 - *x1);
  182.   else
  183.     v_x_mult = 1e-15;
  184.  
  185.   if (*y2 - *y1 != 0.0)
  186.     v_y_mult = (*y2 - *y1);
  187.   else
  188.     v_y_mult = 1e-15;
  189.  
  190.   dx1 = tr_x_ntod(*x1);
  191.   dx2 = tr_x_ntod(*x2);
  192.   dy1 = tr_y_ntod(*y1);
  193.   dy2 = tr_y_ntod(*y2);
  194.  
  195.   if (*bdr >= 0) {
  196.     if (dx1 > dx2) swap(dx1, dx2);
  197.     if (dy1 > dy2) swap(dy1, dy2);
  198.  
  199.     g_setclip(dx1-1, dy1-1, dx2+1, dy2+1);
  200.     g_box(dx1-1, dy1-1, dx2+1, dy2+1, smashcolor(*bdr));
  201.   }
  202.  
  203.   g_setclip(dx1, dy1, dx2, dy2);
  204.  
  205.   if (*bck >= 0)
  206.     g_clear(smashcolor(*bck));
  207. }
  208.  
  209. /* drawing routines */
  210.  
  211. void movabs(wx, wy)
  212. float *wx, *wy;
  213. {
  214.   cur_wx = *wx;
  215.   cur_wy = *wy;
  216. }
  217.  
  218. void movrel(wx, wy)
  219. float *wx, *wy;
  220. {
  221.   cur_wx += *wx;
  222.   cur_wy += *wy;
  223. }
  224.  
  225. void lnabs(wx, wy)
  226. float *wx, *wy;
  227. {
  228.   g_line(tr_x(cur_wx), tr_y(cur_wy), tr_x(*wx), tr_y(*wy), cur_c);
  229.   cur_wx = *wx;
  230.   cur_wy = *wy;
  231. }
  232.  
  233. void lnrel(wx, wy)
  234. float *wx, *wy;
  235. {
  236.   g_line(tr_x(cur_wx),     tr_y(cur_wy),
  237.          tr_x(cur_wx+*wx), tr_y(cur_wy+*wy), cur_c);
  238.   cur_wx += *wx;
  239.   cur_wy += *wy;
  240. }
  241.  
  242. void cir(r)
  243. float *r;
  244. {
  245.   int dx, dy, dr;
  246.  
  247.   if (*r > 0.0) {
  248.     dx = tr_x(cur_wx);
  249.     dy = tr_y(cur_wy);
  250.     dr = tr_r(*r);
  251.     g_circle(dx, dy, dr, cur_c);
  252.   }
  253. }
  254.  
  255. void box(wx1, wy1, wx2, wy2)
  256. float *wx1, *wx2, *wy1, *wy2;
  257. {
  258.   g_box(tr_x(*wx1), tr_y(*wy1), tr_x(*wx2), tr_y(*wy2), cur_c);
  259. }
  260.  
  261. /* mode setting stuff */
  262.  
  263. setcolor(c)
  264. int *c;
  265. {
  266.   cur_c = smashcolor(*c);
  267. }
  268.  
  269. setxor(m)
  270. int *m;
  271. {
  272.   g_setxor(*m);
  273. }
  274.  
  275. /* get parameters - must be called AFTER g_open */
  276.  
  277. void halo_init()
  278. {
  279.   struct g_info info;
  280.  
  281.   g_info(&info);
  282.   h_xsize = info.xsize;
  283.   h_ysize = info.ysize;
  284.   h_colormax = info.colormax;
  285.  
  286.   w_x_offs = w_y_offs = 0;
  287.   w_x_mult = w_y_mult = 1;
  288.   v_x_offs = v_y_offs = 0;
  289.   v_x_mult = v_y_mult = 1;
  290.  
  291.   cur_wx = cur_wy = 0;
  292.   cur_c = 0;
  293. }
  294.