1

I'm trying to figure out this weird problem I've run in to.

I have a multidimensional array that is essentially a 2D matrix comprised of binary values. 1 represents an area that's filled, 0 is empty. I'm treating this as "pixel art" although the application is vastly different.

What I want to do is expand the array to fit a certain amount of ones (adding rows & columns, duplicating "pixels"), sort of when enlarging an image.

Here's what would be a crude example of what I'm working with:

$shape = array(
    array(0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0),
    array(0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0),
    array(0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0),
    array(0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0),
    array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
    array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
    array(0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0),
    array(0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0),
    array(0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0),
    array(0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0),
    array(0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0),
    array(0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0),
    array(0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0),
    array(1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1),
    array(1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1),
); 

Down the line the shapes will get trickier, but I'm using this as a test piece, however they will always be solid and binary just like this star.

Right now the fill value of this array is 135 (how many ones there are). Let's say I want to change the fill value to exactly 309, 2048 or 15. Is there a neat way to accomplish this in PHP while retaining the shape of the initial matrix?

To clarify, I don't want to enlarge the matrix by x amount of columns & rows, I want to specify exactly how many ones I want in the final matrix, if the match isn't 100% I'm fine with that, as long as I'm left with exactly the number of ones I specified.

I'm fooling around with pixel-art scaling algorithms, but can't quite wrap my head around how to deal with this problem.

Matt
  • 1,139
  • 1
  • 11
  • 28
  • applying [dilatation morphology filter](https://en.wikipedia.org/wiki/Dilation_(morphology)) from DIP (digital image processing) multiple times should do the trick you just modify the recursion to stop when your fill counter (how much cells to fill left) hits zero .... – Spektre Jun 11 '18 at 07:52
  • @Spektre thank you for the suggestion. I'll check it out. – Matt Jun 13 '18 at 03:25
  • Also this [fill(c0,c1,c2)](https://stackoverflow.com/a/29412283/2521214) can be modified in the same way and will probably lead to much better shape than dilatation. its my grow fill but you would need to allow at least 3 colors in your matrix not just 2. – Spektre Jun 13 '18 at 06:31

0 Answers0