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:
I then want to provide a user interface able to accept these commands:
Here is my code that I need to implement:
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.
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
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
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");
}
}
I am sorry if it is hard to read, I am trying to learn.