I'm new to the coding world, so take it easy on me here. I'm trying to create a numeric simulations of a square pan being heated at exactly one point i.e. with a wire. Unfortunately, my functions keep failing my doc.tests and I don't understand why. Any help would be much appreciated
import sys
sys.path.append('/home/courses/python')
from logic import *
from numpy import *
def create_plate(size, plate_temperature):
plate_temperatures = empty([size, size])
plate_temperatures[:,:] = plate_temperature
return plate_temperatures
def simulate_plate(plate_temperatures,hot_x,hot_y, hot_temp,air_temp, htc,hcc,ahtc):
""" Simulate one time step on the plate, given array of temperatures, etc. """
precondition()
plate_temperatures[hot_x,hot_y] = hot_temp
#number rows = len(input)
#number cols = len(input[0])
# First, calculate heat flow _into_ every 1cmx1cm square
heat_in = zeros_like(plate_temperatures)
# The "length" of an array is the number of rows; the length of a row is the number of columns.
#because the plate is always a square precondition range(len(plate_temperatures)) ==range(len(plate_temperatures[row_place]))
for row_place in range(len(plate_temperatures)): # is this accurate?
for column_place in range(len(plate_temperatures[row_place])):
plate_temperatures[hot_x,hot_y] = hot_temp
faces = 2 # Number of faces exposed to the air, top and bottom of plate
if(column_place!=0): #square is not on left edge of plate
from_left = (plate_temperatures[row_place,column_place-1] - plate_temperatures[row_place,column_place]) * htc # heat from left
else:#square is on left edge; nothing to the left of it
from_left = 0
if(column_place<(len(plate_temperatures[row_place])-1)): #square is not on the right edge of plate
from_right = (plate_temperatures[row_place,column_place+1] - plate_temperatures[row_place,column_place]) * htc # heat from right
else: #square is on the right edge; nothing to the right of it
from_right = 0
if row_place !=0: #square is not on the top edge of plate
from_above =(plate_temperatures[row_place+1,column_place] - plate_temperatures[row_place,column_place]) * htc # heat from above
else: # square is on the top edge of plate
from_above = 0
if row_place< (len(plate_temperatures)-1):#square is not on the bottom edge of the plate
from_below = (plate_temperatures[row_place-1,column_place] - plate_temperatures[row_place,column_place]) * htc # heat from below
else:#square is on the bottom edge of the plate
from_below = 0
from_air = (air_temp - plate_temperatures[row_place,column_place]) * faces * ahtc # heat from air
heat_in[row_place,column_place] = from_left + from_right + from_above + from_below + from_air
# Now, add the heat we found above to the old temperatures
plate_temperatures[row_place, column_place] += (heat_in[row_place,column_place] * hcc)
# restore the temperature of the heated spot
plate_temperatures[hot_x,hot_y] = hot_temp
def simulate_plate_N_steps(plate_temperatures,hot_x,hot_y, hot_temp,air_temp, htc,hcc,ahtc, N):
""" Just call the one-time-step function N times... """
all_averages = [] # log of temperatures at end of time steps
for t in range(N):
simulate_plate(plate_temperatures,hot_x,hot_y, hot_temp,air_temp, htc,hcc,ahtc)
all_averages.append(average_temp(plate_temperatures))
print "Completed", N, "simulated time steps; end-step average temps were:", all_averages
# copied from http://docs.python.org/lib/module-doctest.html
def _test():
import doctest
result = doctest.testmod()
if result[0] == 0:
print "Wahoo! Passed all", result[1], __file__.split('/')[-1], "tests!"
else:
print "Rats!"
if __name__ == "__main__": _test()
Related
I am simulating 1D heat conduction using brownian motion in python. The question here is to track if the particle pass the inerface of the left or right cell. I have to count it somehow, could you please purpose solution or I should update code concept(rethink model).
Short desciption: Medium is consist of cell, in each cell it has own quantity of particles. The particles are moving from one cell to another. First and last cell has constant quantity of particle (in this case 500 and 0). Result gives the tempertaure profile along x. If we know the number of particles that are pass the interface of the cell (left or right) we might find Heat flux.
Edit: I've made counting of particle pass throught interface (from right or left). But in "theory" the value of Heat Flux should me constant. So, I gues there is the problem with my code(specified code excerpt). Could you please review it. Am I counting right?
Edit code:
import numpy as np
import matplotlib.pyplot as plt
def Cell_dist(a, dx):
res = [[] for i in range(N)]
for i in range(N):
for value in a:
if dx*i < value < dx*i+dx:
res[i].append(value)
return res
L = 0.2 # length of the medium
N = 10 # number of cells
dx = L/N # cell dimension
dt = 1 # time step
dur = 60 # duration
M = 20 # number of particles
ro = 8930 # density
k = 391 # thermal conductivity
C_p = 380 # specific heat capacity
T_0 = 100 # maintained temperature
T_r = 0 # reference temperature
DT = T_0 - T_r # characteristic temperature
a = k / ro / C_p # thermal diffusivity of the copper
dh_r = ro * C_p * dx * DT / M # refernce elementary enthalpy
c = (2*a*dt)**(1/2) # diffusion length
pos = [[] for i in range(N)] # creating cells
for time_step in range(dur):
M_plus = [[] for i in range(N-1)]
M_minus = [[] for i in range(N-1)]
flux = [0] * (N-1)
unirnd = np.random.uniform(0,dx, M).tolist() # uniform random distribution
pos[0] = unirnd # 1st cell BC
pos[-1] = [] # last cell BC at T_r = 0
curr_pos = sorted([x for sublist in pos for x in sublist]) # flatten list of particles (concatenation)
M_t = len(curr_pos) # number of particles at instant time step
pos_tmp = []
# Move each particle
for i in range(M_t):
normal_distr = np.random.default_rng().normal(0,1)
displacement = c*normal_distr
final_pos = curr_pos[i] + displacement
pos_tmp.append(final_pos)
#HERE is the question________________________
if normal_distr > 0:
for i in range(1,len(M_plus)-1):
if i*dx < final_pos < (i+1)*dx:
M_plus[i-1].append(1)
else:
for i in range(len(M_minus)):
if i*dx < final_pos < (i+1)*dx:
M_minus[i].append(1)
#END of the question________________________
for i in range(N-1):
flux[i] = (len(M_plus[i])-len(M_minus[i]))*dh_r/dt/1000
pos_new = Cell_dist(pos_tmp,dx)
pos_new[0] = unirnd
pos = pos_new
walker_number_in_cell = []
for i in range(N):
walker_number_in_cell.append(len(pos[i]))
T_n = []
for num in walker_number_in_cell:
T_n.append(T_r + num*dh_r/ro/C_p/dx)
# __________Plotting FLUX profile__________
x_a = [0]*(N-1)
for i in range(0, N-1):
x_a[i] = "{}".format(i+1)+"_{}".format(i+2)
plt.plot(x_a,flux[0:N-1],'-')
plt.xlabel('Interface')
plt.ylabel('Flux')
plt.show()
I'm attempting to extend the 'tail' of an arrow. So far I've been able to draw a line through the center of the arrow, but this line extends 'both' ways, rather than in just one direction. The script below shows my progress. Ideally I would be able to extend the tail of the arrow regardless of the orientation of the arrow image. Any suggestions on how to accomplish this. Image examples below, L:R start, progress, goal.
# import image and grayscale
image = cv2.imread("image path")
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow("original",image)
# inverts black and white
gray = 255 - image
cv2.imshow("Inverted", gray)
# Extend the borders for the line
extended = cv2.copyMakeBorder(gray, 20, 20, 10, 10, cv2.BORDER_CONSTANT)
cv2.imshow("extended borders", extended)
# contour finding
contours, hierarchy = cv2.findContours(extended, 1, 2)
cont = contours[0]
rows,cols = extended.shape[:2]
[vx,vy,x,y] = cv2.fitLine(cont, cv2.DIST_L2,0,0.01,0.01)
leftish = int((-x*vy/vx) + y)
rightish = int(((cols-x)*vy/vx)+y)
line = cv2.line(extended,(cols-1,rightish),(0,leftish),(255,255,255), 6)
cv2.imshow("drawn line", line)
"Moments" can be strange things. They're building blocks and show up most often in statistics.
It helps to have a little background in statistics, and see the application of those calculations to image data, which can be considered a set of points. If you've ever calculated the weighted average or "centroid" of something, you'll recognize some of the sums that show up in "moments".
Higher order moments can be building blocks to higher statistical measures such as covariance and skewness.
Using covariance, you can calculate the major axis of your set of points, or your arrow in this case.
Using skewness, you can figure out which side of a distribution is heavier than the other... i.e. which side is the arrow's tip and which is its tail.
This should give you a very precise angle. The scale/radius however is best estimated using other ways. You'll notice that the radius estimated from the area of the arrow fluctuates a little. You could find the points belonging to the arrow that are furthest away from the center, and take that as a somewhat stable length.
Here's a longish program that implements the two ideas above and shows the direction of an arrow:
#!/usr/bin/env python3
import os
import sys
import numpy as np
import cv2 as cv
# utilities to convert between 2D vectors and complex numbers
# complex numbers are handy for rotating stuff
def to_complex(vec):
assert vec.shape[-1] == 2
if vec.dtype == np.float32:
return vec.view(np.complex64)
elif vec.dtype == np.float64:
return vec.view(np.complex128)
else:
assert False, vec.dtype
def from_complex(cplx):
if cplx.dtype == np.complex64:
return cplx.view(np.float32)
elif cplx.dtype == np.complex128:
return cplx.view(np.float64)
else:
assert False, cplx.dtype
# utilities for drawing with fractional bits of position
# just to make a pretty picture
def iround(val):
return int(round(val))
def ipt(vec, shift=0):
if isinstance(vec, (int, float)):
return iround(vec * 2**shift)
elif isinstance(vec, (tuple, list, np.ndarray)):
return tuple(iround(el * 2**shift) for el in vec)
else:
assert False, type(vec)
# utilities for affine transformation
# just to make a pretty picture
def rotate(degrees=0):
# we want positive rotation
# meaning move +x towards +y
# getRotationMatrix2D does it differently
result = np.eye(3).astype(np.float32)
result[0:2, 0:3] = cv.getRotationMatrix2D(center=(0,0), angle=-degrees, scale=1.0)
return result
def translate(dx=0, dy=0):
result = np.eye(3).astype(np.float32)
result[0:2,2] = [dx, dy]
return result
# main logic
def calculate_direction(im):
# using "nonzero" (default behavior) is a little noisy
mask = (im >= 128)
m = cv.moments(mask.astype(np.uint8), binaryImage=True)
# easier access... see below for details
m00 = m['m00']
m10 = m['m10']
m01 = m['m01']
mu00 = m00
mu20 = m['mu20']
mu11 = m['mu11']
mu02 = m['mu02']
nu30 = m['nu30']
nu03 = m['nu03']
# that's just the centroid
cx = m10 / m00
cy = m01 / m00
centroid = np.array([cx, cy]) # as a vector
# and that's the size in pixels:
size = m00
# and that's an approximate "radius", if it were a circle which it isn't
radius = (size / np.pi) ** 0.5
# (since the "size" in pixels can fluctuate due to resampling, so will the "radius")
# wikipedia helpfully mentions "image orientation" as an example:
# https://en.wikipedia.org/wiki/Image_moment#Examples_2
# we'll use that for the major axis
mup20 = mu20 / mu00
mup02 = mu02 / mu00
mup11 = mu11 / mu00
theta = 0.5 * np.arctan2(2 * mup11, mup20 - mup02)
#print(f"angle: {theta / np.pi * 180:+6.1f} degrees")
# we only have the axis, not yet the direction
# we will assess "skewness" now
# https://en.wikipedia.org/wiki/Skewness#Definition
# note how "positive" skewness appears in a distribution:
# it points away from the heavy side, towards the light side
# fortunately, cv.moments() also calculates those "standardized moments"
# https://en.wikipedia.org/wiki/Standardized_moment#Standard_normalization
skew = np.array([nu30, nu03])
#print("skew:", skew)
# we'll have to *rotate* that so it *roughly* lies along the x axis
# then assess which end is the heavy/light end
# then use that information to maybe flip the axis,
# so it points in the direction of the arrow
skew_complex = to_complex(skew) # reinterpret two reals as one complex number
rotated_skew_complex = skew_complex * np.exp(1j * -theta) # rotation
rotated_skew = from_complex(rotated_skew_complex)
#print("rotated skew:", rotated_skew)
if rotated_skew[0] > 0: # pointing towards tail
theta = (theta + np.pi) % (2*np.pi) # flip direction 180 degrees
else: # pointing towards head
pass
print(f"angle: {theta / np.pi * 180:+6.1f} degrees")
# construct a vector that points like the arrow in the picture
direction = np.exp([1j * theta])
direction = from_complex(direction)
return (radius, centroid, direction)
def draw_a_picture(im, radius, centroid, direction):
height, width = im.shape[:2]
# take the source at half brightness
canvas = cv.cvtColor(im // 2, cv.COLOR_GRAY2BGR)
shift = 4 # prettier drawing
cv.circle(canvas,
center=ipt(centroid, shift),
radius=ipt(radius, shift),
thickness=iround(radius * 0.1),
color=(0,0,255),
lineType=cv.LINE_AA,
shift=shift)
# (-direction) meaning point the *opposite* of the arrow's direction, i.e. towards tail
cv.line(canvas,
pt1=ipt(centroid + direction * radius * -3.0, shift),
pt2=ipt(centroid + direction * radius * +3.0, shift),
thickness=iround(radius * 0.05),
color=(0,255,255),
lineType=cv.LINE_AA,
shift=shift)
cv.line(canvas,
pt1=ipt(centroid + (-direction) * radius * 3.5, shift),
pt2=ipt(centroid + (-direction) * radius * 4.5, shift),
thickness=iround(radius * 0.15),
color=(0,255,255),
lineType=cv.LINE_AA,
shift=shift)
return canvas
if __name__ == '__main__':
imfile = sys.argv[1] if len(sys.argv) >= 2 else "p7cmR.png"
src = cv.imread(imfile, cv.IMREAD_GRAYSCALE)
src = 255 - src # invert (white arrow on black background)
height, width = src.shape[:2]
diagonal = np.hypot(height, width)
outsize = int(np.ceil(diagonal * 1.3)) # fudge factor
cv.namedWindow("arrow", cv.WINDOW_NORMAL)
cv.resizeWindow("arrow", 5*outsize, 5*outsize)
angle = 0 # degrees
increment = +1
do_spin = True
while True:
print(f"{angle:+.0f} degrees")
M = translate(dx=+outsize/2, dy=+outsize/2) # rotate(degrees=angle) # translate(dx=-width/2, dy=-height/2)
im = cv.warpAffine(src, M=M[:2], dsize=(outsize, outsize), flags=cv.INTER_CUBIC, borderMode=cv.BORDER_REPLICATE)
# resampling introduces blur... except when it's an even number like 0 degrees, 90 degrees, ...
# so at even rotations, things will jump a little.
# this rotation is only for demo purposes
(radius, centroid, direction) = calculate_direction(im)
canvas = draw_a_picture(im, radius, centroid, direction)
cv.imshow("arrow", canvas)
if do_spin:
angle = (angle + increment) % 360
print()
key = cv.waitKeyEx(30 if do_spin else -1)
if key == -1:
continue
elif key in (0x0D, 0x20): # ENTER (CR), SPACE
do_spin = not do_spin # toggle spinning
elif key == 27: # ESC
break # end program
elif key == 0x250000: # VK_LEFT
increment = -abs(increment)
angle += increment
elif key == 0x270000: # VK_RIGHT
increment = +abs(increment)
angle += increment
else:
print(f"key 0x{key:02x}")
cv.destroyAllWindows()
I was working on a function that accepts a length in pixels from the center of the regular figure and count of sides.
Ex:
draw_figure(r=200, side_count=4) : It would draw quadrilateral
draw_figure(r=100, side_count=8) : It would draw an octagon
I tried to make a list of all coordinates that are at the same radius from the center, get a random coordinate and based on length get rest of them, but I couldn't figure it out though.
from time import sleep
import subprocess
import pyautogui
from math import cos, radians, sqrt
from random import choice
def draw_figure(side_count, r):
# Open mspaint and move the cursorto the center.
subprocess.Popen("mspaint", shell=True)
sleep(1)
x, y = pyautogui.size()
pyautogui.moveTo(x/2, y/2)
# Define cos of alpha value in center and side_len of figure
a = 360/side_count
cos_a = cos(radians(a))
side_len = round(sqrt(2*(r**2) - 2*r*cos_a), 3)
# Get every x/y pair on screen inside a set
all_points = set()
for x_cord in list(range(x)):
for y_cord in list(range(y)):
all_points.add((x_cord, y_cord))
# Create points list with all x/y tuples that all are at same
# distance from center by r count of pixels
points = []
for point in all_points:
try:
R = round(sqrt((point[0] - x/2)**2 + (point[1] -
y/2)**2), 3)
if R == r:
points.append(point)
except ValueError:
pass
# Pick a random coordinate and find x/y of side points
r_point = choice(points)
square_vectors = []
pyautogui.moveTo(r_point)
for point in points:
try:
test_len = round(sqrt((r_point[0] - point[0])**2 +
(r_point[1] - point[1])**2), 3)
if test_len == side_len:
vector = tuple([point[0] - r_point[0],
point[1] - r_point[1]])
square_vectors.append(vector)
except ValueError:
pass
for vector in square_vectors:
pyautogui.dragRel(vector)
return square_vectors
Example:
A________B O is center of square
| \ | OA = r
| O \ |
D|___ \_|C
I'm running quite a complex code so I won't bother with details as I've had it working before but now im getting this error.
Particle is a 3D tuple filled with 0 or 255, and I am using the scipy centre of mass function and then trying to turn the value into its closest integer (as I'm dealing with arrays). The error is found with on the last line... can anyone explain why this might be??
2nd line fills Particle
3rd line deletes any surrounding particles with a different label (This is in a for loop for all labels)
Particle = []
Particle = big_labelled_stack[x_start+20:x_stop+20,y_start+20:y_stop+20,z_start+20:z_stop+20]
Particle = np.where(Particle == i ,255,0)
CoM = scipy.ndimage.measurements.center_of_mass(Particle)
CoM = [ (int(round(x)) for x in CoM ]
Thanks in advance. If you need more code just ask but I dont think it will help you and its very messy.
################## MORE CODE
border = 30
[labelled_stack,no_of_label] = label(labelled,structure_array,output_type)
# RE-LABEL particles now no. of seeds has been reduced! LAST LABELLING
#Increase size of stack by increasing borders and equal them to 0; to allow us to cut out particles into cube shape which else might lye outside the border
h,w,l = labelled.shape
big_labelled_stack = np.zeros(shape=(h+60,w+60,l+60),dtype=np.uint32)
# Creates an empty border around labelled_stack full of zeros of size border
if (no_of_label > 0): #Small sample may return no particles.. so this stage not neccesary
info = np.zeros(shape=(no_of_label,19)) #Creates array to store coordinates of particles
for i in np.arange(1,no_of_label,1):
coordinates = find_objects(labelled_stack == i)[0] #Find coordinates of label i.
x_start = int(coordinates[0].start)
x_stop = int(coordinates[0].stop)
y_start = int(coordinates[1].start)
y_stop = int(coordinates[1].stop)
z_start = int(coordinates[2].start)
z_stop = int(coordinates[2].stop)
dx = (x_stop - x_start)
dy = (y_stop - y_start)
dz = (z_stop - z_start)
Particle = np.zeros(shape=(dy,dx,dz),dtype = np.uint16)
Particle = big_labelled_stack[x_start+30:x_start+dx+30,y_start+30:y_start+dy+30,z_start+30:z_start+dz+30]
Particle = np.where(Particle == i ,255,0)
big_labelled_stack[border:h+border,border:w+border,border:l+border] = labelled_stack
big_labelled_stack = np.where(big_labelled_stack == i , 255,0)
CoM_big_stack = scipy.ndimage.measurements.center_of_mass(big_labelled_stack)
C = np.asarray(CoM_big_stack) - border
if dx > dy:
b = dx
else: #Finds the largest of delta_x,y,z and saves as b, so that we create 'Cubic_Particle' of size 2bx2bx2b (cubic box)
b = dy
if dz > b:
b = dz
CoM = scipy.ndimage.measurements.center_of_mass(Particle)
CoM = [ (int(round(x))) for x in CoM ]
Cubic_Particle = np.zeros(shape=(2*b,2*b,2*b))
Cubic_Particle[(b-CoM[0]):(b+dx-CoM[0]),(b-CoM[1]):(b+dy-CoM[1]),(b-CoM[2]):(b+dz-CoM[2])] = Particle
volume = Cubic_Particle.size # Gives volume of the box in voxels
info[i-1,:] = [C[0],C[1],C[2],i,C[0]-b,C[1]-b,C[2]-b,C[0]+b,C[1]+b,C[2]+b,volume,0,0,0,0,0,0,0,0] # Fills an array with label.No., size of box, and co-ords
else:
print('No particles found, try increasing the sample size')
info = []
Ok, so I have a stack full of labelled particles, there are two things I am trying to do, first find the centre of masses of each particle with respect ot the labelled_stack which is what CoM_big_labelled_stack (and C) does. and stores the co-ords in a list (tuple) called info. I am also trying to create a cubic box around the particle, with its centre of mass as the centre (which is relating to the CoM variable), so first I use the find objects function in scipy to find a particle, i then use these coordinates to create a non-cubic box around the particle, and find its centre of mass.I then find the longest dimension of the box and call it b, creating a cubic box of size 2b and filling it with particle in the right position.
Sorry this code is a mess, I am very new to Python
I have been working on a project for my math class in which I am creating a Julia set generator. I had finally succeeded in generating it, but when I show the plot and save the image generated there are white lines all over it. They do not line up with x ticks or y ticks. When I view the saved image it had even more white lines. I have been searching to find what these might be, but I have found nothing.
import matplotlib.pyplot as plt
#Set up window with a name and size.
plt.figure("Julia Set Generator by Eric Kapilik", figsize=(7.0,7.0))
#Set Range of axes
plt.xlim([-2,2])
plt.ylim([-2,2])
#Add labels to axes
plt.xlabel("Real Numbers")
plt.ylabel("Imaginary Numbers")
plt.grid(b=False, which = "major", axis = "both")
plt.grid(b=False, which = "minor", axis = "both")
name = input("Save file as... \n")
#Ask for maximum amount of iterations to base colour
#selection off of with fractions.
max = int(input("What is the maximum amount of iterations you wish to run? "))
#Generate an array of colour names to be used to plot points.
#Set seed of array.
colourArray = ["r"]
for colourNum in range (1, max):
#if the place in array is between 0% and 25% then set the colour to red.
#Same method used for other three colours.
if colourNum >= 0 and colourNum <= (max/4):
colourArray.append("r") #red
elif colourNum > (max/4) and colourNum <= (max/2):
colourArray.append("y") #yellow
elif colourNum > (max/2) and colourNum <= ((3*max)/4):
colourArray.append("g") #green
elif colourNum > ((3*max)/4) and colourNum <= max:
colourArray.append("c") #cyan
#Get constant value of which the julia set is based off of.
#The real number component is plotted on the horizontal axis
#of a complex number grid so we will use x.
xConstant = float(input("Enter real number constant component: "))
#The imaginary nuber compenent of a complex number is plotted on the vertical axis,
#so we will use y in our real number grid (for simplicity's sake).
yConstant = float(input("Enter imaginary number constant component: "))
#Title the graph based on the constatn complex number entered.
plt.title(str(xConstant) + " + " + str(yConstant) + "i")
#See the starting coordinates to be tested and conditions
xTest = float(-2)
yTest = float(2)
stop = False
i = 0
xPrevious = xTest
yPrevious = yTest
#Using an escape time algorith, determine the amout of iterations of the recursion
#are needed for the coordinate to be attarcted to infinity.
#Continue doing this while the y value of the coordinate being tested is less
#than or equal to -2.
while yTest >= -2:
#We are following the recursive function of
#f(Z-1) = Z^2 + C
#Where Z is current coordinate, and C is the constant value.
#Reminder: Both Z and C are actually complex numbers but in our case we
#are using them both as real number coordinates on a real number grid.
xCurrent = ((xPrevious**2) - (yPrevious**2)) + xConstant
yCurrent = (2 * xPrevious * yPrevious) + yConstant
#Points that surpass a circle of radius 2 with a centre point at the origin
#are considered to indefinitely escape to infinity.
#So when the radius of the recursive coordinate based off of the tested coordinate
#becomes greater or equal to two we know it will be attaracted to infinity.
radius = xCurrent**2 + yCurrent**2
#"Is the point an escapee?"
if radius >= 2:
#Since the point has been defined as one that esacpes to infintity
#it is considered an escapee, so set that to true.
escapee = True
#"Is the point a prisoner?"
if i == max:
#The point is considered a prisoner if max iterations is reached and
#the point is still within the circle of radius 2.
#The testeed point will be considered a prisoner based off of the amount
#of iterations we selected, it is possible that with more iterations
#that we would find this to be an escapee.
prisoner = True
#If we have not defined what kind of point this is yet, then go to the next
#iteration of the recursion. i is the number of iterations completed for
#the test point.
if escapee == False and prisoner == False:
i = i + 1
#Out with the old, in with the new. Set the current points to the previous
#points for the next iteration.
xPrevious = xCurrent
yPrevious= yCurrent
#If, however, we have defined the point, then colour it based off of
#the amount of iterations using the array of colours generated at the
#beginning to select the colour.
if escapee == True or prisoner == True:
#This sets the black points that are prisoners, this is the body
#of the julia set.
if i == max:
colourPoint = "k,"
else:
#Colour the point and concatenate a ",", which means to plot this point
#as a pixel.
colourPoint = colourArray[i] + ","
#Plot the point! (Most satisfying part)
plt.plot(xTest, yTest, colourPoint)
#Determine the percentage finished, to give user an idea of how the
#renderig is going. (Not nessecary, but appreciable)
percent = int(((yTest-2)/4) * (-100))
print(str(percent) + "%")
#After setting a colour and plotting the point, jump to the next test coordinate.
#Once the end of the line is reached, jump down one.
if xTest >= 2:
xTest = -2
yTest = yTest - 0.01
else:
xTest= xTest + 0.01
#Reset the starting conditions.
i = 0
escapee = False
prisoner = False
xPrevious = xTest
yPrevious = yTest
#Show the beauty.
print("100%")
print("Wait for matplotlib to finish things up...\nWill take a minute...")
plt.show()
plt.savefig(name)
Plot that was generated:
Saved image, with even more white lines: