【问题标题】:Trying to create a circle within a 2D array试图在二维数组中创建一个圆
【发布时间】:2018-04-19 05:41:21
【问题描述】:

我正在尝试编写一个用于创建 .pgm.ppm 的程序,并尝试使用二维数字数组绘制一个圆圈。使用给定的中心位置 (x,y) 和半径。这是drawCircle() 函数的代码。

void drawCircle(unsigned char pgmImage[][WIDTH], int height, int centerX, int centerY, int radius, unsigned char grayLevel) {

for (int rowIndex = centerY; rowIndex < 50; rowIndex++) {
    for (int colIndex = centerX; colIndex < 50; colIndex++) {
        if ((pow(colIndex - centerX, 2)) + (pow(rowIndex - centerY, 2)) <= pow(radius, 2)) {
            pgmImage[rowIndex][colIndex] = (grayLevel);
        }
    }
}

grayLevel 是我想要的圆圈的灰色阴影。我正在尝试使用公式(x-a)^2 + (y-b)^2 =r^2 绘制圆圈,其中 a 和 b 是我的中心 x 和 y。

【问题讨论】:

  • 您的帖子中没有“问题”。你问的是什么?
  • @YePhIcK 我需要帮助,我被卡住了,不知道为什么我的代码没有将圆圈写入数组。
  • 您的代码做什么?当你运行它时会发生什么。更重要的是:当您调试代码时,您会观察到什么?
  • 如果没有您提供stackoverflow.com/help/mcve,我们(SO 用户)很难真正帮助您。但是,查看您的代码,我注意到一些事情: 1)即使您的代码确实有效,它也最多会产生 1/4 的圆(因为您总是从 X 和 Y 迭代的中心开始); 2)代码滥用pow(),很可能是不必要的
  • 请显示您分配pngImage 数组并传递给drawCircle 的代码,并显示您如何“检查”结果。您是否写入文件并在图形工具中打开它?

标签: c++ arrays draw geometry


【解决方案1】:

当您的中心大于 50 时,我发现了问题。

您的循环初始化从中心开始。

但是,您将条件硬编码为始终小于50,这将是错误的。

也许你想从(0, 0)开始,直到((height - 1), (width - 1))

例如。

rowIndex (0, height]
colIndex: (0, width]

代码片段

