Skip to content

Instantly share code, notes, and snippets.

@simonw
Created December 11, 2024 16:19
Show Gist options
  • Save simonw/0d8225d62e8d87ce843fde471d1432cc to your computer and use it in GitHub Desktop.
Save simonw/0d8225d62e8d87ce843fde471d1432cc to your computer and use it in GitHub Desktop.

2024-12-11T16:19:16 conversation: 01jev893w4msq9sfax1k4mav6s

Model: gemini-2.0-flash-exp

Prompt:

write and execute python to generate a 80x40 ascii art fractal

Response:

Okay, I can help you with that. I'll generate an 80x40 ASCII art fractal using Python. I'll use a simple Mandelbrot set calculation, as it's a common and visually appealing fractal.

Here's the plan:

  1. Define the Mandelbrot Calculation: I'll create a function that checks if a point in the complex plane belongs to the Mandelbrot set.
  2. Set up the coordinate grid: I'll map the 80x40 grid to a complex plane region.
  3. Iterate and generate output: I'll loop through the grid coordinates, calculate the Mandelbrot set membership, and use different characters based on how many iterations it took to escape (or if it didn't escape).
  4. Print the output: I'll output the generated ASCII art.

Here's the Python code:

def mandelbrot(c, max_iter):
    z = 0
    for i in range(max_iter):
        z = z*z + c
        if abs(z) > 2:
            return i
    return max_iter


width = 80
height = 40
max_iterations = 50
output = ""
for row in range(height):
    line = ""
    for col in range(width):
        x = -2.0 + (col / width) * 3.0  # scale to -2 to 1 range
        y = -1.0 + (row / height) * 2.0  # scale to -1 to 1 range
        c = complex(x, y)
        iterations = mandelbrot(c, max_iterations)
        if iterations == max_iterations:
            line += " "
        else:
            chars = ".,-~:;=!*#$@"
            line += chars[iterations % len(chars)]
    output += line + "\n"
print(output)
........,,,,,,----------------------~~~~~~~~:::;=#.*$$;~~~~~~------,,,,,,,,,,,,,
.......,,,,,----------------------~~~~~~~~~:::;=!*@,#=;::~~~~~~------,,,,,,,,,,,
......,,,,,---------------------~~~~~~~~~::::;=!*:::$!=;::~~~~~~------,,,,,,,,,,
......,,,,--------------------~~~~~~~~~~::::;=.#-$ ,;@~;::::~~~~~------,,,,,,,,,
.....,,,---------------------~~~~~~~~~:::;;;=!#!      $=;:::::~~~~-------,,,,,,,
....,,,--------------------~~~~~~~~~::;;;;===!,      .#!=;;;::::~~--------,,,,,,
....,,--------------------~~~~~~~~::;=!*!!!!*#$~     -$**!=;;;=*:~~--------,,,,,
...,,,------------------~~~~~~~::::;=#==@## ! #       ! .$#!!!*!=:~~-------,,,,,
...,,-----------------~~~~~:::::::;;=$,   ;               -$,@~;-;:~--------,,,,
...,-----------------~~~::::::::;;;=!*=  *                    *@*;:~~--------,,,
..,---------------~~~::::::::::;;;=*,$:#                      @$=;::~~--------,,
..,------------~~~:;;;;;::::;;;;==*$ ;                        -#=;::~~--------,,
..---------~~~~~::;!@============!*@!                          @*#=:~~---------,
.,------~~~~~~:::;;$-#*!!#,*!!!!!*;                              #!:~~~--------,
.,---~~~~~~~~::::;=!#.--@@: ..#**#:                             -!;:~~~--------,
.---~~~~~~~::::::;=!*,# @     @@$@                              ,.;:~~~---------
.-~~~~~~~~::::::;==@$,:         --*                             =.;:~~~~--------
.~~~~~~~~::::;;!:**$=            !                              ~=::~~~~--------
.~~~~~~~;;;;;==*@,.,#            !                              !;::~~~~--------
.::;;;*===;!!!##,,  !                                          *=;::~~~~--------
                                                             ,#!=;::~~~~--------
.::;;;*===;!!!##,,  !                                          *=;::~~~~--------
.~~~~~~~;;;;;==*@,.,#            !                              !;::~~~~--------
.~~~~~~~~::::;;!:**$=            !                              ~=::~~~~--------
.-~~~~~~~~::::::;==@$,:         --*                             =.;:~~~~--------
.---~~~~~~~::::::;=!*,# @     @@$@                              ,.;:~~~---------
.,---~~~~~~~~::::;=!#.--@@: ..#**#:                             -!;:~~~--------,
.,------~~~~~~:::;;$-#*!!#,*!!!!!*;                              #!:~~~--------,
..---------~~~~~::;!@============!*@!                          @*#=:~~---------,
..,------------~~~:;;;;;::::;;;;==*$ ;                        -#=;::~~--------,,
..,---------------~~~::::::::::;;;=*,$:#                      @$=;::~~--------,,
...,-----------------~~~::::::::;;;=!*=  *                    *@*;:~~--------,,,
...,,-----------------~~~~~:::::::;;=$,   ;               -$,@~;-;:~--------,,,,
...,,,------------------~~~~~~~::::;=#==@## ! #       ! .$#!!!*!=:~~-------,,,,,
....,,--------------------~~~~~~~~::;=!*!!!!*#$~     -$**!=;;;=*:~~--------,,,,,
....,,,--------------------~~~~~~~~~::;;;;===!,      .#!=;;;::::~~--------,,,,,,
.....,,,---------------------~~~~~~~~~:::;;;=!#!      $=;:::::~~~~-------,,,,,,,
......,,,,--------------------~~~~~~~~~~::::;=.#-$ ,;@~;::::~~~~~------,,,,,,,,,
......,,,,,---------------------~~~~~~~~~::::;=!*:::$!=;::~~~~~~------,,,,,,,,,,
.......,,,,,----------------------~~~~~~~~~:::;=!*@,#=;::~~~~~~------,,,,,,,,,,,

Okay, the code has been executed and the fractal image printed above. The output is a bit blocky because of the limitations of ASCII art, but the general shape of the Mandelbrot set is visible. You can adjust the max_iterations value to get a more detailed image, but it might slow down the process if you make it too high. You can also modify the character set to influence the look of the image.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment