Reputation: 23
I'm trying to write a program for creating .pgm and .ppm and am trying to draw a circle using a 2D array of numbers. Using a given center location (x,y) and the radius. Here is what my code looks like for the drawCircle()
function.
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);
}
}
}
The grayLevel is for what shade of grey I want the circle to be. and I am attempting to draw the circle using the formula (x-a)^2 + (y-b)^2 =r^2
where a and b are my center x and y.
Upvotes: 0
Views: 3111
Reputation: 556
I modified previous example and now it draws all 4 parts of circle.
#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;
}
Upvotes: 0
Reputation: 12174
I see the problem when your center is greater than 50.
Your loop initialization starts at center.
However, you hard-coded the condition to be always less than 50
which will be false.
Perhaps you want to start from (0, 0)
, until ((height - 1), (width - 1))
eg.
rowIndex (0, height]
colIndex: (0, width]
CODE SNIPPET
for (int rowIndex = 0; rowIndex < height; rowIndex++) {
for (int colIndex = 0; colIndex < width; colIndex++) {
Upvotes: 1
Reputation: 35154
I think you code should work fine, provided that you calculate the parameters correctly:
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;
}
}
Output:
--------------------------------------------------
--------------------------------------------------
--------------------------------------------------
--------------------------------------------------
--------------------------------------------------
--------------------------------------------------
--------------------------------------------------
--------------------------------------------------
--------------------------------------------------
--------------------------------------------------
--------------------------------------------------
--------------------------------------------------
--------------------------------------------------
--------------------------------------------------
--------------------------------------------------
--------------------------------------------------
--------------------------------------------------
--------------------------------------------------
--------------------------------------------------
--------------------------------------------------
--------------------------------------------------
--------------------------------------------------
--------------------------------------------------
--------------------------------------------------
--------------------------------------------------
-------------------------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------------------------
Upvotes: 1