# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3 (fractals)
#     language: python
#     name: fractals
# ---

# ### Generating the distance from the complex origin.

import math
def generate_r(p,c,i):
    '''
      outputs a radius, r, from the origin of the complex plane.
      p: starting point as a complex number
      c: constant complex number
      i: number of iterations
    '''
    z = p
    iterations = 0
    r = 0
    while iterations < i:
        z = z**2 + c
        r = math.sqrt(z.real**2 + z.imag**2)
        iterations += 1
    return round(r,2)


# +
c = -0.7 + 0.27j
p = 0.5 + 1j
i = 2

generate_r(p,c,i)
# -

# ### Function from blog post part 1

# +
import matplotlib.pyplot as plt
import numpy as np

def julia_set(w, h, c = -0.7+ 0.27j, zoom=1, niter=256):
    """ A julia set of geometry (width x height) and iterations 'niter' """

    # Why (hxw) ? Because numpy creates a matrix as row x column
    # and height represents the y co-ordinate or rows and
    # width represents the x co-ordinate or columns.
    pixels = np.arange(w*h,dtype=np.uint16).reshape(h, w)
    
    for x in range(w): 
        for y in range(h):
            # calculate the initial real and imaginary part of z,
            # based on the pixel location and zoom and position values
            zx = 1.5*(x - w/2)/(0.5*zoom*w) 
            zy = 1.0*(y - h/2)/(0.5*zoom*h)

            for i in range(niter):
                radius_sqr = zx*zx + zy*zy
                # Iterate till the point is outside
                # the circle with radius 2.
                if radius_sqr > 4: break
                # Calculate new positions
                zy,zx = 2.0*zx*zy + c.imag, zx*zx - zy*zy + c.real

            color = (i >> 21) + (i >> 10)  + i*8
            pixels[y,x] = color
  
    # display the created fractal 
    plt.imshow(pixels)
    plt.show()


# -

julia_set(1024,768,zoom=4,niter = 256)

arr = np.arange(1024*768,dtype=np.uint16)
print(arr)
print(len(arr))
arr2 = np.arange(1024*768,dtype=np.uint16).reshape(768,1024)
print(arr2)
print(len(arr2))

arr = np.arange(1024*768)
print(arr)
print(len(arr))
arr2 = np.arange(1024*768).reshape(768,1024)
print(arr2)
print(len(arr2))



StarcoderdataJupyterScriptsDedupFiltered