# Expressive Expressions
We're now going to look at creating images. The code library we use to create images is not built-in to Scala. You will need to add the following to the worksheet to be able to use it.
```scala mdoc:silent
import cats.effect.unsafe.implicits.global
import doodle.core._
import doodle.image._
import doodle.syntax.all._
import doodle.image.syntax.all._
import doodle.java2d._
```
Let's start with some simple shapes, programming in the worksheet as we've done before.
```scala mdoc
Image.circle(10)
```
What is happening here? `Image` is an object and `circle` a method on that object. We pass to `circle` a parameter, `10` that gives the diameter of the circle we're constructing. Note the type of the result: an `Image`.
```scala mdoc
Image.circle(10)
```
We draw the circle by calling the `draw` method.
```scala
Image.circle(10).draw()
```
A window should appear as shown in @:fref(pictures:circle).
@:figure{ img = "circle.svg", key = "#fig:pictures:circle", caption = "A circle" }
Doodle supports a handful of "primitive" images: circles, rectangles, and triangles. Let's try drawing a rectangle.
```scala
Image.rectangle(100, 50).draw()
```
The output is shown in @:fref(pictures:rectangle).
@:figure{ img = "rectangle.svg", key = "#fig:pictures:rectangle", caption = "A rectangle" }
Finally let's try a triangle, for which the output is shown in @:fref(pictures:triangle).
```scala
Image.triangle(60, 40).draw()
```
@:figure{ img = "triangle.svg", key = "#fig:pictures:triangle", caption = "A triangle" }
### Exercises
#### I Go Round in Circles
Create circles that are 1, 10, and 100 units wide. Now draw them!
@:solution
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.
```scala mdoc:silent
Image.circle(1)
Image.circle(10)
Image.circle(100)
```
We can draw the circles by calling the `draw` method on each circle.
```scala
Image.circle(1).draw()
Image.circle(10).draw()
Image.circle(100).draw()
```
@:@
#### My Type of Art
What is the type of a circle? A rectangle? A triangle?
@:solution
They all have type `Image`, as we can tell from the console.
```scala
:type Image.circle(10)
// doodle.core.Image
:type Image.rectangle(10, 10)
// doodle.core.Image
:type Image.triangle(10, 10)
// doodle.core.Image
```
@:@
#### Not My Type of Art
What's the type of *drawing* an image? What does this mean?
@:solution
Once again, we can ask the console this quesstion.
```scala
:type Image.circle(10).draw()
// Unit
```
We see that the type of drawing an image 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 `()`
You'll note that the console doesn't print unit by default.
```scala
()
```
We can ask the console for the type to show that there really is unit here.
```scala
:type ()
// Unit
```
@:@
没有合适的资源?快使用搜索试试~ 我知道了~
Scala编程的快速、图形化、有趣的介绍。___下载.zip
共358个文件
md:112个
pdf:78个
svg:76个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 149 浏览量
2023-04-18
00:19:51
上传
评论
收藏 6.29MB ZIP 举报
温馨提示
Scala编程的快速、图形化、有趣的介绍。___下载.zip
资源推荐
资源详情
资源评论
收起资源包目录
Scala编程的快速、图形化、有趣的介绍。___下载.zip (358个子文件)
gumroad-cover.ai 1.07MB
epub-cover.ai 1.06MB
directory.conf 196B
directory.conf 173B
directory.conf 141B
directory.conf 118B
directory.conf 118B
directory.conf 114B
directory.conf 104B
directory.conf 92B
directory.conf 90B
directory.conf 87B
directory.conf 84B
.scalafmt.conf 42B
creative-scala.css 3KB
.gitignore 203B
default.template.html 1KB
cover-notes.html 0B
aubergines.jpg 168KB
solution.js 574B
tailwind.config.js 387B
package.json 326B
book.less 22B
plan.md 18KB
hof.md 15KB
api.md 14KB
lists.md 14KB
parametric-curves.md 13KB
natural-numbers.md 12KB
color-palette.md 12KB
flatmap.md 11KB
names.md 11KB
map.md 10KB
paths.md 10KB
functions.md 9KB
product.md 9KB
branching.md 9KB
04-creating-color.md 8KB
index.md 8KB
transform.md 8KB
exercises.md 8KB
foreword-1.md 8KB
functions.md 8KB
index.md 7KB
04-creative.md 7KB
recursion.md 7KB
02-polar.md 7KB
nested-methods.md 6KB
exercises.md 6KB
turtle.md 6KB
paths.md 6KB
substitution.md 6KB
turtles.md 6KB
auxillary-parameter.md 6KB
abstraction.md 6KB
substitution.md 6KB
background.md 6KB
order.md 6KB
random.md 5KB
stars.md 5KB
boxes.md 5KB
adt.md 5KB
05-exercises.md 5KB
writing-methods.md 5KB
terminal.md 4KB
fractals.md 4KB
03-color.md 4KB
match.md 4KB
syntax.md 4KB
fun.md 4KB
flowers.md 4KB
outside-console.md 3KB
local-reasoning.md 3KB
README.md 3KB
04-images.md 3KB
for.md 3KB
02-layout.md 3KB
generative.md 3KB
reactor.md 3KB
imports.md 3KB
console.md 3KB
method-declarations.md 3KB
04-types.md 3KB
take-home-points.md 2KB
methods.md 2KB
02-literals.md 2KB
03-path.md 2KB
semantics.md 2KB
case-classes.md 2KB
value-declarations.md 2KB
index.md 2KB
github.md 2KB
03-objects.md 2KB
exercises.md 2KB
03-expressions.md 2KB
create.md 2KB
exercises.md 2KB
04-types.md 2KB
README.md 2KB
exercises.md 2KB
共 358 条
- 1
- 2
- 3
- 4
资源评论
快撑死的鱼
- 粉丝: 1w+
- 资源: 9154
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- FPGA读写 AD9708+ AD9280 ADDA实验Verilog逻辑源码Quartus工程文件+文档说明+硬件参考原理图
- 基于CH340C设计USB转TLL串口通信模块PADS 9.5设计硬件(原理图+PCB)文件.zip
- 信号与系统大作业-图像处理 实现了直接滤波法维纳滤波法最小二乘滤波法LR递归法matlab源码.zip
- php-leetcode题解之最小栈.zip
- php-leetcode题解之最长不含重复字符的子字符串.zip
- php-leetcode题解之最长公共前缀.zip
- php-leetcode题解之最小差值.zip
- php-leetcode题解之最小基因变化.zip
- php-leetcode题解之最小路径和.zip
- php-leetcode题解之最大正方形.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功