我正在尝试编写用于创建 .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);
}
}
}
灰色等级是我想要圆圈的灰色阴影。我试图使用公式(x-a)^2 + (y-b)^2 =r^2
绘制圆圈,其中a和b是我的中心x和y。
答案 0 :(得分:1)
当您的中心大于 50时,我会发现问题。
您的循环初始化从中心开始。
但是,您将条件硬编码为始终小于50
,这将是错误的。
也许您想从(0, 0)
开始,直到((height - 1), (width - 1))
例如
rowIndex (0, height]
colIndex: (0, width]
CODE SNIPPET
for (int rowIndex = 0; rowIndex < height; rowIndex++) {
for (int colIndex = 0; colIndex < width; colIndex++) {
答案 1 :(得分:1)
我认为您的代码应该可以正常工作,只要您正确计算参数:
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------------------------
答案 2 :(得分:0)
我修改了上一个示例,现在它绘制了圆的所有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;
}