|
In the program, the straight line formed by the trajectory of the yellow point should be displayed between x = 100-400 and y = 100-300. Why are the three directions left and right beyond the boundary? (Only out of the three directions 1) Which prawn help to see.
#include <stdlib.h>
#include <graphics.h>
#include <stdio.h>
main ()
{float pf, ps, a, j;
int y, n, x, i;
int GraphDriver = DETECT, GraphMode;
initgraph (&GraphDriver,&GraphMode, "c:\\turboc\\cgi");
setbkcolor (GREEN);
bar (100,100,400,400);
setfillstyle (SOLID_FILL, RED);
bar (100,300,400,400);
printf ("\n\ninput the number of n:\n");
scanf ("% d",&n);
printf ("input the value of pf (0-1):");
scanf ("% f",&pf);
ps = (1-pf) / 2;
for (i = 0; i <n; i ++)
{j = (float) rand () / 32767 * 300 + 100;
x = (int) j;
y = 100;
while (y <= 300)
(if (x> = 400) x = 100 + x% 400;
if (x <100) x = 300 + x% 100;
a = (float) rand () / 32767;
if (a <pf) y ++;
else if (a <= pf + ps) x--;
else if (a <= 1) x ++;
putpixel (x, y, YELLOW);
if (y == 300) putpixel (x, y, BLUE);
if (getpixel (x-1, y) == 1) putpixel (x, y, BLUE);
if (getpixel (x + 1, y) == 1) putpixel (x, y, BLUE);
if (getpixel (x, y + 1) == 1) putpixel (x, y, BLUE);
}
}
getch ();
closegraph;
} |
|