I've been trying to find the bounding rectangle(s) of a given color, within a pixmap using libgdx. I've found this answer https://stackoverflow.com/a/62911971/14895996 from a thread How to find the bounding rectangle(s) of a given color, withing a bitmap? and tried converting the code into java.
Here's what I have so far:
public class Magik {
Array<Rectangle> bounds = new Array<>();
Array<Vector2> points = new Array<>();
Rectangle tempRect = new Rectangle();
Color compare = new Color();
Color color = new Color();
Pixmap pixmap = null;
public Rectangle getBoundingRectangle() {
int curX = (int) points.first().x;
int curY = (int) points.first().y + 1;
int maxX = (int) arrayMax( points ).x;
for( int y = curY; y < pixmap.getHeight(); y++ )
for( int x = curX; x <= maxX; x++ ) {
if ( color.equals(compare) )
points.add( new Vector2( x, y ) );
else
break;
}
Vector2 p1 = points.first();
Vector2 p2 = points.peek();
return new Rectangle( p1.x, p1.y, p2.x - p1.x, p2.y - p1.y );
}
public Array<Rectangle> getBounds( Pixmap pixmap, Color color ) {
this.color = color;
this.pixmap = pixmap;
for( int y = 0; y < pixmap.getHeight(); y++ ) {
for( int x = 0; x < pixmap.getWidth(); x++ ) {
compare = new Color( pixmap.getPixel( x, y) );
if ( color.equals( compare ) ) {
Vector2 p = new Vector2( x, y );
boolean found = false;
for ( Rectangle rect : bounds ) {
if ( tempRect.set( rect.x - 2, rect.y - 2, rect.width + 4, rect.height + 4 ).contains( p ) ) {
found = true;
break;
}
}
if ( !found ) {
points.add( p );
}
}
}
if( points.size > 0 ) {
tempRect = getBoundingRectangle();
bounds.add( new Rectangle( (int) tempRect.x, (int) tempRect.y, (int) tempRect.width, (int) tempRect.height ) );
points.clear();
}
}
System.out.println( "new" );
for (Rectangle corridorBounds : bounds) {
System.out.println( corridorBounds );
}
return bounds;
}
}
Forgot to set Pixel Color on getBoundingRectangle() method. This should do it, and it's working fine now:
public class Magik {
Array<Rectangle> bounds = new Array<>();
Array<Vector2> points = new Array<>();
Rectangle tempRect = new Rectangle();
Color compare = new Color();
Color color = new Color();
Pixmap pixmap = null;
public Rectangle getBoundingRectangle() {
int curX = (int) points.first().x;
int curY = (int) points.first().y + 1;
int maxX = (int) arrayMax( points ).x;
for( int y = curY; y < pixmap.getHeight(); y++ )
for( int x = curX; x <= maxX; x++ ) {
color = new Color( pixmap.getPixel( x, y ) );
if ( color.equals(compare) )
points.add( new Vector2( x, y ) );
else
break;
}
Vector2 p1 = points.first();
Vector2 p2 = points.peek();
return new Rectangle( p1.x, p1.y, p2.x - p1.x, p2.y - p1.y );
}
public Array<Rectangle> getBounds( Pixmap pixmap, Color color ) {
this.color = color;
this.pixmap = pixmap;
for( int y = 0; y < pixmap.getHeight(); y++ ) {
for( int x = 0; x < pixmap.getWidth(); x++ ) {
compare = new Color( pixmap.getPixel( x, y) );
if ( color.equals( compare ) ) {
Vector2 p = new Vector2( x, y );
boolean found = false;
for ( Rectangle rect : bounds ) {
if ( tempRect.set( rect.x - 2, rect.y - 2, rect.width + 4, rect.height + 4 ).contains( p ) ) {
found = true;
break;
}
}
if ( !found ) {
points.add( p );
}
}
}
if( points.size > 0 ) {
tempRect = getBoundingRectangle();
bounds.add( new Rectangle( (int) tempRect.x, (int) tempRect.y, (int) tempRect.width, (int) tempRect.height ) );
points.clear();
}
}
System.out.println( "new" );
for (Rectangle corridorBounds : bounds) {
System.out.println( corridorBounds );
}
return bounds;
}
}