I want to draw a circle (with 1 or 2 for loops) using pixels position (starts from top left and ends at bottom right)
I successfully drew a rectangle with this method:
private void drawrect(int width,int height,int x,int y) {
int top=y;
int left=x;
if(top<0){
height+=top;
top=0;
}
if(left<0){
width+=left;
left=0;
}
for (int j = 0; j <width; j++) {
for (int i = 0; i <height; i++) {
pixels[((i+top)*w)+j+left] = 0xffffff;//white color
}
}
}
The pixels array contains the pixel index followed by it's color.
pixels[index]=color;
Before that I use this code for "image" and "pixels" array (if this helps you)
img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
pixels = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
But how can I draw only the white pixels like in this image and ignore the other pixels?
Syntax : circle(x, y, radius); where, (x, y) is center of the circle. 'radius' is the Radius of the circle.
Draw a Circle Using the drawOval() Function in Java Now we call the drawOval() function and pass four arguments. The first two arguments are the x and y coordinates of the circle, while the last two arguments specify the width and the height of the circle to be drawn.
You can calculate the minimum angle between two pixels and improve the Kathir solution
...
void DrawCircle(int x, int y, int r, int color)
{
static const double PI = 3.1415926535;
double x1, y1;
// calculates the minimun angle between two pixels in a diagonal.
// you can multiply minAngle by a security factor like 0.9 just to be sure you wont have empty pixels in the circle
double minAngle = acos(1 - 1/r);
for(double angle = 0; angle <= 360; angle += minAngle)
{
x1 = r * cos(angle);
y1 = r * sin(angle);
putpixel(x + x1, y + y1, color);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With