Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

..i T-Mobile Wi-Fi 4:36 PM @ * 66% University of Houston COSC 1306 Spring 2018 H

ID: 3735677 • Letter: #

Question

..i T-Mobile Wi-Fi 4:36 PM @ * 66% University of Houston COSC 1306 Spring 2018 Homework 2 Objective: Write a program that makes use of functions, conditional execution, and looping to perform a repeated calculation and display the results. Problem: Using python, write a program that will take two integer dimensions from the uscr and then perform the necessary calculations to generate a Mandelbrot set of that dimension Deadline: Thursday, April 5, 2018, 1159 PM Requirements: Submit to Blackboard a single python file named "homework2py". Inclade in the header your name, PSID, and the homework number. Use meaningful variable and function names in order to: 1. Accept input from the user on the width and height of the image to be displayed. 2. Write a function to perform the iterations for a single point in the complex plane with a specified upper limit on the iterations. This function should return an indication of whether the given point is in or out of the Mandelbrot set. . Write a function to iterate over the given width and height; determining the corresponding value of the point in the complex plane and calling the iteration function for each point. 4 Output an "in or "out" symbol for each point calculated in order to create an "image

Explanation / Answer

import turtle
import math

# screen size (provided in pixels)
screen_x, screen_y = 800, 600

# complex plane limits
complex_Plane_X, complex_Plane_Y = (-2.0, 2.0), (-1.0, 2.0)

step = 2

def mandelbrot(a , b , num=30):
if abs(a) > 10 ** 12:
return float("nan")
elif num > 0:
return mandelbrot(a ** 2 + b, b, num - 1)
else:
return a ** 2 + b


# turtle config
turtle.tracer(0, 0)
turtle.setup(screen_x, screen_y)
turtle.bgcolor("blue")
screen = turtle.Screen()
screen.title("Mandelbrot Fractal (discretization step = %d)" % (int(step)))
mTurtle = turtle.Turtle()
mTurtle.penup()
mTurtle.shape("turtle")

# px * pixelToX = x in complex plane coordinates
pixel_To_X, pixel_To_Y = (complex_Plane_X[1] - complex_Plane_X[0])/screen_x, (complex_Plane_Y[1] - complex_Plane_Y[0])/screen_y

# plot
for px in range(-screen_x/3, screen_x/3, int(step)):
for py in range(-screen_y/3, screen_y/3, int(step)):
x, y = px * pixel_To_X, py * pixel_To_Y
m = mandelbrot(0, x + 1j * y)
if not math.isnan(m.real):
color = [abs(math.sin(m.imag)) for i in range(3)]
mTurtle.color(color)
mTurtle.dot(2, color)
mTurtle.goto(px, py)
turtle.update()

turtle.mainloop()