This is what your code would look like with indentation:
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
int 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");
}
For one thing you really should read about functions, as there is very much redundant code in there (all the input loops could be handled by a single function.
Now, to the gradient calculation. Are you sure that you need to use abs there? I'm not terribly strong in math, but I seem to remember that the sign of the gradient carries important information about the direction of the line.
As for the output loops
Code:
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 {}
}
You are not changing y at all in this loops (or x in the following loop). Of course this can only produce horizontal/vertical lines. Did you mean to add gradient to y each time through the loop or something like that?