Detecting islands on a 2d map?

Correct me if I’m wrong, but I added the following code to my Coord class.

public boolean equals(Coord c){
		if(c.equals(this)) return true;
		return false;
	}

And I just checked what coords.contains© returns and it came up false, every time; even after implementing equals().


public boolean equals(Coord c) {
    return c.x == x && c.y == y;
}

Whole point is that two Coord objects are equal if they have the same coords, no?

This could also be “fixed” (still implement equals though, for sanity) by not creating a new object each time to add to the coords list:

-coords.add(new Coord(c.x, c.y));
+coords.add(c);

Yeah thats the idea, I was wondering if I should compare x, y coords instead of the objects themselves. Even with that change the same still happens. I just printed out the x, y coords that are getting added and there are repeats.

For example:
ADDING: 407, 10
ADDING: 405, 10
ADDING: 407, 10
ADDING: 406, 9
ADDING: 406, 11
ADDING: 404, 11
ADDING: 406, 11
ADDING: 405, 10
ADDING: 405, 12
ADDING: 406, 11
ADDING: 408, 11
ADDING: 407, 10
ADDING: 407, 12
ADDING: 405, 10
ADDING: 407, 10

Repeats in the queue, or in the result list?
There will be some repeats in the queue, but they will get taken care of by the !coords.contains()
There shoulbn’t be any repeats in the result list.
Do you still have this edit?

Yep, still have that change. Repeats in the result list, every time a coord gets added to the result list I print line saying Added: x, y.
Looking through those results I can see coords repeatedly popping up, like 111, 10 or 113, 12.

Progress! I have found a possible solution to the problem. Instead of using !coords.contains© to check if the tile is already owned, I add the island id (which is predefined before the filling) to a 2d int array called islandId. Here’s what the final result looks like, islands were detected in 54ms.

World map.

Islands each with a random colour.

good solution.

now you could also implement a method to remove/sink islands which are too small (minimum number of tiles reached in a floodfill)
if that fits your gameplay

I just implemented that, I also remove any islands touching the sides. Here’s a few examples of the worlds it makes.




Get some pretty interesting land formations, I’ll probably tweak the perlin noise and water height as I go on.

I’ll post the code when its more readable and complete.