Code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include "gd.h"
#define SIZE 800 /* Cannot be changed because of lack of implementation of C99 with free Borland compiler. It can be modified by the user before compilation.*/
#define TRUE 1
#define FALSE 0
void initialize (int start[][SIZE], int cells); /* Creates array.*/
void atom (int array[][SIZE]); /* Moves the atom.*/
int direction (int side); /* Chooses direction of movement. */
int main (void)
{
int start_atoms, enter_atoms;
int i,x,y;
int grid[SIZE][SIZE];
int black, white;
gdImagePtr im;
FILE *pngout, *jpegout;
/* Gets the number of atoms to scatter in the grid. */
printf("\nPlease enter the number of starting atoms: ");
scanf("%d", start_atoms);
/* Gets the number of atoms to fire into the grid. */
printf("\nPlease enter the number of atoms to be fired into the grid: ");
scanf("%d", enter_atoms);
initialize (grid, start_atoms);
for (i = 0; i <= enter_atoms; i++)
{
atom (grid);
}
im = gdImageCreate(SIZE,SIZE);
black = gdImageColorAllocate(im, 0, 0, 0);
white = gdImageColorAllocate(im, 255, 255, 255);
for (x = 0; x < SIZE; x++)
{
for (y = 0; y < SIZE; y++)
{
if (grid[x][y] == TRUE)
gdImageSetPixel (im, x, y, black);
else gdImageSetPixel (im, x, y, white);
}
}
jpegout = fopen("output.jpg", "wb");
gdImageJpeg(im, jpegout, 0);
fclose(jpegout);
gdImageDestroy(im);
printf("Image output.jpg written to the disk.");
return 0;
}
void initialize (int start[][SIZE], int cells) /* Creates an empty array, then populates with a random assortment of atoms in existence. */
{
int x, y, i=0;
for (x = 0; x < SIZE; x++)
{
for (y = 0; y < SIZE; y++)
{
start[x][y] = 0;
}
}
while (i <= cells)
{
int x = (rand() % (SIZE-10) + 5); /* Creates the atoms in the middle of the map, leaving the borders empty, giving the new atoms a chance to get into the map and not block off the sides. */
int y = (rand() % (SIZE-10) + 5);
if (start[x][y] == FALSE)
{
start[x][y] = TRUE;
i++;
}
}
}
void atom (int array[][SIZE])
{
/* Four sides: 0=top, 1=right, 2=bottom, 3=left.
* When a given side is chosen to start from, the opposite side will be selected as the preferred direction of travel.
* This function only controls the movement for a single atom.
*/
int side = rand() % 4;
int cont = TRUE;
int x, y;
/* Sets the starting point of the atom, given the starting side.*/
switch (side)
{
case 0:
x = rand() % SIZE;
y = 0;
break;
case 1:
x = SIZE - 1;
y = rand() % SIZE;
break;
case 2:
x = rand() % SIZE;
y = SIZE - 1;
break;
case 3:
x = 0;
y = rand() % SIZE;
break;
}
/* Moves the atom one cell at a time.
* First it moves one space.
* If it has left the grid, then the loop quits.
* If it has not left the grid, then it checks for any neighbors. If it has a neighbor, it sets the appropriate array space to TRUE.
*/
while (cont == TRUE)
{
int dir = direction (side);
/* Moves the atom based on the pseudo-random direction. */
if (dir == 0) x -= 1;
else if (dir == 1) y += 1;
else if (dir == 2) x += 1;
else y -= 1;
/* If the atom has left the grid, the loop quits. */
if (x < 0 || y < 0 || x >= SIZE || y >= SIZE) cont = FALSE;
else if (array[x+1][y] == TRUE || array[x-1][y] == TRUE || array [x][y+1] == TRUE || array[x][y-1] == TRUE)
{
array[x][y] = TRUE;
cont = FALSE;
}
/* If cont hasn't been changed, then the loop repeats and the atom moves again. */
}
/* Function will end with either the atom leaving the grid or getting attached to another atom. */
}
int direction (int side)
{
/* To set the propensity to move in a direction, the opposide side of the starting side needs to be selected.
* Opposite side of entrance is given a 40% chance of being chosen, the other three sides get 20% chance each.
*/
int propensity = (side + 2) % 4;
int direction = rand() % 5;
if (direction == 4) /* If the direction happens to be 4, which is an invalid variable, it is reset to the direction of propensity, giving it the extra 20%. */
{
direction = propensity;
}
return direction;
}
What am I missing here? All I want is a direction to go in, and I'm kind of stumped. I'm going to try dumping the variable contents to a log file, but I only "kind of" know how to manipulate text files.