Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
443 views
in Technique[技术] by (71.8m points)

c++ - 为什么即使x和y变大了,程序为什么也要增加x和y的值呢?(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.

(我正在编写一个程序,该程序将一个点从startx,starty移到endx,endy。)

But it moves continuously, even after the x and the y are bigger than endx and endy.

(但是,即使x和y大于endx和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.

(startx,starty,goalx和Goaly是用户输入。)

  ask by Gerg? Szabó translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The control structures depend on startx and starty but these are never changing.

(控制结构取决于startxstarty但它们从未改变。)

Supposedly x and y should be used instead.

(假定应该使用xy代替。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...