%run "boaz_utils.ipynb"
Today:
2pm-3pm review of this week and ask me anything
Optional 3:30pm in Auditorium I crypto talk
At 5pm info on Vanderbilt university
Come take pictures with me at lunch!
Hike tomorrow Entoto
Tomorrow morning: Trip to Entoto with TAs! More details to follow. Tell your lab leader if you're interested in joining!
This image is made of 1,000,000 little dots- pixels ($1024\times 768$).
Each pixel has a different color.
All colors are obtained by mixing red, green and blue
from PIL import Image
import numpy as np
def empty_screen(w=100,h=100):
res = [ [color(255,255,255) for i in range(h)] for j in range(w)]
return res
def color(red=0,green=0,blue=0):
return (red,green,blue)
def array_to_image(array, scale=2):
w = len(array)
h = len(array[0])
data = np.zeros((h*scale, w*scale, 3), dtype=np.uint8)
for i in range(w):
for j in range(h):
for k in range(scale):
for l in range(scale):
data[(h-1)*scale-(j*scale + k),i*scale+l] = array[i][j]
img = Image.fromarray(data, 'RGB')
img.save('my.png')
return dsp.Image(filename='my.png')
def plot_array(array, scale=2):
dsp.clear_output(True)
dsp.display_png(array_to_image(array,scale))
# sys.stdout.flush()
def plot_array(array, scale=2):
dsp.clear_output(True)
w = len(array)
h = len(array[0])
data = np.zeros((h*scale, w*scale, 3), dtype=np.uint8)
for i in range(w):
for j in range(h):
for k in range(scale):
for l in range(scale):
data[(h-1)*scale-(j*scale + k),i*scale+l] = array[i][j]
plt.imshow(data,interpolation="nearest")
plt.show()
def plot_array(array, scale=2):
dsp.clear_output(True)
w = len(array)
h = len(array[0])
data = np.zeros((h*scale, w*scale, 3), dtype=np.uint8)
for i in range(w):
for j in range(h):
for k in range(scale):
for l in range(scale):
data[(h-1)*scale-(j*scale + k),i*scale+l] = array[i][j]
img = Image.fromarray(data, 'RGB')
display(img) # ,Image.LANCZOS)
return None
def RGB(red,green,blue):
s = empty_screen(100,100)
for i in range(len(s)):
for j in range(len(s[i])):
s[i][j] = color(red,green,blue)
return array_to_image(s)
from ipywidgets import interact,interactive, fixed, Box
import ipywidgets as widgets
import IPython.display as dsp
def demo_RGB():
interact(RGB, red=widgets.IntSlider(min=0,max=255,step=50,value=255),
green=widgets.IntSlider(min=0,max=255,step=50,value=0),
blue=widgets.IntSlider(min=0,max=255,step=50,value=0))
demo_RGB()
interactive(children=(IntSlider(value=255, description='red', max=255, step=50), IntSlider(value=0, descriptio…
color(red,green,blue)
: just returns list of 3 numbers
empty_screen(height,width)
: returns an width
$\times$ height
array s
(namely a list of width
lists, each of them is of length height
). For every x
between 0 and width
and y
between 0 and height
, s[x][y] = color(255,255,255)
.
plot_array(s)
: plots the array s
on the screen where s[0][0]
corresponds to the bottom left corner and s[width][length]
corresponds to the top right corner.
We will now demonstrate how to use these functions:
c = [255,0,255]
s = [[c for i in range(100)] for i in range(100)]
plot_array(s)
color(blue=255)
def empty_screen(w=100,h=100):
white = [255,255,255]
res = [ [white for i in range(h)] for j in range(w)]
return res
s = empty_screen(100,100)
plot_array(s)
blue = color(red=0,blue=255,green=0)
for i in range(100):
s[i][55] = blue
plot_array(s)
s = empty_screen(100,100)
for i in range(100):
for j in range(100):
s[i][j] = [i*2,j*2,j*2]
plot_array(s)
s = empty_screen(100,100)
for i in range(100):
for j in range(100):
if i % 10:
s[i][j] = [255,255,255]
else:
s[i][j] = [0,0,0]
plot_array(s)
s = empty_screen(100,100)
white = [255,255,255]
purple = [128,0,128]
for i in range(100):
s[i][i]= purple
if i>=10:
s[i-10][i-10]=white
plot_array(s)
import math
def sine(angle):
return math.sin((angle/360.0)*2*math.pi)
def cosine(angle):
return math.cos((angle/360.0)*2*math.pi)
def cannon(angle,speed,time, gravity=9.8):
x = speed*time*cosine(angle)
y = speed*time*sine(angle) -(gravity/2.0)*(time**2)
return round(x,3),round(y,3)
cannon(90,10,1,gravity=9.8)
(0.0, 5.1)
cannon(45,10,200,gravity=0)
cannon(30,10,100,gravity=0)
cannon(70,10,100,gravity=0)
cannon(45,100,10)
cannon(45,100,20)
cannon(30,100,10)
cannon(60,100,10)
def draw_cannon(angle,speed,g=9.8):
s = empty_screen(100,50)
x =0; y =0; t=0.0
while x<100 and y>=0 and y<50:
s[x][y] = [0,0,255]
(x,y) = cannon(angle,speed,t,g)
x= int(x)
y = int(y)
s[x][y] = [255,0,0]
t += 1.0/speed
plot_array(s)
draw_cannon(60,10,1.6)