I have written a code to be able to produce an image where i can choose a background colour and then draw a line of chosen colour on top. The problem I am having is that I can't get the program to be able to produce vertical and horizontal lines as well as the diagonal lines that my current code is able to produce. Also when I try and draw lines of gradient greater than 1, large gaps appear in the line. How can I stop this from happening?

I have attached my code in a text file, but have also shown my current code below:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

int wait;
#define wait scanf("%d", &wait);

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

#define printdebug 0
#define print p=image[Y][X][0,1,2]; printf("%d", p);
#define deb printf("0");


void main(void)


{	
	int REDVAL1=-1,GREENVAL1=-1,BLUEVAL1=-1,REDVAL2=-1,GREENVAL2=-1,BLUEVAL2=-1;// VARIABLES COLOUR IMAGE
	int REDVAL=-1,GREENVAL=-1,BLUEVAL=-1;										// COLOUR COMPONENTS OF LINE
	int X=0, Y=0, p=0;															//1,2 dimension of 3d array
	int image[YSIZE][XSIZE][3];													//int of 3d array "image"
	float gradredval=0, gradgreenval=0, gradblueval=0;							// int var for colour transition.
	int x1=-1,x2=-1,y1=-1,y2=-1;												// int var for line co-ord.
	float ly=0, linegrad, ln;													// ly,ln for smoothing of line.
	
//ASK USER FOR INPUT
	while (((REDVAL1>255)||(GREENVAL1>255)|| (BLUEVAL1>255)||(REDVAL1<0)||(GREENVAL1<0)|| (BLUEVAL1<0))){
		printf("ENTER 3 COLORS COMPONENTS FOR LEFT SIDE IMAGE\n");
		scanf("%d%d%d" ,&REDVAL1,&GREENVAL1,&BLUEVAL1);
	}

	printf("ENTER 3 COLORS COMPONENTS FOR RIGHT SIDE IMAGE\n");
	while (((REDVAL2>255)||(GREENVAL2>255)|| (BLUEVAL2>255)||(REDVAL2<0)||(GREENVAL2<0)|| (BLUEVAL2<0))){
		scanf("%d%d%d" ,&REDVAL2,&GREENVAL2,&BLUEVAL2);
	}
	
	while (((REDVAL>255)||(GREENVAL>255)|| (BLUEVAL>255)||(REDVAL<0)||(GREENVAL<0)|| (BLUEVAL<0))){
		printf("ENTER 3 COLORS COMPONENTS FOR THE LINE\n");
		scanf("%d%d%d" ,&REDVAL,&GREENVAL,&BLUEVAL);
	}

	while ((x1>255)||(y1>128)||(x1<0)||(y1<0)){
		printf("ENTER COORDINATE OF POINT A\n");
		scanf("%d%d" ,&x1,&y1);
	}

		while ((x2>255)||(y2>128)||(x2<0)||(y2<0)){
		printf("ENTER COORDINATE OF POINT B\n");
		scanf("%d%d" ,&x2,&y2);
	}
	
	// initialise the array WITH VALUES "0"		
//*********************************	
	
	for (Y=0; YSIZE!=Y ;Y++){
		for (X=0; XSIZE!=X ;X++){
			image[Y][X][RED]=0 ;image[Y][X][GREEN]=0;image[Y][X][BLUE]=0;
		}
	}

//**********************************
// To store 2 colours at different half of the image.
//**********************************
	for (Y=0; YSIZE!=Y ;Y++){
		for (X=0; XSIZE!=X ;X++){
			image[Y][X][RED]=REDVAL1 ;image[Y][X][GREEN]=GREENVAL1;image[Y][X][BLUE]=BLUEVAL1;
		}
	}
// ********************************* 
// 1st colour is applied to whole image. Now needs applying to right half of image.
	for (Y=0; YSIZE!=Y ;Y++){
		for (X=(XSIZE*0.5); XSIZE!=X ;X++){	
			image[Y][X][RED]=REDVAL2 ;image[Y][X][GREEN]=GREENVAL2;image[Y][X][BLUE]=BLUEVAL2;
		}
	}

//**********************************
//Transition Gradient
//**********************************
// We need to have gradient for each colour. So 3 gradients.  


	gradredval= (((float)(REDVAL2 - REDVAL1))/((float)(XSIZE-1)));
	gradgreenval=(((float)(GREENVAL2 - GREENVAL1))/((float)(XSIZE-1)));
	gradblueval= (((float)(BLUEVAL2 - BLUEVAL1))/((float)(XSIZE-1)));
	
	for (Y=0; YSIZE!=Y ;Y++){
		for (X=0; XSIZE!=X ;X++){
			image[Y][X][RED]=(REDVAL1+(gradredval*X)) ;image[Y][X][GREEN]=(GREENVAL1+(gradgreenval*X));image[Y][X][BLUE]=(BLUEVAL1+(gradblueval*X));
		}
	}

//**********************************
//Two Lines
//**********************************

	linegrad=(((float)(y2-y1))/((float)(x2-x1)));
	
	if (y2>y1)
	{
		for (Y=y1; y2!=Y ;Y++){
			if (x2>x1)
			{	
				for (X=x1; x2!=X ;X++){	
					ly= ((linegrad*((float)(X-x1)))+y1);
					ln=ly-((int)ly);
					
					if (ln>0.5)
					{
						image[((int)ly)+1][X][RED]=REDVAL ;image[((int)ly)+1][X][GREEN]=GREENVAL;image[((int)ly)+1][X][BLUE]=BLUEVAL;
					}
					else
					{
						image[(int)ly][X][RED]=REDVAL ;image[(int)ly][X][GREEN]=GREENVAL;image[(int)ly][X][BLUE]=BLUEVAL;
					}
				}
			}
			else
			{	
				for (X=x2; x1!=X ;X++){	
					ly= ((linegrad*((float)(X-x1)))+y1);
					ln=ly-((int)ly);
					
					if (ln>0.5)
						{
							image[((int)ly)+1][X][RED]=REDVAL ;image[((int)ly)+1][X][GREEN]=GREENVAL;image[((int)ly)+1][X][BLUE]=BLUEVAL;
						}
					else
						{
							image[(int)ly][X][RED]=REDVAL ;image[(int)ly][X][GREEN]=GREENVAL;image[(int)ly][X][BLUE]=BLUEVAL;
						}
				}
			}
		}

	}
	else
	{
		for (Y=y2; y1!=Y ;Y++){
		
			if(x2>x1)
			{
				for (X=x1; x2!=X ;X++){
					ly= ((linegrad*((float)(X-x1)))+y1);
					ln=ly-((int)ly);
				
					if (ln>0.5){
						image[((int)ly)+1][X][RED]=REDVAL ;image[((int)ly)+1][X][GREEN]=GREENVAL;image[((int)ly)+1][X][BLUE]=BLUEVAL;
					}
					else{
						image[(int)ly][X][RED]=REDVAL ;image[(int)ly][X][GREEN]=GREENVAL;image[(int)ly][X][BLUE]=BLUEVAL;
					}
				}
			}
			else
			{
			
				for (X=x2; x1!=X ;X++){	
					ly= ((linegrad*((float)(X-x1)))+y1);
					ln=ly-((int)ly);
					
					if (ln>0.5){
						image[((int)ly)+1][X][RED]=REDVAL ;image[((int)ly)+1][X][GREEN]=GREENVAL;image[((int)ly)+1][X][BLUE]=BLUEVAL;
					}
					else{
						image[(int)ly][X][RED]=REDVAL ;image[(int)ly][X][GREEN]=GREENVAL;image[(int)ly][X][BLUE]=BLUEVAL;
					}
				}
			}

		}

	}

// Print the the remaining data stored in the array to the *.ppm file
//***********************************************************************************
	{
		FILE*pfile=NULL; /* Declaration of Pointer */
		pfile = fopen("myfile.ppm", "w"); /*Open myfile.ppm to write it */
		fprintf(pfile,"P3\n# %s\n %d %d\n255\n", "myfile.ppm", XSIZE, YSIZE );
		
		for (Y=0; YSIZE!=Y ;Y++){
			for (X=0; XSIZE!=X ;X++){
				fprintf (pfile,"%d %d %d\n", image[Y][X][RED] ,image[Y][X][GREEN],image[Y][X][BLUE]);
			}
		}
		fclose(pfile);
	}
	
	printf(" :)PRINTED TO FILE ""MY FILE .*ppm""(:");
	wait
}

If able, i would greatly appreciate any advice or tips that you can offer to help with the alteration of my code.

Many Thanks,
Andrew

If able, i would greatly appreciate any advice or tips that you can offer to help with the alteration of my code.

Good. Since you asked, and I can barely follow your code the way it's indented, I highly suggest
1) using a consistent format, especially with { & }
2) using better and consistent indentation
3) better use of whitespace
See this

First of all, explain the loops

for (Y=y1; y2!=Y ;Y++){

at lines 110 and 144. I am asking because the loop parameter Y is not used in the loop body.

Second, (re)read an article on a Bresenham algorithm until you understand completely how it works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.