| |

VerySource

 Forgot password?
 Register
Search
View: 1027|Reply: 8

Help me see why it crossed the line?

[Copy link]

2

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-2-11 20:00:02
| Show all posts |Read mode
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? Which prawn helps 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;
}
Reply

Use magic Report

0

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-4-10 15:00:02
| Show all posts
if (x> = 400) x = 100 + x% 400;
Original 100 = <x <= 400
After x = 100 + x% 400, 200 = <x <= 500
After the if (x> = 400) x = 100 + x% 400 statement, the possible value of x is 500
Reply

Use magic Report

0

Threads

9

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-7-29 23:30:01
| Show all posts
{j=(float)rand()/32767*300+100;
-----------------------
Before this, first use srand() to set a seed, and then get random numbers

Like here
srand(300);//The maximum will not exceed 300.

x=rand() + 100;//This is in 100 to 400,

y=rand(); so it is 100-300
Reply

Use magic Report

0

Threads

78

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 China

Post time: 2020-8-2 19:00:01
| Show all posts
srand(300);//The maximum will not exceed 300.
=================
ms srand does not have such a restrictive effect ·····
Reply

Use magic Report

0

Threads

78

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 China

Post time: 2020-8-2 19:15:01
| Show all posts
hile(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);
}

Here x and y are not properly controlled ····
Reply

Use magic Report

0

Threads

78

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 China

Post time: 2020-8-2 19:45:01
| Show all posts
if(x>=400) x=100+x%400;
if(x<100) x=300+x%100;

These two sentences can be removed,
Because before the while,
j=(float)rand()/32767*300+100;
x=(int)j; //Here has been limited x between 100-400
Reply

Use magic Report

0

Threads

9

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-8-2 20:15:01
| Show all posts
What are you talking about?

if(x>=400) x=100+x%400;

In fact, this value can also exceed 400, for example when X=799

if(799>=400) x=100+799%400 == 100 + 399 == 499

So here should be changed to: if(x>=300) x=100+x%300;

But I don’t know whatzjwzjwprawn said "ms srand does not have such a restrictive effect..." I am a little puzzled! Maybe I haven't programmed for a long time. . . . . . . . . .
Reply

Use magic Report

0

Threads

9

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-8-2 20:30:01
| Show all posts
Hehe,zjwzjwis still clear, I have used it before, but I haven't understood it deeply! Stupid donkey. . . . . . . . .


The explanation for random numbers is this:
_ Although there is no real random number generator in the computer, it is possible to repeat the generated numbers
The frequency is very low, consistent with their seeming random. The program <function> that realizes this function is called a pseudo-random number generator.
_There are many theories about how to generate random numbers, and they can be discussed in a thick book. :) (mainly mathematical knowledge) So what we care about is how to generate random numbers.
_ _ No matter what method you use to implement a random number generator, you must provide it with a "seed"
The initial value of, and the value itself is preferably random. This value is generally generated using fast counting registers or shift registers. _But in practice, we generally use time values ​​instead.
_Current C compilers provide a pseudo-random number generator function based on ANSI standards to generate random numbers.
Both MS and BORLAND support this standard through srand() and rand().
_ Their workflow is as follows:
(1): First, provide a "seed" to srand(), which is a value of type unsigned_int.
(2):_Then, call rand(), it will return a random number according to the value provided to srand() (range between _0~32767)
(3): Call rand() as many times as needed to get new random numbers continuously.
(4): Whenever a new "seed" can be provided to srand() to further "randomize" rand()
Output the result.
Reply

Use magic Report

0

Threads

78

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 China

Post time: 2020-8-2 20:45:01
| Show all posts
The while part is modified as follows: (there is no other modification)

while(y<300) /* =300 is the exit condition, so modify it to <300 */
{
a=(float)rand()/32767;
if(a<pf) y++;
else if(a<=pf+ps) x--;
else if(a<=1) x++;

if(x>400) x=400; /*Add three constraints*/
if(x<100) x=100;
if(y>300) y=300; /*y is a single increment, so one condition is enough*/

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);
}
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list