| |

VerySource

 Forgot password?
 Register
Search
View: 1030|Reply: 4

The master helped 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-17 14:30:01
| 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? (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;
}
Reply

Use magic Report

0

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-4-20 18:30:01
| Show all posts
Need to debug, it is recommended to use lz to track on tc, will it be a "pixel" upward shift when floating-point to integer conversion?
Reply

Use magic Report

2

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-4-21 11:00:01
| Show all posts
But while (y <= 300) should define the coordinates in the Y direction;
     if (x> = 400) x = 100 + x% 400;
     if (x <100) x = 300 + x% 100; defines the coordinates in the X direction, even if there is an offset during the plastic transformation, it should be determined by the above boundary first
Reply

Use magic Report

0

Threads

18

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

Post time: 2020-5-15 10:45:01
| Show all posts
j = (float) rand () / 32767 * 300 + 100;
   x = (int) j;


J, just dive right.\
Reply

Use magic Report

2

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-5-24 21:00:02
| Show all posts
I can copy this code directly to the text and run it with TC, N can enter tens to hundreds randomly, pf should be a decimal between 0-1
I can't figure out why it crosses the boundary 1. .
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