代码之家  ›  专栏  ›  技术社区  ›  tinyJman

尝试在二维阵列中创建圆

  •  0
  • tinyJman  · 技术社区  · 7 年前

    我正在写一个程序来创建 .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);
            }
        }
    }
    

    灰度是我想要的圆圈的灰度。我试图用公式画出这个圆 (x-a)^2 + (y-b)^2 =r^2 其中a和b是我的中心x和y。

    3 回复  |  直到 7 年前
        1
  •  1
  •   Joseph D.    7 年前

    当你的中心 大于 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
  •  1
  •   Stephan Lechner    7 年前

    我认为只要正确计算参数,您的代码应该可以正常工作:

    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------------------------
    
        3
  •  0
  •   VideoProcessingResearcher    7 年前

    我修改了前面的示例,现在它绘制了圆的所有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;
    }