turning math into pictures l.
Skip this Video
Loading SlideShow in 5 Seconds..
Turning Math into Pictures PowerPoint Presentation
Download Presentation
Turning Math into Pictures

Loading in 2 Seconds...

play fullscreen
1 / 60

Turning Math into Pictures - PowerPoint PPT Presentation

  • Uploaded on

Turning Math into Pictures John Peterson Yale University http://haskell.org/edsl Welcome! You’ll need to know a few things for this session: Math: coordinates on a plane, functions, graphs Computers: using Windows and editing text files

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

Turning Math into Pictures

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
turning math into pictures

Turning Math into Pictures

John Peterson

Yale University



You’ll need to know a few things for this session:

  • Math: coordinates on a plane, functions, graphs
  • Computers: using Windows and editing text files

If you don’t have much experience with these things please pair up with a more experienced student!

making pictures
Making Pictures

We will make pictures by writing computer programs that turn basic mathematics into pictures. Our goal is for each student (or team) to make three pictures. We will place these on a web page for everyone to see when this session is over.

Please ask questions at any time!

using pan
Using Pan#

As I talk I will show you sample picture programs. I’d like you to run these yourself as I’m talking so you won’t fall asleep. And you can also change these programs a little bit to see what happens or fiddle with the controls if you’re bored.

Let’s see how to run and edit a program.

displaying a picture
Displaying a Picture

All you need to do is open (click on) a .pan file to view the picture it describes. Please look in the folder “pictures” on your desktop and click on the picture labeled “01-welcome”

Note that there are controls which govern the picture. Play with these a little.

Now save the picture on your disk with the “save as” selection on the File menu.

writing a program
Writing a Program

Now, let’s write a program to make a picture. Use the text editor and write the following:

picture (r @ a) =

if even(r/10) then white else green

Save this in the file test.pan

running a program
Running a Program

To run a program, open (double click) the test.pan file. What happens when you run this program?

If you make any mistakes entering the program you’ll get a message when you open it. You should go back to the editor to fix the problem, close the window with the message, and try again.


When I have an image on a slide or some code that has a file name under it you will find that file in the “pictures” directory. You can run the demo while I’m talking to see what happens


extending a language
Extending a Language

You already know two languages: English and Mathematics.

We want to have a language that includes Mathematics and pictures too.

Let’s think about how we extend (add new words to) English to see how we can extend Mathematics.

what s this mean
What’s this mean?

“Fred gave me his zax”

what s this mean11
What’s this mean?

“Fred gave me his zax”

I’m stumped too! So what should we do?

looking up a new word
Looking up a new word…

“Fred gave me his zax”

From a dictionary:

n. a tool similar to a hatchet, used for cutting and dressing roofing slates.

building language
Building Language

Aha! We can make a language bigger by giving definitions to new words. The new word is a name for some new concept.

Mathematicians use definitions to create new words (names) all the time.

These definitions express new words in terms of ones you already know.

computer languages
Computer Languages

A computer language is just like any other language except a computer knows what you’re saying.

Computers are very fussy! If you write down something that’s wrong (bad spelling, misplaced punctuation, total nonsense) it will complain.


One more thing: definitions often are parameterized. That is, they contain names that are “filled in” when the definition is used. Such a definition is a function.

Definition: the midpoint of a line between points A and B is a point on the line, C, such that the distance from A to C is the same as the distance from B to C.


The computer wants you to write down definitions in a very particular way:

name parameters = body

pi = 3.14159

increment x = x + 1

plusTwo x = increment (increment x)

midpoint a b = (a + b)/2

We don’t use parenthesis for function calling;

f x instead of f(x)

types and logic
Types and Logic

There are a lot of ways to “mess up” in a language:

Typing Mistakes: Fred ga?ve mehis z.ax

Illogical sentences:

“Have a zax day!”

Since zax is a noun this doesn’t make sense.

