简单的地图绘制问题

Simple Map Plotting Issue

本文关键字:问题 绘制 地图 简单      更新时间:2023-10-16

我已经编写了一个简单的地图绘图程序,但有一些我无法识别的错误。

  1. 该错误仅在 X 坐标为正时发生,当它是负值时可以。
  2. 为什么当我的范围只有 11 时打印最后一列点?

代码如下:

int xRange = 11;
int yRange = 11;
string _space = "   ";
string _star = " * ";
for( int x = xRange; x > 0; x-- )
{
    for( int y = 0; y < yRange; y++ )
    {
        int currentX = x - 6;
        int currentY = y - 5;
        //demo input
        int testX = 2; //<----------ERROR for +ve int, correct for -ve
        int testY = -4; //<-------- Y is working ok for +ve and -ve int
        //Print x-axis
        if( currentY == 0 )
        {
            if( currentX < 0 )
                cout << currentX << " ";
            else
                cout << " " << currentX << " ";
        }
        //Print y-axis
        if( currentX == 0 )
        {
            if( currentY < 0 )
                cout << currentY << " ";
            else
                //0 printed in x axis already
                if( currentY != 0 )
                    cout << " " << currentY << " ";
        }
        else if( currentY == testX and currentX == testY )
            cout << _star;
        else
            cout << " . ";
    }
    //print new line every completed row print
    cout << endl;
}

演示输入的输出 (x: 2, y: -4): (它在 3 处显示 x 这是错误的)

 .  .  .  .  .  5  .  .  .  .  .  . 
 .  .  .  .  .  4  .  .  .  .  .  . 
 .  .  .  .  .  3  .  .  .  .  .  . 
 .  .  .  .  .  2  .  .  .  .  .  . 
 .  .  .  .  .  1  .  .  .  .  .  . 
-5 -4 -3 -2 -1  0  1  2  3  4  5 
 .  .  .  .  . -1  .  .  .  .  .  . 
 .  .  .  .  . -2  .  .  .  .  .  . 
 .  .  .  .  . -3  .  .  .  .  .  . 
 .  .  .  .  . -4  .  .  *  .  .  . 
 .  .  .  .  . -5  .  .  .  .  .  .

演示输入的输出 (x: -2, y: 4):

 .  .  .  .  .  5  .  .  .  .  .  . 
 .  .  .  *  .  4  .  .  .  .  .  . 
 .  .  .  .  .  3  .  .  .  .  .  . 
 .  .  .  .  .  2  .  .  .  .  .  . 
 .  .  .  .  .  1  .  .  .  .  .  . 
-5 -4 -3 -2 -1  0  1  2  3  4  5 
 .  .  .  .  . -1  .  .  .  .  .  . 
 .  .  .  .  . -2  .  .  .  .  .  . 
 .  .  .  .  . -3  .  .  .  .  .  . 
 .  .  .  .  . -4  .  .  .  .  .  . 
 .  .  .  .  . -5  .  .  .  .  .  .

谁能帮助确定我的代码中的两个问题?谢谢。

if( currentY == testX and currentX == testY )

这看起来不对。你不应该比较X到X和Y与Y吗?

仔细一看,一切都更加奇怪了。外部循环生成行,但使用 x 索引它们。内部循环为每一行生成列,您可以使用 y 对其进行索引。对于哪个轴是X轴,哪个轴是Y轴,人们普遍存在混淆。

编辑:啊,我现在看到了问题。currentY == 0 时,打印轴的数字,打印点。

问题是,当你打印 Y 轴时,你仍然打印一个点,所以 y 轴右侧的所有内容都移动了 1。 你应该在那里有另一个else

if( currentY == 0 )
{
    ....
}
else if (currentX == 0)  // <--- add an else there
{
    ....
}
else if ...