Images

Let's start with some simple shapes, programming in the worksheet as we've done before.

Image.circle(100)
// res0: Image = Circle(d = 100.0)

What is happening here? Image is an object and circle a method on that object. We pass to circle a parameter, 100 that gives the diameter of the circle we're constructing. Note the type of the result is Image.

We draw the circle by calling the draw method.

Image.circle(100).draw()

A window should appear showing the picture below.

A circle

Doodle supports a handful of "primitive" images: circles, rectangles, and triangles. Let's try drawing a rectangle.

Image.rectangle(100, 50).draw()

The output is shown below.

A rectangle

Finally let's try a triangle, which we specify in terms of a width and a height. You should see the below image.

Image.triangle(120, 80).draw()

A triangle

Exercises

Exercise: I Go Round in Circles

Create circles that are 1, 10, and 100 units wide. Now draw them!

In this exercise we're checking that our Doodle install is working correctly and we're getting used to using the library. One of the important points in Doodle is we separate defining the image from drawing the image. We'll talk more about this throughout the book.

We can create circles with the code below.

Image.circle(1)
Image.circle(10)
Image.circle(100)

We can draw the circles by calling the draw method on each circle.

Image.circle(1).draw()
Image.circle(10).draw()
Image.circle(100).draw()

Exercise: My Type of Art

What is the type of a circle? A rectangle? A triangle?

They all have type Image, as we can tell from the worksheet.

Image.circle(10) // doodle.core.Image
Image.rectangle(10, 10) // doodle.core.Image
Image.triangle(10, 10) // doodle.core.Image

Exercise: Not My Type of Art

What's the type of drawing an image? What does this mean?

In this case the worksheet doesn't print any type.

Image.circle(10).draw()

However, we can mouse over the expression, or use the trick of giving the expression an incorrect type, to find that the type is Unit. Unit is the type of expressions that have no interesting value to return. This is the case for draw; we call it because we want something to appear on the screen, not because we have a use for the value it returns. There is only one value with type Unit. This value is also called unit, which written as a literal expression is ()