nurpax / petmate

PETSCII editor with Electron/React/Redux
MIT License
181 stars 14 forks source link

flood fill tool #80

Open Esshahn opened 6 years ago

Esshahn commented 6 years ago

This is basically the fill method that paint programs use. I've implemented this for Spritemate, too. Would be a fun tool for Petmate.

This might get you started:

  floodfill(pos)
  {
    // https://stackoverflow.com/questions/22053759/multidimensional-array-fill
    // get target value
    let x = pos.x;
    let y = pos.y;
    var data = this.all.sprites[this.all.current_sprite].pixels;

    // multicolor check
    var stepping = 1;
    var is_multi = this.all.sprites[this.all.current_sprite].multicolor;
    if (is_multi) stepping = 2;

    if (is_multi && x%2 !== 0) x=x-1;
    var target = data[y][x];

    function flow(x,y,pen) 
    {

        // bounds check what we were passed
        if (y >= 0 && y < data.length && x >= 0 && x < data[y].length) 
        {
            if (is_multi && x%2 !== 0) x=x-1;
            if (data[y][x] === target && data[y][x] != pen) 
            {
                data[y][x] = pen;
                flow(x-stepping, y, pen);    // check up
                flow(x+stepping, y, pen);    // check down
                flow(x, y-1, pen);    // check left
                flow(x, y+1, pen);    // check right
            }
        }
    }

    flow(x,y,this.all.pen);
    this.all.sprites[this.all.current_sprite].pixels = data;
  }
nurpax commented 6 years ago

This makes a lot of sense for normal pixel data. But what's a sensible way to do this with PETSCII? There's both pixel (screencode) and color information per block. What's the traverse condition? Treat all non-space chars as a boundary?

Esshahn commented 6 years ago

I had it in mind like:

But I must admit I'm not sure if that turns out to be a power feature. Might be kept best in the backlog for a bit...