Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

250
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!