Mathematicians use logic to tell whether a sentence is gibberish or OK.

Types (like “noun”) describe a set of objects.

I’m sure the computer will occasionally complain about the syntax or types in your programs. Logic will help us understand things better.

about pan
About Pan#

A program is like a dictionary. It is a set of definitions, each starting in the leftmost column, in any order you want.

There is a built-in vocabulary that you use to build new definitions

A program must define the name “picture” so that when you run it the viewer knows what you want to see.

everything is a function
Everything is a Function

Functions can be used to represent all sorts of things.

We’re going to use functions to represent pictures.

Every function has a domain (sorts of things that “go in” to the function) and a range (sorts of things that “come out” of the function.

For pictures, our domain is (a piece of) the coordinate plane and our range is colors. That is, a picture is something that tells you “if you look here, you’ll see this color”


A coordinate is something that “points to” a place on the plane. A coordinate is like a house address - it tells you how to find a place.

Coordinate systems have an origin - a point in the center of the plane from which other points are located.

rectangular coordinates
Rectangular Coordinates

Rectangular coordinates are written as (x,y), where x is the projection of the point onto the x axis and y is the projection of the point onto the y axis.


P (4,2)




polar coordinates
Polar Coordinates

There is another way to express coordinates on the plane: polar coordinates. These use a distance, d, and a direction, angle, to locate a point from the origin.



We write this as (d @ angle) in our language. The angle is measured in radians - these are sort of like degrees except they go from 0 to 2p (or -p to p)

some questions
Some Questions

If you are standing at the origin of the coordinate system and someone told you to go to (x,y) how would you go there?

What if they told you to go to (d @ a)?

coordinates in pan
Coordinates in Pan#

The usual coordinate system in Pan# uses “pixels” (those little dots that make up your display) to measure distance. The origin is in the center of the display window.

You can see these two coordinate systems in action in your demos:





We describe colors using 3 numbers, each between 0 and 1

Amount of Red

Amount of Green

Amount of Blue

Example: Yellow = Red + Green rgb 1 1 0

Play with colors a minute using

05-showColor.pan to see what a color looks like

a picture
A Picture!

We can define a simple picture like this:

picture(d @ a) =

if d < 50 then black

else white

What will this picture look like?

The “if - then - else” allows you to choose one of two colors

an interactive picture
An Interactive Picture!

r <- slider “Select r” (0,100) 50 picture(d @ a) =

if d < r then black

else white

The “slider” allows you to see how the picture changes as “r” changes.



A region is a function that tells you whether a point is in the region or not.

So the circle in the last example is a region. You could write this as

circ (d @ a) = d < r

picture p =

if circ p then black else white

We’ll separate the program into regions and a “region painting” function.


A region is a function that tells you whether a point is in the region or not.

So the circle in the last example is a region. You could write this as

circ (d @ a) = d < r

picture p =

if circ p then black else white

We’ll separate the program into regions and a “region painting” function.

A region


A region is a function that tells you whether a point is in the region or not.

So the circle in the last example is a region. You could write this as

circ (d @ a) = d < r

picture p =

if circ p then black else white

We’ll separate the program into regions and a “region painting” function.

Painting the region


We want to draw stripes! Let’s control

  • Distance between stripes (pixels)
  • Width of the stripe (fraction)

We need a special function to do this:

floor(x) is the greatest integer less that x.

floor (2.34) = 2

floor (9.99) = 9

floor (-0.1) = ???


We compute the fractional part of x/width. Where is this 0?

frac x = x - floor x

stripes width size x = frac ((x + width/2)/width) < size

p <- slider "Distance between stripes" (0, 100) 20

w <- slider "Stripe width (fraction)" (0,1) 0.5

picture (x,y) =

if stripes p w x then black else white


combining regions
Combining Regions

We can make functions that glue regions together in different ways.

How would you make a function that forms the union of two regions?

region union
Region Union

A point “p” is in the union of r1 and r2 if it is in r1 or r2.

orRegion r1 r2 =

\p -> r1 p || r2 p

Don’t worry if this seems hard to follow!

We won’t have to write functions to do things with regions - they are already defined for you.

this is “or”

region looks here

region functions
Region Functions

Here are the built-in regions functions:

andRegion: points in both regions

orRegion: points in either region

xorRegion: points in just one region

rMinus: points in the first but not

the second region

invertRegion: points outside a region

combining regions36
Combining Regions

Let’s take the functions like “orRegion” out for a test drive …

vstripes (x,y) = stripes 20 0.3 x

hstripes (x,y) = stripes 20 0.3 y

circ (d @ a) = d < 70

r = orRegion vstripes hstripes

picture p = if r p then black else white


combining regions37
Combining Regions

Some more regions to try … you can remove the -- in front of these lines to test them.

r = andRegion vstripes hstripes

r = xorRegion vstripes hstripes

r = invertRegion circ

r = rMinus vstripes circ





polar stripes
Polar Stripes

Striping in polar coordinates is pretty cool! What do you think these regions look like?

r (d @ a) = stripes 20 0.3 d

r (d @ a) = stripes (2*pi/20) 0.5 a



fancy painting
Fancy Painting

There’s no need to just use black and white. Paint with any color you want. Paint more than one region. Use rgb to make exotic colors. Here’s an example:

r1 (x, y) = stripes 40 0.3 x

r2 (x, y) = stripes 40 0.3 y

r3 (d @ a) = stripes 25 0.7 d

r4 = orRegion r1 r2 -- the whole grid

r5 = andRegion r1 r2 -- the intersections

c1 = rgb 1 0.9 0 -- yellowish orange

picture p =

if r5 p then blue else

if r4 p then red else

if r3 p then c1 else white


moving regions
Moving Regions

We can move regions around! This function takes a region and shifts its center to a new point:

moveR (xc,yc) r =

\(x,y) -> r (x-xc, y-yc)

Read this as “point (x,y) is in the moved region if point (x-xc,y-yc) is in the original region.”

op art
Op Art

Here’s a program with interference patterns:

d <- slider “Distance” (0, 50) 20

circles (d @ a) = stripes 10 0.5 d

twoCircles = orRegion


(moveR (d, 0) circles)

picture p = if twoCircles p then black

else white


activity 1
Activity 1

Make a picture using regions. Here are some ideas:

  • Make some fancy regions by combining the simple ones we’ve seen in new ways.
  • Think of new ways to define regions. Can you make squares or diamonds or ovals?
  • What happens when you stripe something like (x+y) or (x*x)?
  • Use many different colors to paint your regions.
  • Use rgb to make fancy colors
  • Move some regions around to create interference patterns
  • Use sliders!

We’re going to try something with animations now! A fade turns one picture into another. You see a lot of these in TV and the movies. We’ll make fades that last exactly 1 second.

To make things easy the two pictures will always be in the square between -1 and 1.

region based fading
Region Based Fading

We can simplify the problem by using a region to control the fade. This region should not touch the square at t=0 and cover the entire square at t=1.

r t (x,y) = x < 2*t-1

picture <- fadeRegion r


Evaluate the region at t = 0 and t = 1 to make sure

things work right!


Let’s reuse our stripes function to

make a fade:

r t (x,y) = stripes 0.1 t x

picture <- fadeRegion r


Check the region at t=0 and t=1 to make sure!


Fading is a form of interpolation. In general, interpolation takes two objects and a number between 0 and 1 and returns an object between the two endpoints.

We can use the following built-in interpolations:

interpolateC t color1 color2

interpolateN t number1 number2

interpolateP t point1 point2

What is the formula for interpolateN?

That first example could be rewritten as

r t (x,y) = x < interpolateN t (-1) 1

color interpolation
Color Interpolation

We can interpolate colors directly. This should be very familiar!

fader im1 im2 t p =

interpolateC t (im1 p) (im2 p)

picture <- fadeImage fader


another fader
Another Fader

Here’s a “head scratcher”! Can you figure it out?

fader im1 im2 t (x,y) =

if abs x > t || abs y > t then

im1 (x,y) else

im2 (x/t, y/t)


Note: abs x is the distance to the origin

|| is “or”

activity 2
Activity 2

Write your own fader. Some ideas:

Use an interesting region based on polar coordinates

Use two different kinds of interpolation

Use point interpolation to move a region

Use color interpolation to soften the

edge of a region

Intersect regions that are formed by lines.

Change the rate of time flow

funny faces
Funny Faces

We can use functions to alter a picture from your disk. This “outside” picture is a function just like the ones that you display. This program just displays the selected picture:

thePicture <- image “Select a picture”

picture = thePicture


moving the picture
Moving the Picture

We’ll want to move the picture around a little to see what happens as it moves. If you use “getPicture” the image comes with a bunch of controls:

thePicture <- getPicture “Select a picture”

picture = thePicture


changing the picture
Changing the Picture

Now that we have a picture, we can change it if we want. Here’s a really simple way to change a picture: add a mirror:

thePicture <- getPicture “Select a picture”

picture (x,y) = if x > 0 then thePicture (x,y)

else thePicture (-x, y)



We can use any transformation that scrambles the points in a picture! In general, these transformations give you a new place to look in the picture at each point.

Here are some transfomers:

moveIt (x,y) = (x+1, y+1)

What is moveIt (2,3)?

twistIt (x,y) = (y, -x)

What is twistIt (2,3)

visualizing transformations
Visualizing Transformations

What we’re doing is using a transformation as a lens which distorts an image.

Here’s a transformation:

f (x,y) = (x*x, y*y)

f (0,0) = (0,0)

f (1,1) = f(-1,1) = f(1,-1) = f (-1,-1) = (1,1)

f (2,2) = (4,4)

So, if we look at (2,2) we’ll see what’s at (4,4)

domains and ranges
Domains and Ranges

The domain of a function is the inputs over which it is defined. The range is the set of possible outputs.

A lens makes the domain and range visible.

The domain of the lens function should be the entire plane - otherwise there are places we can’t look!

The range tells you what part of the incoming image is visible. What is the range of the mirror lens?

a radial transformation
A Radial Transformation

Lets look at transformations that alter only the distance from the origin. That is, something like

lens (d @ a) = (f(d) @ a)

for some function f. Suppose f(d) = d*d - what will happen?

This is called a


hi big nose
Hi, Big Nose!

What’s the relationship between the parabola and the way the picture is changed?

The “100” keeps the picture from getting too small.

thePicture <- getPicture “Select a picture”

picture (d @ a) = thePicture (d*d / 100 @ a)


Think about the slope of the parabola!


This transformation adds extra rotation as points

move out from the origin

f (d @ a) = (d @ a + k * d)

We can adjust k with a slider and watch how the

picture changes.


altering the distance
Altering the Distance

We’ll use a function that moves things in or out depending on the angle. We’ll use a point that is rotated a multiple of the current angle to decide how much to change the distance.

picture (d @ a) = thePicture ( bumps d a @ a )

bumps d a = d * bumper (1 @ a*n)

bumper (x,y) = 1 + k * x


This one takes some deep thinking! Look at what

happens when k is 0.

activity 3
Activity 3
  • Make a “funny face” using some of the picture warping functions we have explained.
  • Make something humorous and fun - not disgusting! You shouldn’t do anything that you wouldn’t want someone else to do to a picture of you.
  • Here are some ideas:
  • Try more than one mirror
  • Change the function used in radial distortion
  • Combine two or three different distortions
  • Use “if then else” in your functions
  • Try square roots (sqrt)