Home > Enterprise >  Create blob shape in list/array
Create blob shape in list/array

Time:08-27

Is there any fast way of creating filled organic looking blob shapes in a list/array? (around 30x30 would be enough, nothing bigger) I have referred to this post C create random shaped "blob" objects but the problem is that this creates an outline and is not fast as it has to loop through every integer angle.

Any ideas would be appreciated. I'm coding in python but just the plain algorithm would work :) Thanks in advance!

CodePudding user response:

Eventually I settled on using metaballs. Here's the code:

@dataclass
class Metaball:
    center: tuple[int, int]
    radius: float

def blob():
    balls = [Metaball((randint(4, 11), randint(4, 11)), randint(2, 4)) for _ in range(randint(3, 4))]

    result = []

    for y in range(16):
        result.append([])
        for x in range(16):
            if sum([(ball.radius / distance / 2) if (distance := dist(ball.center, (x, y))) else 10 for ball in balls]) > 1.2:
                result[y].append("#")
            else:
                result[y].append(".")

    return result

for row in blob():
     for ch in row:
         print(ch, end=" ")
     print()
  • Related