hello,

sorry to bother anyone but i am new to c programming and trying to write a code that draws basic lines of any gradient using arrays, but i can only getting straight lines as the gradient turns out to be 0.

Any help offered would be duly appreciated.

The code i wrote is:

the line is viewed using Xnview.Code:#include<stdio.h> #include<math.h> #define WIDTH 256 //Defines the maxiximum width in image array #define HEIGHT 128 //Defines the maximum height in image array #define RED 0 //Defines the Red colour component #define GREEN 1 //Defines the Green colour component #define BLUE 2 //Defines the Blue colour component void main () { int Red, Green, Blue; //Colour components int Y_start, Y_end, X_start, X_end; //Declares position of line start and line end int x, y; //Declares x and y variables int X2, X1, Y2, Y1; //Maximum and minimum values of X and Y int image [WIDTH][HEIGHT][3]; //Declares the image array int count= 1; //declares count variable int a, b; //loop control variable int m; //Gradient FILE *pfile; //File pointer pfile = fopen("basic-lines.ppm", "w"); for(x=0; x<256; x++) //for loop which includes x variable ranging its max value { for(y=0; y<128; y++) //for loop whiuch includes y variable ranging its max value { image [x][y][RED]=255; image [x][y][GREEN]=255; // values needed to set the background colour of the area to white image [x][y][BLUE]=255; } } printf("\nAll values entered should be between 0 and 255"); printf("\n\nEnter values for the following colours to determine the colour of the line: "); do { printf("\n\nRED:\t"); scanf("%i", &Red); if (Red < 0 || Red > 255) printf("\n\aNumber is out of range, please enter another\a:"); } while ((Red < 0) || (Red > 255)); do { printf("\nGREEN:\t"); scanf("%i", &Green); if (Green < 0 || Green > 255) printf("\n\aNumber is out of range\a:"); } while ((Green < 0) || (Green > 255)); do { printf("\nBLUE:\t"); scanf("%i", &Blue); if (Blue < 0 || Blue > 255) printf("\n\aNumber is out of range:"); } while ((Blue < 0) || (Blue > 255)); do { printf("\nPlease enter the X co-ordinate for the start of the line: "); scanf("%i", &X_start); if(X_start > 256) printf("\a\nCo-ordinate is out of range\a:"); } while ((X_start > 256)); do { printf("\nPlease enter the Y co-ordinate for the start of the line: "); scanf("%i", &Y_start); if (Y_start > 128) printf("\n\aCo-ordinate is out of range\a:"); } while ((Y_start > 128)); do { printf("\nPlease enter the X co-ordinate for the end of the line:\t "); scanf("%i", &X_end); if (X_end > 256) printf("\n\aCo-ordinate is out of range\a:"); } while ((X_end > 256)); do { printf("\nPlease enter the Y co-ordinate for the end of the line: "); scanf("%i", &Y_end); if (Y_end > 128) printf("\n\aCo-ordinate is out of range\a:"); } while ((Y_end > 128)); if (X_start>X_end) //If statement to check if x value for start of line is greater than end of line { X2=X_start; //If so, X2 becomes X co-ordinate of start of line X1=X_end; //X1 becomes X co-ordinate of end of line } else { X2=X_end; //Otherwise X2 becomes X co-ordinate for end of line X1=X_start; //and X1 becomes X co-ordinate for the start of the line } if (Y_start>Y_end) //if statement to check if Y value for start of line is greater than that for end of line { Y2=Y_start; //If so, Y2 becomes Y co-ordinate of start of line Y1=Y_end; //Y1 becomes X co-ordinate of end of line } else { Y2=Y_end; //Otherwise Y2 becomes Y co-ordinate for end of line Y1=Y_start; //and Y1 becomes Y co-ordinate for the start of the line } m= abs((Y_end-Y_start)/(X_end-X_start)); //Calculating the gradient a=Y1; //Equating 'a' variable to max Y value b=X2; //Equating 'b' variable to min X value if ( m <=1) { printf("Gradient: %f \n", m);//Condition for if gradient is less than or equal to 1 for (x=X1; x<=X2; x++) // For when x variable = X1, less than or equal to X2, and incremented by 1 { y=a; if ((x>=0 && x<=255) && (y>=0 && y<=127)) { image [x][y][RED]=Red; image [x][y][GREEN]=Green; //Defines array of red green and blue image [x][y][BLUE]=Blue; } else { } } } else { m=1/m; printf("Gradient is: %i\n", m); for (y=Y1; y<=Y2; y++) // For when y variable = Y1, less than or equal to Y2, and incremented by 1 { (float)b=(float)(b+m); x=b; if ((x>=0 && x<=255) && (y>=0 && y<=127)) { image [x][y][RED]=Red; image [x][y][GREEN]=Green; image [x][y][BLUE]=Blue; } else { } } } fprintf(pfile, "P3\n256 128\n255\n\n"); for(y=0; y<128; y++) { for(x=0; x<256; x++) { fprintf(pfile, "%d ", image [x][y][RED]); fprintf(pfile, "%d ", image [x][y][GREEN]); fprintf(pfile, "%d ", image [x][y][BLUE]); } } fclose(pfile); scanf("%*s"); }