|
程序中,黄色点的轨迹所成的直线应该在x=100-400,y=100-300之间显示,为什么左右下三个方向都越界了呢?(三个方向只越出1)哪位大虾帮忙看看。
#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;
}
|
|