☰ Contents Data That Travels Together

Records and Images

What is Pict?

Student: OK so we saw that we can create Picts using stuff like new Circle and new Rect, right? And we saw that new could be used with the records we designed. Does that mean those things are records?
Prof: Yes, more or less, that’s a good observation! That’s why you see the fields in the record printing. The book just has a little extra special step where it shows the image, too. I wrote record Circle(...) { ... } in a place that gets loaded for all the book programs.
Prof: Actually, this is a good time to do a little more with images. There are a few more constructors for images I’d like to show you.
Student: OK!
Prof: First let’s show the Above constructor, which puts two images on top of one another:
Student: Oh, that’s cool. So images can be combined!
Prof: Right! And notice that the record for the Above also prints out, which shows that it is made of those two other images. This shows a general idea called composition: we take two images and combine, or compose, them together into a new image. There are lots of ways to compose two images together beyond Above, what might some be?
Student: Well, I bet there’s Below if there’s Above. Maybe they can be beside each other, too?
Prof: Anything else?
Student: Hmm… Maybe something to, like, put one image in front of the other?

Prof: Nice ideas. Here are a few constructor signatures:

  • Below(Pict bottom, Pict top)
  • Beside(Pict left, Pict right)
  • Overlay(Pict front, Pict back)
Student: OK! How do they work?
Prof: Try working with the trunk and top definitions I gave you to learn what each does.
Student: OK…
Student: OK, I get it. Below is just like above flipped around. Beside puts them next to each other. And Overlay puts one on in front of the other.
Prof: That’s right. We can make some pretty cool pictures just by composing these operations together. It will teach us a lot about constructing interesting compound values.

Practice

Make the flag of Bangladesh: a red circle in front of a green rectangle. Name the finished picture flag, and make the circle small enough to fit inside the rectangle.