Lecture 2
A Simple 2D Graphics Language
The week ahead…
Reminders / Announcements
A typical week…
Monday - Attend lecture, take a look at tutorial, and homework
Tuesday - Watch the pre-recorded lecture for Wednesday; peruse the tutorial
Wednesday - Come to tutorial session, try your best to iron out all of your questions; see if you can do any of the HW
Thursday - by now, you should have a strong sense of how long it will take you to complete HW Exercise; if you need more time, apply for late penalty waiver
Friday - come to class, participate in activities; finish up HW
Sat/Sun - take the weekend off if possible
Resources available throughout the week: Canvas, Class, Office Hours, and edSTEM
Our Goal for Today
Function calls
function input1 … inputn
(function input1 … inputn)
Examples:
Note: + and string-append can allow variable numbers of inputs in Racket (i.e. they are variadic)
What’s the text for this DFD?
Chained (nested) calls (Composing Functions)
(function input1 … inputn)
Examples
"this "� (string-append "is a "
"test"))
"blue")
(square 10� "solid"
"red"))
Subexpressions and data flow
Subexpressions and data flow
(string-append (string-append (string-append "this " "is a ") "tedious ") (string-append "example of " "chaining"))
Running code�in DrRacket
Using DrRacket
Our Style Rules for this Class
What to do when it breaks
Making it legible
(string-append (string-append (string-append "this " "is a " "tedious ") (string-append "example of " "chaining")))
Making it legible
(string-append (string-append (string-append "this " "is a " "tedious ") (string-append "example of " "chaining")))
Making it legible
(string-append (string-append (string-append "this "
"is a "
"tedious ")
(string-append "example of "
"chaining")))
Making it legible
(string-append (string-append (string-append "this "
"is a ")
"tedious ")
(string-append "example of "
"chaining"))
(string-append (string-append (string-append "this "
"is a ")
"tedious ")
(string-append "example of "
"chaining"))
Indented code is a DFD (just written right to left)
Making it mutually intelligible
Keeping your code indented
2D Graphics
One extra bit of magic…
#lang 2htdp/isl+
(require 2htdp/image)
Rectangles and Squares
(rectangle width height� mode color)
(square width mode color)
Creates a rectangle where…
Circles and Ellipses
(ellipse width height� mode color)
(circle radius mode color)
Creates curved objects where…
Compositing images
(overlay image image …)
(underlay image image …)
Make a compound image from multiple image objects where…
Official Documentation Link (overlay)
Official Documentation Link (underlay)
Placing images next to one another
(above images …)
(beside images …)
Both make an image by laying out the specified images vertically (above) or horizontally (beside).
Shifting images relative to one another
(overlay/offset top-image
right down
bottom-image)
Composites the two images according to some rules:
overlay/xy works similarly
Rotate
(rotate angle image)
Rotates image counter-clockwise angle degrees about its center
Wedges
(wedge radius angle mode color)
Creates a wedge of a circle with…
Scaling
(scale scale-factor image)
Scale an inputted image by some scale-factor
Ex. 10 means 10 times as large
A note on Colors
Color objects
(make-color r g b)
(make-color r g b a)
(color r g b)
Alternatively, you can also specify a color using a color object
Using define to "remember" data
(define a-green-square (square 10 "solid" "green"))
Debugging Example (if time)
Dataflow debugging example
(overlay (circle 30 "solid" "green" square 100 "solid" "orange")))
Checking the data flow
Dataflow debugging example
circle: expects only 3 arguments, but found 7
(overlay (circle 30 "solid" "green" square 100 "solid" "orange")))
Dataflow debugging example
(overlay (circle 30 "solid" "green" square 100 "solid" "orange"))
(overlay (circle 30� "solid"� "green"
square� 100
"solid" � "orange")))
Dataflow debugging example
(overlay (circle 30� "solid"� "green"
(square 100
"solid" � "orange")))
Dataflow debugging example
(overlay (circle 30� "solid"� "green"
(square 100
"solid" � "orange")))
Dataflow debugging example
(overlay (circle 30� "solid"� "green"))
(overlay (circle 30� "solid"� "green")
(square 100
"solid" � "orange"))