moving a circle on a line

Hi .
Could someone please help me with this ?
this is a simple board made by circles . three small circles will be thrown and land on a random point in the board range one after one .
could someone please tell me what causes my code not to work properly ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <graphics.h>
#include <time.h>

using namespace std;

int main()
{
    int g=0,m=0,x=0,y=0,t,u,z;
    srand(time(NULL));
    initgraph(&y ,&m,"");
    initwindow(800 ,600 , " Dart Random" );
    setcolor(15);
    circle(x+400, 250 + y, 1);
    circle(x+400, 250 + y, 30);
    circle(x+400, 250 + y, 45);
    circle(x+400, 250 + y, 60);

    for(int i=1 ; i<=3 ; i++)
    {
        int x = (rand()%800) + 1;
        int y = (rand()%600) + 1;
        Sleep(100);
        if(x<346 && x>454 && y>310 && y<36) continue;
        else
        {
            x++;
            y++;
            circle(250+x , 250+y , 2);
        }
    }




    getch();
    return 0;
}
What do you mean by 'not to work properly'?

Do to using a very old compiler (turbo c), it is not possible to reproduce your problem.
no no that's not cause of compiler . well i dont use TC to compile .
Well there's something wrong in my code . the three small circles should start moving from x=250 and y=250 and stop at a random place on the range that i specified (inside the big circles) . but that doesn't even move .
well i dont use TC to compile .
What else do you use?


You loop just shows three [small] circles (if line 24 allows it).

What you need to do is:
Determine the start/end coodinates outside the loop.
Calculate the slope between both coodinates.
Calculate the move coordinates according to this slope.

You also need to determine the step size.


Each 'physical' move requires to actions:
1. Clear the circle at the current position.
2. Draw the circle at the new position.
Your condition on line 34 is wrong. Consider this part: x<346 && x>454. What number is less than 346 and also greater than 454? I suspect you "or" instead of "and", so those &&'s should be ||'s instead.
Well they told us to install CodeBlocks 10.05 as our IDE , and import graphics.h to it .
That's stupid somehow .
So , i use MinGW compiler right now .

Thanks so much for the help coder777 , I'm sorry for asking one more question but could you please tell me what will the counter inside of the "for" loop be used for?
@dhayden those numbers specify the range where the small circle should fall (big circles) .
which is between 346 and 454 on x .
Could you maybe post all the code now ? I am sure the problems are not that difficult to solve if you can get us the whole picture.
@Thomas1965 Uh well the code is somehow ruined now :s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <graphics.h>
#include <time.h>

using namespace std;

int main()
{
    int g=0,m=0,x1,a,y1,x=0,y=0,b;
    srand(time(NULL));
    initgraph(&y ,&m,"");
    initwindow(800 ,600 , " Dart Random" );
    setcolor(15);
    circle(x+400, 250 + y, 1);
    circle(x+400, 250 + y, 30);
    circle(x+400, 250 + y, 45);
    circle(x+400, 250 + y, 60);


    //y=ax+b


    for(int i=0 ;i<=3  ;i++ )
    {
            int x2 = (rand()%800) + 1;
            int y2 = (rand()%600) + 1;
            x1=250;
            y1=250;
            if(x2<346 && x2>454 && y2>310 && y2<36) continue;
            a=(y2-y1)/(x2-x1);
            b=y1/(a*x1);
            circle(x1*b , y1 ,3 );

    }




    getch();
    return 0;
}


the code is supposed to work like a dart which gets thrown on a board .
I really apologize cause i keep this topic going on
@Molly,

I created a project in CodeBlocks but couldn't compile it. It seems graphics.h is not included with CodeBlocks. Could you compile it?

I really apologize cause i keep this topic going on

No problem.
Well graphics.h was made for Turbo C especially , 30 years ago! nowadays its not supported by compilers and we had to import it ourself , because our teacher said so!!!
I don't know why do they still teach c++ by using such old libraries here in Iran .
Yes it is really strange to use such old stuff specially when there is so many free alternatives available.
If found this tutorials about graphics and TurboC, maybe it will help you.

http://www.phanderson.com/graphics/
http://electrosofts.com/cgraphics/
http://www.softwareandfinance.com/Turbo_C/Graphics/Index.html
Oh thanks I'll take a look
what will the counter inside of the "for" loop be used for?
Well, if you use sleep for 1/10 seconds and you want that animation for 1 second then

count = 10

You don't need to calculate the slope in each itaration. You can do that outside the loop. Next thing you need is the distance between the coordinates.

step_size = distance / count

Since step_size is basically the distance to the new position you need the reverse distance formula to calculate all the intermediate steps.

To make the 'physical' move you may clear the background and draw all the circles again, but there is a simpler method to avoid that flicker:

Draw the circel to move with the background color and the draw all circles including the moved circle again.
@dhayden those numbers specify the range where the small circle should fall (big circles) .
which is between 346 and 454 on x .

I understand, but my point still stands: no number is less than 346 AND greater than 454. You need to change the condition .
Topic archived. No new replies allowed.