Hi,
how can I store all pixel from a circle/ellipse in an array?
Thank you for your help
Printable View
Hi,
how can I store all pixel from a circle/ellipse in an array?
Thank you for your help
#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 < 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.
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
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.
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.
I want to calculate the average RGB-value of the circle and pie of the circle.
Have you got another idea?
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.
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) <= 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 <= right; x++)
{
for (int y = top; x <= 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 >=0 && r < 256);
ASSERT(g >=0 && g < 256);
ASSERT(b >=0 && b < 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.
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?
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
hello guys, :D
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
Do you mean this one?
thank you for this.