Java Gulf Issue

I need to write a fill fill algorithm for color pixels of an image that are inside black borders. I wrote the following, based on some posts here on SO:

private Queue<Point> queue = new LinkedList<Point>(); private int pickedColorInt = 0; private void floodFill(Pixmap pixmap, int x, int y){ //set to true for fields that have been checked boolean[][] painted = new boolean[pixmap.getWidth()][pixmap.getHeight()]; //skip black pixels when coloring int blackColor = Color.rgba8888(Color.BLACK); queue.clear(); queue.add(new Point(x, y)); while(!queue.isEmpty()){ Point temp = queue.remove(); int temp_x = temp.getX(); int temp_y = temp.getY(); //only do stuff if point is within pixmap bounds if(temp_x >= 0 && temp_x < pixmap.getWidth() && temp_y >= 0 && temp_y < pixmap.getHeight()) { //color of current point int pixel = pixmap.getPixel(temp_x, temp_y); if (!painted[temp_x][temp_y] && pixel != blackColor) { painted[temp_x][temp_y] = true; pixmap.drawPixel(temp_x, temp_y, pickedColorInt); queue.add(new Point(temp_x + 1, temp_y)); queue.add(new Point(temp_x - 1, temp_y)); queue.add(new Point(temp_x, temp_y + 1)); queue.add(new Point(temp_x, temp_y - 1)); } } } } 

This does not work properly. For example, in the following test image: enter image description here

Random rectangles will be colored depending on where I clicked. For example, clicking anywhere below the purple rectangle will redraw the purple rectangle. A click inside the purple rectangle redraws the green rectangle. I checked it and I pass the correct parameters to the method, so the problem is probably somewhere inside my loop.

+6
source share
1 answer

Your algorithm is correct, only your input parameters are missing.

Random rectangles will be colored depending on where I clicked. For example, clicking anywhere below the purple rectangle will redraw the purple rectangle. A click inside the purple rectangle redraws the green rectangle.

If you look at the image, the colored rectangles are not really random. The real problem is the incorrect Y coordinate. In particular, your Y coordinate is inverted.

This is because most of the time LibGDX uses the lower left, y-up coordinate system, but in the case of Pixmap it is located on the upper left y-down.

A simple fix for this is to simply invert the value of Y by doing y = pixmap.getHeight() - y .

+2
source

Source: https://habr.com/ru/post/986115/


All Articles