试图在二维数组中创建一个圆

Trying to create a circle within a 2D array

我正在尝试编写一个程序来创建 .pgm.ppm 并尝试使用 2D 绘制一个圆数字数组。使用给定的中心位置 (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。

当您的中心 大于 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++) {

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

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------------------------

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