Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

255
Vistas
Island detection: javscript

The problem

I am making a javascript island generator using DFS algorithm, but I ran into a problem. I want to detect the position and area of each island, and not all the points that lie within the island. It creates a new island for every single coordinate above sea level. Here is my code:

// recursive function to check the island's size
    checkIsland(island, x, y){
        if (this.terrain.getHeightFromMap(x, y) >= 3) {
            island.size++;
            if (x > 0) {
                this.checkIsland(island, x - 1, y);
                this.matrix[`${x - 1}:${y}`] = 1;
            }
            if (x < this.terrain.length - 1) {
                this.checkIsland(island, x + 1, y);
                this.matrix[`${x + 1}:${y}`] = 1;
            }
            if (y > 0) {
                this.checkIsland(island, x, y - 1);
                this.matrix[`${x}:${y - 1}`] = 1;
            }
            if (y < this.terrain.length - 1) {
                this.checkIsland(island, x, y + 1);
                this.matrix[`${x}:${y + 1}`] = 1;
            }
            // update the matrix
            this.matrix[`${x}:${y}`] = 1;
        }
    }

    update(){
        this.campos = [Math.round(this.camera.position.x), Math.round(this.camera.position.y), Math.round(this.camera.position.z)];
        for (var x = -10+this.campos[0]; x < 10+this.campos[0]; x++) {
            for (var y = -10+this.campos[2]; y < 10+this.campos[2]; y++) {
                    if(this.matrix[`${x}:${y}`] == undefined){
                        let island = Object.assign({}, island_example);
                        island.position = [x, y];
                        this.checkIsland(island, x, y);
                        if(island.size > 1){
                            this.islands.push(island);
                        }
                    }
            }
        }
        for(var i in this.islands){
            let _ = BABYLON.MeshBuilder.CreatePlane("quad", {width: 10, height: 10}, this.scene);
            _.position.y = this.terrain.getHeightFromMap(this.islands[i].position[0], this.islands[i].position[1]) + 1;
            _.position.x = this.islands[i].position[0];
            _.position.z = this.islands[i].position[1];

            // remove the island from the list
            if(this.islands[i].size < 10){
                this.islands.splice(i, 1);
            }
        }
    }

Output: (Warning: Lags the browser!) output

Expected output: (One island's details) A single 2d coordinate denoting the position of the centre of the island, and the island's area.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda