48
An Abstract Environment: Picture Language (runes.py) Need also graphics.py + image2gif.py

Lecture2supp-PictureLanguage

Embed Size (px)

DESCRIPTION

picture programming

Citation preview

Page 1: Lecture2supp-PictureLanguage

An Abstract Environment:

Picture Language

(runes.py) Need also graphics.py + image2gif.py

Page 2: Lecture2supp-PictureLanguage

Elements of Programming

1.  Primitives 2. Means of Combination 3. Means of Abstraction

Page 3: Lecture2supp-PictureLanguage

Primitive building blocks

show(rcross_bb)

Picture object

Page 4: Lecture2supp-PictureLanguage

Primitive building blocks

show(corner_bb)

Page 5: Lecture2supp-PictureLanguage

Primitive building blocks

show(sail_bb)

Page 6: Lecture2supp-PictureLanguage

Primitive building blocks

show(nova_bb)

Page 7: Lecture2supp-PictureLanguage

Primitive building blocks

show(heart_bb)

Page 8: Lecture2supp-PictureLanguage

Applying operations

op(picture) Example: show(heart_bb)

Page 9: Lecture2supp-PictureLanguage

Primitive Operation: Rotating to the Right

clear_all() show(quarter_turn_right(sail_bb))

operation picture

result is a picture object!

Page 10: Lecture2supp-PictureLanguage

Derived Operation: Rotating Upside Down

def turn_upside_down(pic): return quarter_turn_right( quarter_turn_right(pic)) clear_all() show(turn_upside_down( sail_bb))

Page 11: Lecture2supp-PictureLanguage

How about Rotating to the Left?

clear_all() def quarter_turn_left(pic): return quarter_turn_right( turn_upside_down( pic))

show(quarter_turn_left( sail_bb))

Page 12: Lecture2supp-PictureLanguage

Means of Combination: Stacking clear_all()

show(stack(rcross_bb,sail_bb))

Page 13: Lecture2supp-PictureLanguage

Multiple Stacking clear_all()

show(stack(rcross_bb, stack(rcross_bb, sail_bb)))

Page 14: Lecture2supp-PictureLanguage

Placing pictures one beside the other

def beside(pic1, pic2): return quarter_turn_right( stack( quarter_turn_left(pic2), quarter_turn_left(pic1))) clear_all() show(beside(sail_bb, rcross_bb))

Page 15: Lecture2supp-PictureLanguage

A complex object

clear_all() show( stack( beside( quarter_turn_right(rcross_bb), turn_upside_down(rcross_bb)), beside( rcross_bb, quarter_turn_left(rcross_bb))))

Let’s give it a name: make_cross

Page 16: Lecture2supp-PictureLanguage

stack( beside( quarter_turn_right(rcross_bb), turn_upside_down(rcross_bb)), beside( rcross_bb, quarter_turn_left(rcross_bb)))

Page 17: Lecture2supp-PictureLanguage

stack( beside( quarter_turn_right(pic), turn_upside_down(pic)), beside( pic, quarter_turn_left(pic)))

Page 18: Lecture2supp-PictureLanguage

def make_cross(pic): return stack( beside( quarter_turn_right(pic), turn_upside_down(pic)), beside( pic, quarter_turn_left(pic)))

Page 19: Lecture2supp-PictureLanguage

Naming your objects clear_all() my_pic = make_cross(sail_bb) show(my_pic) my_pic_2 = make_cross(nova_bb) show(my_pic_2)

Page 20: Lecture2supp-PictureLanguage

Repeating the pattern

clear_all() show(make_cross(make_cross(nova_bb)))

Page 21: Lecture2supp-PictureLanguage

Repeating multiple times clear_all() def repeat_pattern(n, pat, pic): if n == 0: return pic else: return pat(repeat_pattern(n-1, pat, pic)) show(repeat_pattern(4, make_cross, nova_bb))

RECURSION

function Qn: What does

repeat_pattern return?

Page 22: Lecture2supp-PictureLanguage

Anonymous Functions def square(x): return x * x foo = lambda x: x * x foo(1) foo(4)

1 16

Page 23: Lecture2supp-PictureLanguage

New Patterns clear_all() show(repeat_pattern(3, lambda pic: beside(pic, pic), nova_bb)) clear_all() show(repeat_pattern(3, lambda pic: stack(pic, pic), nova_bb))

anonymous function

Page 24: Lecture2supp-PictureLanguage

Another nice pattern clear_all()

show(repeat_pattern(4, make_cross, rcross_bb))

Page 25: Lecture2supp-PictureLanguage

What about 3 rows? clear_all() show(stack_frac(1/3, rcross_bb, sail_bb)) clear_all() show(stack_frac(1/3, rcross_bb, stack(rcross_bb, rcross_bb)))

Page 26: Lecture2supp-PictureLanguage

Stacking n times def stackn(n,pic): if n == 1: return pic else: return stack_frac(1/n, pic, stackn(n-1, pic)) clear_all() show(stackn(3, nova_bb))

clear_all() show(stackn(5, nova_bb))

Page 27: Lecture2supp-PictureLanguage

A rectagular quilting pattern clear_all() show(stackn(5, quarter_turn_right( stackn(5, quarter_turn_left(nova_bb)))))

Page 28: Lecture2supp-PictureLanguage

A rectangular quilting proc def nxn(n,pic): return stackn(n, quarter_turn_right( stackn(n, quarter_turn_left(pic)))) clear_all() show(nxn(3, make_cross(rcross_bb)))

What does nxn do?

Page 29: Lecture2supp-PictureLanguage

No idea how a picture is

represented

Page 30: Lecture2supp-PictureLanguage

No idea how the operations do

their work

Page 31: Lecture2supp-PictureLanguage

Yet, we can build complex

pictures

Page 32: Lecture2supp-PictureLanguage

Functional Abstraction

Page 33: Lecture2supp-PictureLanguage

We can also make Stereograms!!

Page 34: Lecture2supp-PictureLanguage

STEREOGRAM

GENERATOR depth map stereogram

function

Functional Abstraction

Page 35: Lecture2supp-PictureLanguage

Can’t see stereograms?

Page 36: Lecture2supp-PictureLanguage

Anaglyphs

Page 37: Lecture2supp-PictureLanguage

And if you think this is cool …

Page 38: Lecture2supp-PictureLanguage

You haven’t seen nothing

yet!

Page 39: Lecture2supp-PictureLanguage
Page 40: Lecture2supp-PictureLanguage

What have we learned?

•  Functional Abstraction •  Functions are objects

Page 41: Lecture2supp-PictureLanguage

Helps us manage complexity by

focusing on high-level problem solving

Page 42: Lecture2supp-PictureLanguage

Creating 3D objects We use greyscale to represent depth – Black is nearest to you – White is furthest away

means

Page 43: Lecture2supp-PictureLanguage

Creating 3D objects

means

Page 44: Lecture2supp-PictureLanguage

Overlay Operation clear_all() show(overlay(sail_bb, rcross_bb))

Page 45: Lecture2supp-PictureLanguage

Advanced Overlay Operation clear_all() show(overlay_frac(1/4, corner_bb, heart_bb))

Page 46: Lecture2supp-PictureLanguage

Scaling clear_all() show(scale(1/2, heart_bb))

Page 47: Lecture2supp-PictureLanguage

depth map stereogram

stereogram(scale(1/2, heart_bb))

Stereogram generator

depth map

Page 48: Lecture2supp-PictureLanguage