Java – Why does Pixmap have a variance of 1?

Why does Pixmap have a variance of 1?… here is a solution to the problem.

Why does Pixmap have a variance of 1?

I’m trying to draw a circle using Pixmap. To make the issue clearer, I filled the entire Pixmap area with white and then drew the circles with different colors. This is the code that I think should work.

I set the width/height of the Pixmap to twice the size of the circle radius.

Then I drew a circle in the middle of the Pixmap (radius, radius).

public static Texture circle(int radius, Color color) {
  Pixmap pixmap = new Pixmap(radius * 2, radius * 2, Pixmap.Format.RGBA4444);
  pixmap.setColor(Color.WHITE);
  pixmap.fill();
  pixmap.setColor(color);
  pixmap.fillCircle(radius, radius, radius);
  Texture texture = new Texture(pixmap);
  pixmap.dispose();
}

Unfortunately, the pixelmap cuts off the circles on the right and bottom. For example:

enter image description here

If I increase the width and height of the Pixmap by 1, it looks fine :

enter image description here

I’m free to add an extra pixel, but I’d like to understand why this is necessary. Why does setting the radius of a circle to X cause the diameter to actually be X + 1?

Solution

To get the result you want, the position of the center of the circle must fall between two pixels so that the number of full pixels on either side of the position is similar. My guess is that the Pixmap code defines the position of the pixel to represent the center of the pixel. So the dot (radius, radius) is closer to the right than the left, and (radius-1, radius-1) is closer to the left than the left. With this positional definition, the center of the circle should be at the (radius-.5, radius-.5) position.

If you have to place the center of the circle in the middle of a pixel, then it makes sense for you to use position (radius, radius) as the circle, and you’d need the width and height of the Pixmap to be (2*radius + 1, 2*radius+1). This way, the number of pixels on both sides of the center of the circle is the same, radius + .5 pixels. At that time you may want to draw a circle with radius + .5, if the library accepts it.

Related Problems and Solutions