Jonas Bartkowski
Jonas Bartkowski

Reputation: 387

How do I render a circle with radius x?

So I am having quite a difficult problem.

I kind of want a method like this:

public Color[][] createCircle(Color c, int d)
{
    Color[][] Data = new Color[d][d];
    //fillColorArrayForCircle(Data); 
    return Data;
}

In Theory this method renders a circle (not filled out) of the Diameter d into a 2D color Array and then returns it. (Everything that is part of the Circle is Color C, the rest is null. The center of the circle is at [d-(d-1)/2]²) However I have no idea, how to determine in what places the "pixels" need to be.

I also looked up http://en.wikipedia.org/wiki/Midpoint_circle_algorithm however I didn't understand it. (extremely complicated imo)

It would be very nice if one of you could show me the correct implementation and/or explain to me how I can come up with one. greetings!

Upvotes: 0

Views: 318

Answers (1)

lbalazscs
lbalazscs

Reputation: 17794

If this is some sort of homework, then this answer will not be satisfactory, but if it is a real problem, then a workaround would be the following:

  1. Create a BufferedImage
  2. Render a circle on it using the Java2D API. Let it do the hard work. You can have this way antialiasing as well.
  3. Read the pixel values of the image, and build your array accordingly.

Upvotes: 1

Related Questions