CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: Store all pixel from a circle in an array?

1. Member
Join Date
May 2000
Posts
38

## Store all pixel from a circle in an array?

Hi,
how can I store all pixel from a circle/ellipse in an array?

Thank you for your help

2. Senior Member
Join Date
Jun 2001
Location
Lewes, UK
Posts
1,313

## Re: Store all pixel from a circle in an array?

#include "math.h" // need sin/cos functions
#define _PI (2*asin(1)) // define PI

#define STEP_SIZE 360 // number of points to store

#define XIndex 0
#define YIndex 1

double array[2][STEP_SIZE];
// or
// CPoint ptArray[STEP_SIZE];

double radius = 50;
CPoint ptC(200,200); // Centre point
double step = (2* _PI) / STEP_SIZE;

for (int index = 0; index &lt; STEP_SIZE; index ++)
{
double theta = index * (2* _PI) / STEP_SIZE;

array[XIndex][index] = (radius * cos(theta)) + ptC.x;
array[YIndex][index] = (radius * sin(theta)) + ptC.y; // for ellipse use a different value for the radius

// if we have a DC to test... Plot the pixel on the graphics screen
// add 0.5 to assist rounding to nearest integer
dc.SetPixel((int)(array[XIndex][index]+ 0.5), (int)(array[YIndex][index]+0.5),RGB(255,0,0));
}

Hope this helps

Eugene Gill

Please take the time to reply and/or rate this answer if you found it useful.

Other readers will then be better informed.

3. Member
Join Date
May 2000
Posts
38

## Re: Store all pixel from a circle in an array?

First of all thank you for your help!

I think, I've expressed me wrong.
I want to store all pixel from the area of the circle in an array, not only the points from the circumference.

If you could help me further, it would be very nice.
Thanks for your time

4. Senior Member
Join Date
Jun 2001
Location
Lewes, UK
Posts
1,313

## Re: Store all pixel from a circle in an array?

Why?

It's not very efficient.

Hope this helps

Eugene Gill

Please take the time to reply and/or rate this answer if you found it useful.

Other readers will then be better informed.

5. Senior Member
Join Date
Aug 2001
Location
North Bend, WA
Posts
1,947

## Re: Store all pixel from a circle in an array?

If your question relates to finding the pixels inside of a circle, it is a pretty ugly process.

Calculate 2 rectanges. An outer rectange
left = center.x - radius;
top = center.y - radius;
width = radius * 2;
height = radius * 2;

Then calculate the bigest rectangle that fits inside the circle.

left = center.x = cos(PI/2) * radius;

etc.

simply scan all pixels inside the inner rectangle.

For each pixel in the outer rectangle that is not in the inner rectange, calculate its distance from the center sqrt(x*x + y*y) If its less than the radius its inside the circle.

Hope this helps.

6. Member
Join Date
May 2000
Posts
38

## Re: Store all pixel from a circle in an array?

I want to calculate the average RGB-value of the circle and pie of the circle.
Have you got another idea?

7. Member
Join Date
May 2000
Posts
38

## Re: Store all pixel from a circle in an array?

Thank you for help,

do you even knpw how to destinate the pixel (inclusively the content) of a pie of the circle.
The 360 pixel of the circumference I calculated before.

8. Senior Member
Join Date
Jun 2001
Location
Lewes, UK
Posts
1,313

## Re: Store all pixel from a circle in an array?

Better off storing the RGB values than the pixels then.

What about this? (DISCLAIMER, untested!)

class Circle
{
public:
int m_xCenter;
int m_yCenter;
double m_Radius;
BOOL PtInCircle(int x, int y);
};

BOOL Circle::PtInCircle(int x, int y)
{
// Determine x, y with centre at 0,0
double absX = x - m_xCenter / 2;
double absY = y - m_yCenter / 2;

// Apply formula
return ((absX * absX) / (m_Radius * m_Radius) + (absY * absY) / (m_Radius * m_Radius) &lt;= 1);
}

