C Programming - Functions and Commands

Thread Starter

smarch

Joined Mar 14, 2009
52
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:


Rich (BB code):
int FloodImage(pixel_colour flood_colour);
/* Fills entire array with pixels of the given colour 
/* always returns 0

int DrawLine(int start_x, int start_y, int finish_x, int finish_y)
/* draws a straight line between points (x_start, y_start) and 
/* (x_finish, y_finish) in colour line_colour

int RefreshImage( );
/* writes new copy of image file to disk and increments 
/* generation number so that next RefreshImage() call does not 
/* overwrite but writes a new file 
/* returns new generation number
I then want to provide a user interface able to accept these commands:

Rich (BB code):
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

LI x1 y1 x2 y2 r g b //draws line in colour r g b

RE //Performs image file refresh

EX //terminates the program
Here is my code that I need to implement:

Rich (BB code):
#include <stdio.h>

#define XSIZE 256
#define YSIZE 128
#define RED 0
#define GREEN 1
#define BLUE 2

int image[YSIZE][XSIZE][3];

main()
{
    int x, y;                        // Loop 

    int xs = 10;                    // Line start and end
    int ys = 10;
    int xe = 180;
    int ye = 120;


    int r1 = 255;                    // used to generate                  
    int r2 =   0;
    int g1 =  60;
    int g2 =  30;
    int b1 =   0;
    int b2 = 255;

    int rval, gval, bval;            // used for colour of line
    float m;                        // holds gradient of line
    int debug = 1;                    // set to 0 to turn off debug  printf's




    for(x=0; x<XSIZE; x++)                //Columns
        for(y=0; y<YSIZE; y++)            //Rows
        {
            
            image[y][x][RED]   = r1 + (int)(x * (r2 - r1)/XSIZE);
            image[y][x][GREEN] = g1 + (int)(x * (g2 - g1)/XSIZE);
            image[y][x][BLUE]  = b1 + (int)(x * (b2 - b1)/XSIZE);
        }

        // Now draw a line    
// Draws a line from xs, ys to xe, ye
// in colour rval, gval, bval


        rval = 128;
    gval = 255;
    bval = 0;    
    if (abs(xe - xs) > abs(ye - ys))        //check gradient less  than one
    {
        if (debug) printf("Gradient less than 1 - looping in x\n");
        m = (ye - ys)/(float)(xe - xs);        //Calculate gradient
        if (debug) printf("Gradient in x is %f\n", m);
        if (xs > xe) 
        {
            int temp;
            //swap xs and xe and ys and ye
            if (debug) printf("x0 greater so need to swap first and  second coordinate\n");
            temp = xs; xs = xe; xe = temp; 
            temp = ys; ys = ye; ye = temp; 
            if (debug) printf("Points are: %d, %d and %d, %d\n", xs, ys,  xe, ye);
        }    
        for (x = xs; x < xe; x++)
        {
            y = (int)(ys + (m * (x - xs)));
            //if (debug) printf("x : %d y : %d \n", x, y);
            image [y][x][RED] = rval;
            image [y][x][GREEN] = gval;
            image [y][x][BLUE] = bval;
        }
    }
    else                                    // gradient greater than one  if here
    {
        if (debug) printf("Gradient greater than 1 - looping in y\n");
        m = (xe - xs)/(float)(ye - ys);        //Calculate gradient
        if (debug) printf("Gradient in y is %f\n", m);
        if (ys > ye)
        {
            int temp;
            //swap xs and xe and ys and ye
            if (debug) printf("ys greater so need to swap first and  second coordinate\n");
            temp = xs; xs = xe; xe = temp; 
            temp = ys; ys = ye; ye = temp; 
            if (debug) printf("Points are: %d, %d and %d, %d\n", xs, ys,  xe, ye);
        }    
        for (y = ys; y < ye; y++)
        {
            x = (int)(xs + (m * (y - ys)));
            //printf("x : %d y : %d \n", x, y);
            image [y][x][RED] = rval;
            image [y][x][GREEN] = gval;
            image [y][x][BLUE] = bval;
        }
    }



        
            {
        FILE *pfile = NULL;
        int x,y;    
        pfile = fopen("myfile.ppm", "w");
        fprintf(pfile, "P3\n# %s\n%d %d\n255\n", "Myfile.ppm", XSIZE,  YSIZE); 
        for(y = 0; y < YSIZE; y++)
            for(x = 0; x < XSIZE; x++)
                fprintf(pfile,"%d %d  %d\n",image[y][x][0],image[y][x][1],image[y][x][2]);
        fclose(pfile);
        system("myfile.ppm");
    }

        }
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.
 

davebee

Joined Oct 22, 2008
540
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.
 

Thread Starter

smarch

Joined Mar 14, 2009
52
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.
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.
 

davebee

Joined Oct 22, 2008
540
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.
 
Top