C Programming - Functions and Commands

Discussion in 'Programmer's Corner' started by smarch, Jun 22, 2010.

  1. smarch

    Thread Starter Active Member

    Mar 14, 2009
    52
    0
    Hi,

    I am trying to teach myself C programming, using different sources, I have compiled the programme in this post.
    The code generates a background gradient with a line, what I want to do now is implement the following functions:


    Code ( (Unknown Language)):
    1. int FloodImage(pixel_colour flood_colour);
    2. /* Fills entire array with pixels of the given colour
    3. /* always returns 0
    4.  
    5. int DrawLine(int start_x, int start_y, int finish_x, int finish_y)
    6. /* draws a straight line between points (x_start, y_start) and
    7. /* (x_finish, y_finish) in colour line_colour
    8.  
    9. int RefreshImage( );
    10. /* writes new copy of image file to disk and increments
    11. /* generation number so that next RefreshImage() call does not
    12. /* overwrite but writes a new file
    13. /* returns new generation number
    I then want to provide a user interface able to accept these commands:

    Code ( (Unknown Language)):
    1. FL r g b  // indicates that text to the end of a line is commentsets   every pixel in the image to the colour r g b
    2.  
    3. LI x1 y1 x2 y2 r g b //draws line in colour r g b
    4.  
    5. RE //Performs image file refresh
    6.  
    7. EX //terminates the program
    Here is my code that I need to implement:

    Code ( (Unknown Language)):
    1. #include <stdio.h>
    2.  
    3. #define XSIZE 256
    4. #define YSIZE 128
    5. #define RED 0
    6. #define GREEN 1
    7. #define BLUE 2
    8.  
    9. int image[YSIZE][XSIZE][3];
    10.  
    11. main()
    12. {
    13.     int x, y;                        // Loop
    14.  
    15.     int xs = 10;                    // Line start and end
    16.     int ys = 10;
    17.     int xe = 180;
    18.     int ye = 120;
    19.  
    20.  
    21.     int r1 = 255;                    // used to generate                  
    22.     int r2 =   0;
    23.     int g1 =  60;
    24.     int g2 =  30;
    25.     int b1 =   0;
    26.     int b2 = 255;
    27.  
    28.     int rval, gval, bval;            // used for colour of line
    29.     float m;                        // holds gradient of line
    30.     int debug = 1;                    // set to 0 to turn off debug  printf's
    31.  
    32.  
    33.  
    34.  
    35.     for(x=0; x<XSIZE; x++)                //Columns
    36.         for(y=0; y<YSIZE; y++)            //Rows
    37.         {
    38.            
    39.             image[y][x][RED]   = r1 + (int)(x * (r2 - r1)/XSIZE);
    40.             image[y][x][GREEN] = g1 + (int)(x * (g2 - g1)/XSIZE);
    41.             image[y][x][BLUE]  = b1 + (int)(x * (b2 - b1)/XSIZE);
    42.         }
    43.  
    44.         // Now draw a line    
    45. // Draws a line from xs, ys to xe, ye
    46. // in colour rval, gval, bval
    47.  
    48.  
    49.         rval = 128;
    50.     gval = 255;
    51.     bval = 0;    
    52.     if (abs(xe - xs) > abs(ye - ys))        //check gradient less  than one
    53.     {
    54.         if (debug) printf("Gradient less than 1 - looping in x\n");
    55.         m = (ye - ys)/(float)(xe - xs);        //Calculate gradient
    56.         if (debug) printf("Gradient in x is %f\n", m);
    57.         if (xs > xe)
    58.         {
    59.             int temp;
    60.             //swap xs and xe and ys and ye
    61.             if (debug) printf("x0 greater so need to swap first and  second coordinate\n");
    62.             temp = xs; xs = xe; xe = temp;
    63.             temp = ys; ys = ye; ye = temp;
    64.             if (debug) printf("Points are: %d, %d and %d, %d\n", xs, ys,  xe, ye);
    65.         }    
    66.         for (x = xs; x < xe; x++)
    67.         {
    68.             y = (int)(ys + (m * (x - xs)));
    69.             //if (debug) printf("x : %d y : %d \n", x, y);
    70.             image [y][x][RED] = rval;
    71.             image [y][x][GREEN] = gval;
    72.             image [y][x][BLUE] = bval;
    73.         }
    74.     }
    75.     else                                    // gradient greater than one  if here
    76.     {
    77.         if (debug) printf("Gradient greater than 1 - looping in y\n");
    78.         m = (xe - xs)/(float)(ye - ys);        //Calculate gradient
    79.         if (debug) printf("Gradient in y is %f\n", m);
    80.         if (ys > ye)
    81.         {
    82.             int temp;
    83.             //swap xs and xe and ys and ye
    84.             if (debug) printf("ys greater so need to swap first and  second coordinate\n");
    85.             temp = xs; xs = xe; xe = temp;
    86.             temp = ys; ys = ye; ye = temp;
    87.             if (debug) printf("Points are: %d, %d and %d, %d\n", xs, ys,  xe, ye);
    88.         }    
    89.         for (y = ys; y < ye; y++)
    90.         {
    91.             x = (int)(xs + (m * (y - ys)));
    92.             //printf("x : %d y : %d \n", x, y);
    93.             image [y][x][RED] = rval;
    94.             image [y][x][GREEN] = gval;
    95.             image [y][x][BLUE] = bval;
    96.         }
    97.     }
    98.  
    99.  
    100.  
    101.        
    102.             {
    103.         FILE *pfile = NULL;
    104.         int x,y;    
    105.         pfile = fopen("myfile.ppm", "w");
    106.         fprintf(pfile, "P3\n# %s\n%d %d\n255\n", "Myfile.ppm", XSIZE,  YSIZE);
    107.         for(y = 0; y < YSIZE; y++)
    108.             for(x = 0; x < XSIZE; x++)
    109.                 fprintf(pfile,"%d %d  %d\n",image[y][x][0],image[y][x][1],image[y][x][2]);
    110.         fclose(pfile);
    111.         system("myfile.ppm");
    112.     }
    113.  
    114.         }
    Can anyone advise/direct me or anything helpful at all to accomplish this with my code, I am quite confused as to how I should even start implementing it, my codes just don't seem to work and I just get confused more and more.
    I am sorry if it is hard to read, I am trying to learn.
     
  2. davebee

    Well-Known Member

    Oct 22, 2008
    539
    46
    The more you can figure out on your own, the better you'll learn programming.

    I just have a couple of tips.

    Separate the user input and program operation structure from the drawing actions by implementing each drawing and coloring action as a separate calleable procedure.

    Have main() perform all the one-time initializing that has to be done, then have it enter a loop where it waits for user input, then then executes actions, one of which is terminating the program.

    Structuring the logic of the program is a matter of personal style, but my own feeling is that when you have several levels of nested if-else-ifs, or a long series of repeated if-else-if blocks, the logic quickly becomes extremely difficult to follow.

    Sometimes it's hard to avoid some logic nesting, but I'd suggest trying to break up the logic into short, individual modules (procedures and functions) that are carefully named and each do a single piece of work.

    That can go a long way towards making the code easier to develop and debug.
     
  3. smarch

    Thread Starter Active Member

    Mar 14, 2009
    52
    0
    Thanks for your reply, not a lot of that makes sense to me to be honest, I'm having major problems trying to understand it all and how I can progress. Could you give me an example of what you mean and explain it step by step, or get me started on my program annotating what I need to do? If that makes sense.
     
  4. davebee

    Well-Known Member

    Oct 22, 2008
    539
    46
    No, I really do not have the time to help with the actual coding.

    But I will give you another tip.

    Start by writing the smallest program that completely compiles and runs, error-free, even if it is as simple as that classic beginners program that prints "Hello World!" then exits.

    When that program is working, add one feature to it. Get that new feature working perfectly. Maybe you could work on getting the user input "EX" to terminate the program.

    If you've copied some code from somewhere else, that's fine, everyone re-uses code, but don't try to copy, compile and run a huge amount of unknown code and expect it to work. Add one feature at a time.

    The point is that when you start with a working program, no matter how small, and you add one feature, then if it breaks, you know exactly where to look for the problem.
     
  5. smarch

    Thread Starter Active Member

    Mar 14, 2009
    52
    0
    Thanks for your reply and your tips
     
Loading...