home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_07_06 / v7n6083a.txt < prev    next >
Text File  |  1989-07-25  |  12KB  |  361 lines

  1. =========================================================================
  2. EXAMPLE PROGRAM LISTING 1
  3. =========================================================================
  4.  
  5. /*==============================================*/
  6. /* EXAMPLE 1                                    */
  7. /* Using slope intercept method to plot a line. */
  8. /* Very simplified.                             */
  9. /*==============================================*/
  10. #define swap(x,y) {register int temp; temp = x; x = y; y = temp;}
  11.  
  12. drawline( x1, y1, x2, y2)
  13. int x1, y1, x2, y2;
  14. {
  15.     float slope, intercept;
  16.     float x,y;
  17.  
  18.     if(x1 > x2){
  19.         swap(x1, x2);
  20.         swap(y1, y2);
  21.     }
  22.     slope = (float)(y1 - y2)/(float)(x1 - x2);
  23.     intercept = (float)y1 - (slope * (float)x1);
  24.  
  25.     for( x = (float)x1; x <= (float)x2; x+=1.0){
  26.         y = ((slope * x) + intercept);
  27.         plot((int)x, (int)y);
  28.     }
  29. }
  30.  
  31.  
  32. =========================================================================
  33. EXAMPLE PROGRAM LISTING 2
  34. =========================================================================
  35. /*=====================================================*/
  36. /* EXAMPLE 2                                           */
  37. /* Using slope intercept method to plot a line.        */
  38. /* Improved by not using floats.                       */
  39. /* Very simplified.                                    */
  40. /*=====================================================*/
  41. #define swap(x,y) {register int temp; temp = x; x = y; y = temp;}
  42.  
  43. drawline( x1, y1, x2, y2)
  44. int x1, y1, x2, y2;
  45. {
  46.     long delta_x, delta_y;
  47.     long intercept;
  48.     long x,y;
  49.  
  50.     delta_x = x1 - x2;
  51.     delta_y = y1 - y2;
  52.     intercept = y1 - (delta_y * x1)/delta_x;
  53.  
  54.     if(x1 > x2){
  55.         swap(x1, x2);
  56.         swap(y1, y2);
  57.     }
  58.     for( x = (long)x1; x <= (long)x2; x++){
  59.         y = (x * delta_y)/delta_x + intercept;
  60.         plot((int)x, (int)y);
  61.     }
  62. }
  63.  
  64.  
  65. =========================================================================
  66. EXAMPLE PROGRAM LISTING 3
  67. =========================================================================
  68. /*==============================================*/
  69. /* EXAMPLE 3                                    */
  70. /* Using slope intercept method to plot a line. */
  71. /* Improved by not using floats.                */
  72. /* Further improved by checking for Horizontal  */
  73. /* and verticle lines, and by incrementing on   */
  74. /* the axis which changes most.                 */
  75. /*==============================================*/
  76. #define swap(x,y) {register int temp; temp = x; x = y; y = temp;}
  77.  
  78. drawline( x1, y1, x2, y2)
  79. int x1, y1, x2, y2;
  80. {
  81.     long delta_x, delta_y;
  82.     long intercept;
  83.     long x,y;
  84.  
  85.     delta_x = x1 - x2;
  86.     delta_y = y1 - y2;
  87.  
  88.     if(delta_x == 0){
  89.         v_line(x1, y1, y2); /* draw a verticle line */
  90.         return;
  91.     }
  92.     if(delta_y == 0){
  93.         h_line(x1, x2, y1); /* draw a horizontal line */
  94.         return;
  95.     }
  96.  
  97.     intercept = y1 - (delta_y * x1)/delta_x;
  98.  
  99.     if(delta_y > delta_x){ /* X increases faster than Y */
  100.         if(x1 > x2){
  101.             swap(x1, x2);
  102.             swap(y1, y2);
  103.         }
  104.         for( x = (long)x1; x <= (long)x2; x++){
  105.             y = (x * delta_y)/delta_x + intercept;
  106.             plot((int)x, (int)y);
  107.         }
  108.     }
  109.     else{                  /* Y increases faster than X */
  110.         if(y1 > y2){
  111.             swap(x1, x2);
  112.             swap(y1, y2);
  113.         }
  114.         for( y = (long)y1; y <= (long)y2; y++){
  115.             x = ((y - intercept) * delta_x)/ delta_y;
  116.             plot((int)x, (int)y);
  117.         }
  118.     }
  119. }
  120.  
  121. =========================================================================
  122. EXAMPLE PROGRAM LISTING 4
  123. =========================================================================
  124.  
  125. /*----------------------------------------------------------------*/
  126. /*  Program Listing 4                                             */
  127. /*                                                                */
  128. /*    MODULE: line.c                                              */
  129. /*  CONTENTS: drawline(), h_line(), v_line(), clip_chk()          */
  130. /*    AUTHOR: John T. Bell                                        */
  131. /*      DATE: 03 June, 1989                                       */
  132. /*----------------------------------------------------------------*/
  133. #include "graph.h"
  134.  
  135. /*----------------------------------------------------------------*/
  136. /* This macro swaps the value of integer X with integer Y.        */
  137. /*----------------------------------------------------------------*/
  138. #define swap(x,y) {register int temp; temp = x; x = y; y = temp;}
  139.  
  140. /*--------------------------------------------------------------------------*/
  141. /*   Function: drawline(int strt_x, int strt_y, int end_x, int end_y);      */
  142. /*    Purpose: Draw a line from the starting coordinates to the             */
  143. /*             ending coordinates.                                          */
  144. /*    Returns: 0 if line can be drawn, -1 if error.                         */
  145. /*      Setup: Computer must be in graphics mode.                           */
  146. /* Attributes: NONE                                                         */
  147. /*      Notes: Uses the "Octantal Digital Differential Analizer" method     */
  148. /*             to draw the line.                                            */
  149. /*             Ref: Artwick, "Microcomputer Displays, Graphics,             */
  150. /*                  and Animation",Prentice Hall, 1985. pp 20-30            */
  151. /*--------------------------------------------------------------------------*/
  152. int drawline(strt_x,strt_y,end_x,end_y)
  153. int strt_x,strt_y,end_x,end_y;
  154. {
  155.     int dx,dy;              /* delta x and delta y */
  156.     int err,count;          /* error value, and point counter */
  157.  
  158.     /* test for data in range */
  159.     if(clip_chk(strt_x,strt_y) || clip_chk(end_x,end_y))
  160.         return(0);
  161.  
  162.     if(strt_x == end_x)     /* if verticle line do faster v_line() */
  163.         return(v_line(strt_x,strt_y,end_y));
  164.  
  165.     if(strt_y == end_y)     /* if horizontal line do faster h_line() */
  166.         return(h_line(strt_x,end_x,strt_y));
  167.  
  168.     if(end_x < strt_x){     /* swap points to map octants 3-6 into 1,2,7,8 */
  169.         swap(strt_x,end_x);
  170.         swap(strt_y,end_y);
  171.     }
  172.     dx = end_x - strt_x; /* calc change in x (delta x) */
  173.     dy = end_y - strt_y; /* calc change in y (delta y) */
  174.  
  175.     if(dy < 0){             /* Select Octant 7,8 or 1,2 */
  176.         if( -dy > dx){      /* Octant 7 Line */
  177.             err = dy/2;
  178.             for(count = -dy+1; count >= 0; count--){
  179.                 plot(strt_x,strt_y);
  180.                 strt_y--;
  181.                 err += dx;
  182.                 if(err >= 0){
  183.                     strt_x++;
  184.                     err += dy;
  185.                 }
  186.             }
  187.         }
  188.         else{           /* Octant 8 Line */
  189.             err = -dx/2;
  190.             for(count = dx+1; count >= 0; count--){
  191.                 plot(strt_x,strt_y);
  192.                 strt_x++;
  193.                 err -= dy;
  194.                 if(err >= 0){
  195.                     strt_y--;
  196.                     err -= dx;
  197.                 }
  198.             }
  199.         }
  200.     }
  201.     else{               /* Octant 1 or 2  */
  202.         if(dy > dx){    /* Octant 2 Line */
  203.             err = -dy/2;
  204.             for(count = dy+1; count >= 0; count--){
  205.                 plot(strt_x,strt_y);
  206.                 strt_y++;
  207.                 err += dx;
  208.                 if(err >= 0){
  209.                     strt_x++;
  210.                     err -= dy;
  211.                 }
  212.             }
  213.         }
  214.         else{           /* Octant 1 Line */
  215.             err = -dx/2;
  216.             for(count = dx+1; count >= 0; count--){
  217.                 plot(strt_x,strt_y);
  218.                 strt_x++;
  219.                 err += dy;
  220.                 if(err >= 0){
  221.                     strt_y++;
  222.                     err -= dx;
  223.                 }
  224.             }
  225.         }
  226.     }
  227.     return(1);
  228. }
  229.  
  230. int v_line(x, strt_y, end_y)
  231. int x;
  232. int strt_y, end_y;
  233. {
  234.     if(strt_y > end_y){
  235.         swap(strt_y, end_y);
  236.     }
  237.  
  238.     for(; strt_y <= end_y; strt_y++)
  239.         plot(x, strt_y);
  240.     return(1);
  241. }
  242.  
  243. int h_line(strt_x, end_x, y)
  244. {
  245.     if(strt_x > end_x){
  246.         swap(strt_x, end_x);
  247.     }
  248.     for(;strt_x <= end_x; strt_x++)
  249.         plot(strt_x, y);
  250.     return(1);
  251. }
  252.  
  253. /*--------------------------------------------------------------------*/
  254. /* clip_chk(int x, int y);                                            */ 
  255. /* returns TRUE if x or y is out of the screen plotting range.        */
  256. /* This needs the globals G_maxx and G_maxy which correspond to       */
  257. /* the maximum X and Y axis values.                                   */
  258. /*--------------------------------------------------------------------*/
  259. clip_chk(x,y)
  260. int x,y;
  261. {
  262.     return((((x < 0) || (x > G_maxx) || (y < 0) || (y > G_maxy)) ? 1 : 0));
  263. }
  264.  
  265. =========================================================================
  266. LISTING 5 graph.h
  267. =========================================================================
  268.  
  269. #ifndef EXTRN
  270. extern int G_maxx, G_maxy;
  271.  
  272. #else
  273. int G_maxx, G_maxy;
  274. #endif
  275.  
  276. #define AZTEC
  277. #ifdef AZTEC
  278. extern int _maxx;
  279. extern int _xaspect, _yaspect;
  280. extern _oldx, _oldy;
  281. #endif
  282.  
  283. =========================================================================
  284. LISTING 6 demo.c
  285. =========================================================================
  286.  
  287. /*----------------------------------------------------------------*/
  288. /*                Copyright 1989 by John T. Bell                  */
  289. /*                                                                */
  290. /*    MODULE: DEMO.C                                              */
  291. /*   PURPOSE: DEMO program for line drawing routines.             */
  292. /*  CONTENTS:                                                     */
  293. /*    AUTHOR: John T. Bell                                        */
  294. /*   HISTORY: Created 05/31/89                                    */
  295. /*----------------------------------------------------------------*/
  296. #include <stdio.h>
  297.  
  298. #define EXTRN
  299. #include "graph.h"
  300.  
  301. /*--------------------------------------------------------------------------*/
  302. /* Here we need to define;                                                  */
  303. /*                                                                          */
  304. /* G_maxx - Maximum value for the X axis.                                   */
  305. /* G_maxy - Maximum value for the Y axis.                                   */
  306. /*                                                                          */
  307. /* We should also put the screen into graphics mode and clear the screen    */
  308. /* to our background color.                                                 */
  309. /*                                                                          */
  310. /* The following implemenation was written for Aztec C and an IBM CGA card. */
  311. /*--------------------------------------------------------------------------*/
  312. graph_init()
  313. {
  314.     mode('H'); /* sets the graphic mode to 640 by 200 */
  315.     G_maxx = _maxx;
  316.     G_maxy = 200;
  317.     color('W'); /* sets plotting color to white */
  318. }
  319.  
  320. /*--------------------------------------------------------------------------*/
  321. /* This just undoes what graph_init did.                                    */
  322. /*--------------------------------------------------------------------------*/
  323. graph_restore()
  324. {
  325.     mode(0);
  326. }
  327.  
  328. /*-------------------------------------------*/
  329. /* use Aztec point() function to plot points */
  330. /*-------------------------------------------*/
  331. int plot(x,y)
  332. {
  333.     return(point(x, G_maxy - y));
  334. }
  335.  
  336. main()
  337. {
  338.     int x, y;
  339.     int cx, cy;
  340.  
  341.     graph_init();
  342.  
  343.     cx = G_maxx/2;
  344.     cy = G_maxy/2;
  345.  
  346.     for(x = 0; x <= G_maxx; x += (G_maxx/30))
  347.         drawline(cx, cy, x, 0);
  348.  
  349.     for(y = 0; y <= G_maxy; y += (G_maxy/20))
  350.         drawline(cx, cy, G_maxx, y);
  351.  
  352.     for(x = G_maxx; x >= 0; x -= (G_maxx/30))
  353.         drawline(cx, cy, x, G_maxy);
  354.  
  355.     for(y = G_maxy; y >= 0; y -= (G_maxy/20))
  356.         drawline(cx, cy, 0, y);
  357.  
  358.     getchar();    /* pause for keypress */
  359.     graph_restore();
  360. }
  361.