Skip to content

Instantly share code, notes, and snippets.

@simonw

simonw/ascii.md Secret

Created April 18, 2025 00:13

2025-04-18T00:10:37 conversation: 01js33qv6nnj899yghq3zk0pm3 id: 01js33qv6r3ggx3f3qke3qe7s9

Model: gemini-2.5-flash-preview-04-17

Prompt

render a mandelbrot fractal in ascii art

Response

Okay, I can render a basic ASCII art representation of the Mandelbrot set for you.

The process involves:

  1. Defining a region in the complex plane (e.g., from -2 to 1 on the real axis and -1.5 to 1.5 on the imaginary axis).
  2. Dividing this region into a grid corresponding to the characters of the ASCII art.
  3. For each point in the grid, testing if the corresponding complex number is in the Mandelbrot set by iterating the function z = z^2 + c, starting with z = 0.
  4. Counting how many iterations it takes for the magnitude of z to exceed a threshold (usually 2). If it doesn't exceed the threshold within a maximum number of iterations, the point is considered part of the set.
  5. Mapping the iteration count to an ASCII character. Points inside the set get one character (like '#'), and points outside get characters based on how quickly they escape.

I will use Python with some standard parameters for the region and resolution.

# Parameters for the fractal
width = 80  # Width of the ASCII art in characters
height = 45 # Height of the ASCII art in characters
x_min = -2.0
x_max = 1.0
y_min = -1.5
y_max = 1.5
max_iterations = 100 # Maximum iterations to determine if a point is in the set
escape_threshold_sq = 4.0 # Squared magnitude threshold (|z|^2 > 4)

# Characters to use for different escape speeds (density)
# From fastest escape (space) to slowest (almost in the set, like @)
# Use '#' for points inside the set (reached max_iterations)
chars = " .:-=+*#%@" # 10 characters + '#' for inside
inside_char = '#'

# Calculate the step sizes
dx = (x_max - x_min) / width
dy = (y_max - y_min) / height

# Generate the ASCII art
output = []
for row in range(height):
    line = ""
    # Map pixel row to imaginary coordinate
    c_imag = y_max - row * dy
    for col in range(width):
        # Map pixel column to real coordinate
        c_real = x_min + col * dx

        # Initial conditions for the Mandelbrot iteration
        z_real = 0.0
        z_imag = 0.0
        iteration = 0

        # Mandelbrot iteration: z = z^2 + c
        while z_real * z_real + z_imag * z_imag < escape_threshold_sq and iteration < max_iterations:
            z_real_new = z_real * z_real - z_imag * z_imag + c_real
            z_imag_new = 2 * z_real * z_imag + c_imag
            z_real = z_real_new
            z_imag = z_imag_new
            iteration += 1

        # Map the iteration count to a character
        if iteration == max_iterations:
            # Point is likely inside the set
            line += inside_char
        else:
            # Point is outside, use a character based on how fast it escaped
            # Scale iteration count to the range of chars
            char_index = int(iteration / max_iterations * (len(chars) -1)) # -1 because index is 0 to len-1
            line += chars[char_index]
    output.append(line)

# Print the generated ASCII art
for line in output:
    print(line)
..                              
                                                 #                              
                                                  ..                            
                                                 .:.                            
                                              .:.+#+:.                          
                                               :#####.                          
                                               .#####.                          
                                      =.....*:#########%-#     -                
                                      -####################.###.:               
                                    . --######################:                 
                   .               .*#########################..                
                    .     .       .:###########################.:.              
                     .:....=..    .+############################.               
                     .:#=######:..-#############################*.              
                    -=###########.##############################.               
                ..:..############:##############################                
              ...#############################################.                 
              ...#############################################.                 
                ..:..############:##############################                
                    -=###########.##############################.               
                     .:#=######:..-#############################*.              
                     .:....=..    .+############################.               
                    .     .       .:###########################.:.              
                   .               .*#########################..                
                                    . --######################:                 
                                      -####################.###.:               
                                      =.....*:#########%-#     -                
                                               .#####.                          
                                               :#####.                          
                                              .:.+#+:.                          
                                                 .:.                            
                                                  ..                            
                                                 #                              
                                                ..

Here is an ASCII art representation of the Mandelbrot set, centered around the main cardioid. The '#' characters generally represent points within the set, while other characters indicate points outside the set that escape the iteration at different rates.

Token usage:

10 input, 1,531 output, {"promptTokensDetails": [{"modality": "TEXT", "tokenCount": 10}], "toolUsePromptTokenCount": 1172, "toolUsePromptTokensDetails": [{"modality": "TEXT", "tokenCount": 1172}], "thoughtsTokenCount": 1272}

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