|
25 Years of Programming
An open source source for C, C++, OWL, BASIC, MDB, XLS, DOT, and more... |
Home Projects Up Sitemap Search Blog Forum+Chat About Us Privacy Terms of Use Feedback FAQ Images Services Ads Donate Humor |
|
|
Normal.c - normal distributionDrops a particle from the top center of the screen, and then makes random left/right decisions about which way to go on the way down, and as a result builds a normal distribution at the screen bottom. Other versions:
|
|
/* normal.c 5-21-93
Copyright (C)1993 Steven Whitney.
Published under GNU GPL (General Public License) Version 2, with ABSOLUTELY NO WARRANTY.
Initially published by http://25yearsofprogramming.com
DeSmet C for the Heathkit H-100 computer. It uses some H-19 terminal escape codes.
THIS BEGAN AS A C CONVERSION OF MY NORMAL.BAS PROGRAM, WHICH DROPS A PARTICLE
FROM THE TOP CENTER OF THE SCREEN, AND THEN MAKES RANDOM LEFT/RIGHT DECISIONS
ABOUT WHICH WAY TO GO ON THE WAY DOWN, AND AS A RESULT BUILDS A NORMAL DISTRIBUTION
AT THE BOTTOM OF THE SCREEN. HOWEVER, I THINK I ADDED THE "STICKY" ROUTINES FROM DLA.C,
SO IT MAKES STRANGE TREELIKE FORMATIONS AT THE SCREEN BOTTOM, INSTEAD.
*/
#include "stdio.h"
/* One version of DeSmet frand() had a bug. It sometimes returned a negative number. */
double posrnd()
{
double r;
do
{
r = frand();
}
while((r < 0.) || (r == 1.));
return r;
}
main(argc,argv)
int argc;
char **argv;
{
char ch = '\0'; /* user input */
int i; /* counter */
int x, y; /* screen coordinates */
int xi, yi; /* temp save of old coordinates */
int test; /* cumulative test of surrounding points */
int seed, random;
int show = TRUE; /* whether or not to show point's path */
int trail = FALSE; /* erase current point before plotting next? */
int drawcirc = TRUE; /* whether to start by drawing a circle */
FILE *infile;
cls();
puts("Usage: NORMAL option option ...\n");
puts("\nOptions:\n");
puts("H Display this help screen and exit\n");
puts("Lfname Load file before starting\n");
puts("\n\nWhile program is running:\n");
puts("Q Quit\n");
puts("S Toggle show-points flag\n");
puts("T Toggle show point's entire path flag\n");
puts("\nEnter random number seed (-32768 to 32767): ");
scanf("%d",&seed);
srand(seed);
puts("\033x1\033E\033x5"); /* enable 25th line, cls, cursor off */
for(i = 1 ; i < argc ; i++)
{
if(tolower(argv[i][0]) == 'h')
exit(0);
if(tolower(argv[i][0]) == 'l')
{
zedload(&(argv[i][1]));
drawcirc = FALSE;
break;
}
}
if(drawcirc)
line(0,224,639,224,1);
x = 320;
y = 1;
do
{
if(ch == 't')
trail = !trail;
if(ch == 's')
show = !show; /* toggle */
if(show)
pset(x,y,2);
test = 0;
test |= point(x,y+1);
test |= point(x-1,y);
test |= point(x+1,y);
/*
test |= point(x+1,y+1);
test |= point(x-1,y+1);
*/
if((test & ~2) != 0) /* if it hits another point, */
{ /* that is not red */
pset(x,y,4); /* it sticks to it */
x = 320; /* start again from center */
y = 1;
}
else
{
xi = x; yi = y;
y++;
switch(random = (int)(posrnd() * 2))
{
case 0: x += (int)(posrnd() * 2); break;
case 1: x -= (int)(posrnd() * 2); break;
default:
putchar(7);
locate(24,1);
printf("%d",random);
break;
}
if(show && !trail)
pset(xi,yi,0); /* turn it off */
} /* before moving to next point */
}
while((ch = tolower(csts())) != 'q');
zedsave();
puts("\033z"); /* reset terminal */
exit(0);
}
|
|
|
|
|
|