# Java create a Polygon for each island in tile game

I am making a 2d top-down tile based game, which is randomly generated each new world.

What I want to do is create a Polygon (or other more appropriate shape I don't know of) for each landmass not disrupted by land, basically each unseparated landmass/island, I want to do this for where my player is to spawn in the world, I want to have it so it has a higher chance of spawning on the biggest landmass in the world, rather than spawning on a tiny several tile island. So, all I need to know is how would I create some sort of shape system to recognise different islands.

The reason I suggest a Polygon is because the player would randomly spawn in that area so it has to be accurate to the land, it can't start of by spawning in water nearby.

If a Polygon is obsolete for this idea, and another system would be better let me know.

• This sounds like a simple connected component search problem, which we've answered here before. Where have you run into difficulty implementing it? – DMGregory Sep 17 '19 at 19:13
• @DMGregory I haven't tried anything yet, but I don't actually know where to start, my quesion was asking for ideas to my problem – Tyler Harrison Sep 18 '19 at 15:42

For the main island I would do something like this. I would choose how many sides the polygon has to have. Then, I would choose a value, how far the farthest point of the island can be. Then I would create an array of coordinates of my polygon like this:

public class Island{

public static void main(String[] args){

int numberOfPoints = 10;
float [][] islandCoordinates = float[numberOfPoints];//array of coordinates
float maxDistance = 1000;

float stepAngle = 2 * Math.PI / numberOfPoints;

for(int i = 0;  i < numberOfPoints; i++){
float dist = maxDistance * Math.random();
float xcoordinate = Math.cos(stepAngle * i) * dist;
float ycoordinate = Math.sin(stepAngle * i) * dist;

islandCoordinates[i] = xcoordinate;
islandCoordinates[i] = ycoordinate;
}

}

}


This would make the island look very simple, but I think with playing with the values you could make it a lot nicer.

Good luck!