for (int rowIndex = 0; rowIndex < height; rowIndex++) {
    for (int colIndex = 0; colIndex < width; colIndex++) {

【讨论】:

    【解决方案2】:

    我认为你的代码应该可以正常工作,前提是你正确计算了参数:

    constexpr int HEIGHT = 50, WIDTH = 50;
    
    void drawCircle(unsigned char pgmImage[][WIDTH], int height, int centerX, int centerY, int radius, unsigned char grayLevel) {
    
        for (int rowIndex = centerY; rowIndex < 50; rowIndex++) {
            for (int colIndex = centerX; colIndex < 50; colIndex++) {
                if ((pow(colIndex - centerX, 2)) + (pow(rowIndex - centerY, 2)) <= pow(radius, 2)) {
                    pgmImage[rowIndex][colIndex] = (grayLevel);
                }
            }
        }
    }
    
    int main() {
    
        unsigned char pgmImage[HEIGHT][WIDTH] = { {0} };
        int centerY = HEIGHT/2;
        int centerX = WIDTH/2;
        int radius = min(centerX,centerY) - 1;
    
        drawCircle(pgmImage, HEIGHT, centerX, centerY, radius, 1);
        for (int r=0;r<HEIGHT;r++) {
            for (int c=0;c<WIDTH;c++) {
                char o = (pgmImage[r][c] != 0) ? 'X' : '-';
                cout << o;
            }
            cout << endl;
        }
    }
    

    输出:

    --------------------------------------------------
    --------------------------------------------------
    --------------------------------------------------
    --------------------------------------------------
    --------------------------------------------------
    --------------------------------------------------
    --------------------------------------------------
    --------------------------------------------------
    --------------------------------------------------
    --------------------------------------------------
    --------------------------------------------------
    --------------------------------------------------
    --------------------------------------------------
    --------------------------------------------------
    --------------------------------------------------
    --------------------------------------------------
    --------------------------------------------------
    --------------------------------------------------
    --------------------------------------------------
    --------------------------------------------------
    --------------------------------------------------
    --------------------------------------------------
    --------------------------------------------------
    --------------------------------------------------
    --------------------------------------------------
    -------------------------XXXXXXXXXXXXXXXXXXXXXXXXX
    -------------------------XXXXXXXXXXXXXXXXXXXXXXXX-
    -------------------------XXXXXXXXXXXXXXXXXXXXXXXX-
    -------------------------XXXXXXXXXXXXXXXXXXXXXXXX-
    -------------------------XXXXXXXXXXXXXXXXXXXXXXXX-
    -------------------------XXXXXXXXXXXXXXXXXXXXXXXX-
    -------------------------XXXXXXXXXXXXXXXXXXXXXXXX-
    -------------------------XXXXXXXXXXXXXXXXXXXXXXX--
    -------------------------XXXXXXXXXXXXXXXXXXXXXXX--
    -------------------------XXXXXXXXXXXXXXXXXXXXXXX--
    -------------------------XXXXXXXXXXXXXXXXXXXXXX---
    -------------------------XXXXXXXXXXXXXXXXXXXXXX---
    -------------------------XXXXXXXXXXXXXXXXXXXXX----
    -------------------------XXXXXXXXXXXXXXXXXXXXX----
    -------------------------XXXXXXXXXXXXXXXXXXXX-----
    -------------------------XXXXXXXXXXXXXXXXXXX------
    -------------------------XXXXXXXXXXXXXXXXXX-------
    -------------------------XXXXXXXXXXXXXXXXX--------
    -------------------------XXXXXXXXXXXXXXXX---------
    -------------------------XXXXXXXXXXXXXXX----------
    -------------------------XXXXXXXXXXXXXX-----------
    -------------------------XXXXXXXXXXXX-------------
    -------------------------XXXXXXXXXX---------------
    -------------------------XXXXXXX------------------
    -------------------------X------------------------
    

    【讨论】:

    • 正如预期的那样,循环从中心开始,而不是在(0,0)
    【解决方案3】:

    我修改了之前的示例,现在它绘制了圆的所有 4 个部分。

    #include <math.h>
    #include <string>
    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    const int HEIGHT = 50, WIDTH = 50;
    
    void drawCircle(unsigned char pgmImage[][WIDTH], int height, int centerX, int centerY, int radius, unsigned char grayLevel)
    {
    
        for (int rowIndex = centerY; rowIndex < HEIGHT; rowIndex++)
        {
            for (int colIndex = centerX; colIndex < WIDTH; colIndex++)
            {
                if ((pow(1.0*(colIndex - centerX), 2)) + (pow(1.0*(rowIndex - centerY), 2)) <= pow(1.0*radius, 2))
                {
                    pgmImage[rowIndex][colIndex] = (grayLevel);
                }
            }
        }
        for (int rowIndex = 0; rowIndex < centerY; rowIndex++)
        {
            for (int colIndex = 0; colIndex < centerX; colIndex++)
            {
                    if ((pow(1.0*(colIndex - centerX), 2)) + (pow(1.0*(rowIndex - centerY), 2)) <= pow(1.0*radius, 2))
                    {
                        pgmImage[rowIndex][colIndex] = (grayLevel);
                    }
            }
        }
        for (int rowIndex = 0; rowIndex < centerY; rowIndex++)
        {
            for (int colIndex = centerX; colIndex < WIDTH; colIndex++)
            {
                if ((pow(1.0*(colIndex - centerX), 2)) + (pow(1.0*(rowIndex - centerY), 2)) <= pow(1.0*radius, 2))
                {
                    pgmImage[rowIndex][colIndex] = (grayLevel);
                }
            }
        }
        for (int rowIndex = centerY; rowIndex < HEIGHT; rowIndex++)
        {
            for (int colIndex = 0; colIndex < centerX; colIndex++)
            {
                if ((pow(1.0*(colIndex - centerX), 2)) + (pow(1.0*(rowIndex - centerY), 2)) <= pow(1.0*radius, 2))
                {
                    pgmImage[rowIndex][colIndex] = (grayLevel);
                }
            }
        }
    }
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        unsigned char pgmImage[HEIGHT][WIDTH] = { {0} };
        int centerY = HEIGHT/2;
        int centerX = WIDTH/2;
        int radius = std::min(centerX,centerY) - 1;
    
        drawCircle(pgmImage, HEIGHT, centerX, centerY, radius, 1);
        for (int r=0;r<HEIGHT;r++)
        {
            for (int c=0;c<WIDTH;c++)
            {
                char o = (pgmImage[r][c] != 0) ? 'X' : '-';
                cout << o;
            }
            cout << endl;
        }
        getch();
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-23
      • 2020-08-02
      • 2013-07-16
      • 1970-01-01
      • 2020-07-25
      • 2017-08-13
      • 2016-12-27
      相关资源
      最近更新 更多