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

Generated on Wed Apr 9 08:56:03 2003 for TREES by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002