为什么程序增加了 x 和 y 的值,即使它们更大了,那么它们应该是?
Why does the program increase the value of the x and the y even after they are bigger, then they should be?
我正在制作一个程序,它移动一个点,从 startx, starty 到 endx, endy。
但它连续移动,即使在 x 和 y 大于 endx 和 endy 之后。
这是代码:
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 和 goaly 是用户输入。
控制结构取决于 startx
和 starty
,但它们永远不会改变。应该使用 x
和 y
代替。
试试这个
int x, y;
x = startx; y = starty;
int xInc = sqrt(2) * speedx;
int yInc = sqrt(2) * speedy;
if(x < goalx && y < goaly)
{
while(x < goalx || y < goaly)
{
x += xInc;
y += yInc;
}
}
else if(x < goalx && y > goaly)
{
while(x < goalx || y > goaly)
{
x += xInc;
y -= yInc;
}
}
else if(x > goalx && y < goaly)
{
while(x > goalx || y < goaly)
{
x -= xInc;
y += yInc;
}
}
else if(x > goalx && y > goaly)
{
while(x > goalx || y > goaly)
{
x -= xInc;
y -= yInc;
}
}
我正在制作一个程序,它移动一个点,从 startx, starty 到 endx, endy。 但它连续移动,即使在 x 和 y 大于 endx 和 endy 之后。 这是代码:
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 和 goaly 是用户输入。
控制结构取决于 startx
和 starty
,但它们永远不会改变。应该使用 x
和 y
代替。
试试这个
int x, y;
x = startx; y = starty;
int xInc = sqrt(2) * speedx;
int yInc = sqrt(2) * speedy;
if(x < goalx && y < goaly)
{
while(x < goalx || y < goaly)
{
x += xInc;
y += yInc;
}
}
else if(x < goalx && y > goaly)
{
while(x < goalx || y > goaly)
{
x += xInc;
y -= yInc;
}
}
else if(x > goalx && y < goaly)
{
while(x > goalx || y < goaly)
{
x -= xInc;
y += yInc;
}
}
else if(x > goalx && y > goaly)
{
while(x > goalx || y > goaly)
{
x -= xInc;
y -= yInc;
}
}