Main Page   Data Structures   File List   Data Fields   Globals  

bresh.c

Go to the documentation of this file.
00001 /*
00002 ##########################################
00003 # bresh.c -
00004 #   Algorithm for drawing a line in an image.
00005 #
00006 # RCS: $Id: bresh.c,v 2.1 1999/01/14 15:37:32 conner Exp $ 
00007 # 
00008 # Digital Image Analysis Lab
00009 # Dept. of Electrical and Computer Engineering
00010 # University of Arizona
00011 ##########################################
00012 */
00013 
00014 /*
00015     Breshenham line drawing algorithm
00016 
00017 Modified so that the points to be drawn 
00018       are in one function.  
00019       plot_point(x,y)  is only function that uses the 
00020       external variables.
00021 
00022 Also:  All of the lines are draw
00023        from ymax values to ymin values.
00024        X ordering doesn't matter.
00025 
00026 */
00027 
00028 #include <stdio.h>
00029 /*#include <sys/param.h>*/
00030 #include <sys/types.h>
00031 
00032 /* RCS Indentification information */
00033 static char rcsid[] = "$Id: bresh.c,v 2.1 1999/01/14 15:37:32 conner Exp $";
00034 
00035 extern int **x_array_buffer;    /* Window Size */
00036 extern int count_point;
00037 extern int point;
00038 
00039 brshnm (x1, y1, x2, y2)
00040      int x1, y1, x2, y2;
00041 {
00042 
00043   int x, y;
00044   int tempx, tempy;
00045   int dely, delx;
00046   int ebar;
00047   int i;
00048 
00049 #ifdef  DEBUG
00050   printf ("x1 = %d , y1 = %d\n", x1, y1);
00051   printf ("x2 = %d , y2 = %d\n", x2, y2);
00052 #endif
00053 
00054   if ((x1 > x2) && (y1 > y2))
00055     {
00056 
00057       /* ------------------------------------------------------
00058        *  Case 1
00059        *  Neg. Slope
00060        *  X1, Y1 is greater than X2, Y2
00061        *  ie. Line starts at low end of screen and goes up
00062        */
00063 
00064 #ifdef  DEBUG
00065       printf ("First point is greater than second point\n");
00066       printf ("x1 = %d , y1 = %d\n", x1, y1);
00067       printf ("x2 = %d , y2 = %d\n", x2, y2);
00068 #endif
00069 
00070       pos_hor_slope (x1, y1, x2, y2);
00071 
00072     }
00073   else if ((x1 > x2) && (y1 == y2))
00074     {
00075 
00076       /* ------------------------------------------------------
00077        *  Case 2.
00078        *  Horizontal Line where 
00079        *  X1 > X2
00080        *  ie. Line starts at right and goes left
00081        */
00082 
00083       pos_hor_slope (x1, y1, x2, y2);
00084 
00085     }
00086   else if ((x1 < x2) && (y1 > y2))
00087     {
00088 
00089 #ifdef  DEBUG
00090       printf (" Neg slope points \n");
00091 #endif
00092 
00093       neg_slope (x1, y1, x2, y2);
00094 
00095     }
00096   else if ((x1 > x2) && (y1 < y2))
00097     {
00098 
00099 #ifdef  DEBUG
00100       printf ("Neg slope points w/ \n");
00101       printf ("Start at Upper right  to Lower Left \n");
00102 #endif
00103 
00104       neg_slope (x1, y1, x2, y2);
00105 
00106     }
00107   else if ((x1 == x2) && (y1 != y2))
00108     {
00109 
00110       /* ------------------------------------------------------
00111        *  Case 3. 
00112        *  VERTICAL line 
00113        */
00114 
00115 #ifdef  DEBUG
00116       printf ("Vertical Slope\n");
00117 #endif
00118 
00119       vertical_slope (x1, y1, x2, y2);
00120 
00121     }
00122   else
00123     {
00124 
00125       pos_hor_slope (x1, y1, x2, y2);
00126 
00127     }
00128 
00129 }
00130 
00131 /* ------------------------------------------------------ */
00132 /* This routine plots point by point vertical lines.
00133  * 
00134  */
00135 vertical_slope (x1, y1, x2, y2)
00136      int x1, y1, x2, y2;
00137 {
00138   int x, y, tempy;
00139   x = x1;
00140 
00141   if (y1 < y2)
00142     {
00143       for (y = y1; y <= y2; y++)
00144         {
00145           plot_points (x, y);
00146         }
00147     }
00148   else
00149     {
00150       for (y = y1; y >= y2; y--)
00151         {
00152           plot_points (x, y);
00153         }
00154     }
00155 }
00156 
00157 /* ------------------------------------------------------ */
00158 /* This routine plots point by point  lines with
00159  * Neg slope .
00160  */
00161 neg_slope (x1, y1, x2, y2)
00162      int x1, y1, x2, y2;
00163 {
00164   int x, y;
00165   int delx, dely;
00166   int ebar;
00167   int i;
00168 
00169   delx = abs (x2 - x1);
00170   dely = abs (y2 - y1);
00171 
00172   /* Initialization
00173      of loop
00174    */
00175   x = x1;
00176   y = y1;
00177 
00178   if (dely < delx)
00179     {
00180       /*
00181        * Slope is less than 1
00182        */
00183       ebar = 2 * dely - delx;
00184       for (i = 0; i < delx; i++)
00185         {
00186           plot_points (x, y);
00187 
00188           if ((x1 < x2) && (y1 > y2))
00189             {
00190 
00191               while (ebar > 0)
00192                 {
00193                   y = y - 1;
00194                   ebar = ebar - 2 * delx;
00195                 }
00196 
00197               x = x + 1;
00198               ebar = ebar + 2 * dely;
00199 
00200             }
00201           else
00202             {
00203 
00204               while (ebar > 0)
00205                 {
00206                   y = y + 1;
00207                   ebar = ebar - 2 * delx;
00208                 }
00209 
00210               x = x - 1;
00211               ebar = ebar + 2 * dely;
00212 
00213             }
00214         }
00215     }
00216 
00217   else
00218     {
00219       /*
00220        * Slope Greater than 1
00221        */
00222 #ifdef  DEBUG
00223       printf ("Slope greater than 1\n");
00224 #endif
00225 
00226       ebar = 2 * delx - dely;
00227       for (i = 0; i < dely; i++)
00228         {
00229           plot_points (x, y);
00230 
00231           if ((x1 < x2) && (y1 > y2))
00232             {
00233 
00234               while (ebar > 0)
00235                 {
00236                   x = x + 1;
00237                   ebar = ebar - 2 * dely;
00238                 }
00239 
00240               y = y - 1;
00241               ebar = ebar + 2 * delx;
00242 
00243             }
00244           else
00245             {
00246 
00247               while (ebar > 0)
00248                 {
00249                   x = x - 1;
00250                   ebar = ebar - 2 * dely;
00251                 }
00252 
00253               y = y + 1;
00254               ebar = ebar + 2 * delx;
00255 
00256             }
00257         }
00258     }
00259 }
00260 
00261 /* ------------------------------------------------------ */
00262 /* This routine plots point by point  lines with
00263  * Pos slope and horizontal lines.
00264  */
00265 pos_hor_slope (x1, y1, x2, y2)
00266      int x1, y1, x2, y2;
00267 {
00268   int x, y;
00269   int delx, dely;
00270   int ebar;
00271   int i;
00272 
00273   /* Initialization
00274      of loop
00275    */
00276   x = x1;
00277   y = y1;
00278 
00279   delx = abs (x2 - x1);
00280   dely = abs (y2 - y1);
00281 
00282   if (dely < delx)
00283     {
00284       /*
00285        * Slope is less than 1
00286        */
00287       ebar = 2 * dely - delx;
00288       for (i = 0; i < delx; i++)
00289         {
00290           plot_points (x, y);
00291 
00292           while (ebar > 0)
00293             {
00294               if (y1 < y2)
00295                 {
00296                   y = y + 1;
00297                 }
00298               else
00299                 {
00300                   y = y - 1;
00301                 }
00302               ebar = ebar - 2 * delx;
00303             }
00304 
00305           if (x1 < x2)
00306             {
00307               x = x + 1;
00308             }
00309           else
00310             {
00311               x = x - 1;
00312             }
00313           ebar = ebar + 2 * dely;
00314 
00315         }
00316 
00317     }
00318   else
00319     {
00320       /*
00321        * Slope Greater than 1
00322        */
00323 
00324 #ifdef  DEBUG
00325       printf ("Slope greater than 1\n");
00326       printf ("than 1\n");
00327 #endif
00328 
00329       ebar = 2 * delx - dely;
00330       for (i = 0; i < dely; i++)
00331         {
00332           plot_points (x, y);
00333 
00334           while (ebar > 0)
00335             {
00336               if (x1 < x2)
00337                 {
00338                   x = x + 1;
00339                 }
00340               else
00341                 {
00342                   x = x - 1;
00343                 }
00344               ebar = ebar - 2 * dely;
00345             }
00346 
00347           if (y1 < y2)
00348             {
00349               y = y + 1;
00350             }
00351           else
00352             {
00353               y = y - 1;
00354             }
00355           ebar = ebar + 2 * delx;
00356         }
00357     }
00358 
00359 }
00360 
00361 /* 
00362    Uses global variables to either count or fill the 
00363    passed array.
00364 */
00365 plot_points (x, y)
00366      int x, y;
00367 {
00368 
00369 #ifdef  DEBUG
00370   printf ("x = %d, y= %d \n", x, y);
00371 #endif
00372 
00373   if (count_point)
00374     {
00375 
00376       point++;
00377 #ifdef  DEBUG
00378       printf ("Points allocated %d\n", point);
00379 #endif
00380 
00381     }
00382   else
00383     {
00384 
00385       if (x < 0)
00386         {
00387           printf ("Bad X value %d\n", x);
00388         }
00389       else
00390         {
00391 
00392           x_array_buffer[point][0] = x;
00393           x_array_buffer[point++][1] = y;
00394 
00395         }
00396 
00397     }
00398 
00399 }

Generated on Sun May 18 15:36:07 2003 for tclSadie by doxygen1.3