Search code examples
c++coordinatesmoveinfinity

Why does the program increase the value of the x and the y even after they are bigger, then they should be?


I'm making a program, that moves a dot, from the startx, starty to endx, endy. But it moves continuously, even after the x and the y are bigger than endx and endy. Here is the code:

int x, y;
            x = startx; y = starty;
            if(startx<goalx){
                if (starty < goaly) {
                    while (startx < goalx || starty < goaly) {
                        x += sqrt(2)*speedx;
                        y += sqrt(2)*speedy;
                    }
                }
                else {
                    while (startx < goalx || starty > goaly) {
                        x += sqrt(2)*speedx;
                        y -= sqrt(2)*speedy;
                    }
                }
            }
            else {
                if (starty < goaly) {
                    while (startx > goalx || starty < goaly) {
                        x -= sqrt(2)*speedx;
                        y += sqrt(2)*speedy;
                    }
                }
                else {
                    while (startx > goalx || starty > goaly) {
                        x -= sqrt(2)*speedx;
                        y -= sqrt(2)*speedy;
                    }
                }


            }
            cout << x << ", " << y << endl;

startx, starty, goalx and goaly are user inputs.


Solution

  • The control structures depend on startx and starty but these are never changing. Supposedly x and y should be used instead.