Circle c1;
c1.m_xCenter = 100;
c1.m_yCenter = 100;
c1.m_Radius = 20;

int left = int (c1.m_xCenter - m_Radius - 0.5) // rounded down
int right = int (c1.m_xCenter + m_Radius + 0.5) // rounded up

int top = int (c1.m_yCenter - m_Radius - 0.5) // rounded down
int bottom = int (c1.m_yCenter + m_Radius + 0.5) // rounded up

double r = 0;
double g = 0;
double b = 0;
int pointCount = 0;

for (int x = left; x &lt;= right; x++)
{
for (int y = top; x &lt;= bottom; x++)
{
// is pixel in circle?
if c1.PtInCircle(x,y)
{
// get RGB values
pointCount++;
COLORREF rgbV = dc.GetPixel(x,y);
r+= GetRValue(rgbV);
g+= GetGValue(rgbV);
b+= GetBValue(rgbV);
}
}
}

r = int(0.5+ (r/pointCount)); // round to nearest int
g = int(0.5+ (g/pointCount)); // round to nearest int
b = int(0.5+ (b/pointCount)); // round to nearest int

ASSERT(r &gt;=0 && r &lt; 256);
ASSERT(g &gt;=0 && g &lt; 256);
ASSERT(b &gt;=0 && b &lt; 256);

// average
COLORREF averageRGB = RGB(int(r),int(g),int(b));

Hope this helps

Eugene Gill

Please take the time to reply and/or rate this answer if you found it useful.

Other readers will then be better informed.

9. Junior Member
Join Date
Feb 2022
Posts
11

## Re: Store all pixel from a circle in an array?

I found a simple problem of calculating the area of a circle in pixel values. easycpstest.com/pixel-circle-generator for example, for radius=5 pixels,the area is 97 pixels (not 78.54 pixels). Is there any place where I can find the conversion or formula that can be used to calculate the area in pixels?
Last edited by existenceproduct; February 27th, 2022 at 03:05 PM.

10. ## Re: Store all pixel from a circle in an array?

Originally Posted by existenceproduct
I found a simple problem of calculating the area of a circle in pixel values. for example, for radius=5 pixels,the area is 97 pixels (not 78.54 pixels). Is there any place where I can find the conversion or formula that can be used to calculate the area in pixels?
Don't you want to show your code producing such a problem?

11. Member +
Join Date
Feb 2017
Posts
647

## Re: Store all pixel from a circle in an array?

Originally Posted by existenceproduct
Is there any place where I can find the conversion or formula that can be used to calculate the area in pixels?
Are you looking for a solution to the Gauss circle problem?

https://en.wikipedia.org/wiki/Gauss_circle_problem

https://mathworld.wolfram.com/GausssCircleProblem.html
Last edited by wolle; February 27th, 2022 at 03:40 PM.

12. Junior Member
Join Date
Feb 2022
Posts
11

## Re: Store all pixel from a circle in an array?

hello guys,
I'm trying to create lines, pixels, circles, and squares that appear on-screen by sgdk, I know there's a function, but nn I'm getting it, could anyone help me :?: moving from Florida to Alabama
Last edited by existenceproduct; May 1st, 2022 at 07:40 AM.

13. ## Re: Store all pixel from a circle in an array?

Originally Posted by existenceproduct
hello guys,
I'm trying to create lines, pixels, circles, and squares that appear on-screen by sgdk, I know there's a function, but nn I'm getting it, could anyone help me :?:
Do you mean this one?

14. Junior Member
Join Date
Mar 2022
Posts
9

## Re: Store all pixel from a circle in an array?

thank you for this.

15. Junior Member
Join Date
Feb 2022
Posts
11

## Re: Store all pixel from a circle in an array?

Originally Posted by wolle
thank you for the solution
Last edited by existenceproduct; May 16th, 2022 at 02:55 PM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Click Here to Expand Forum to Full Width

Featured