“Bootcamp” Lesson 2 - Northeastern University...• Note: this lesson is mostly review from...

28
Working with images and scenes CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.5 1 © Mitchell Wand, 2012-2014 This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.

Transcript of “Bootcamp” Lesson 2 - Northeastern University...• Note: this lesson is mostly review from...

Page 1: “Bootcamp” Lesson 2 - Northeastern University...• Note: this lesson is mostly review from Lesson 0.4 2. Learning Objectives • At the end of this lesson, the student should

Workingwithimagesandscenes

CS5010ProgramDesignParadigms“Bootcamp”Lesson2.5

1©MitchellWand,2012-2014ThisworkislicensedunderaCreative Commons Attribution-NonCommercial 4.0 International License.

Page 2: “Bootcamp” Lesson 2 - Northeastern University...• Note: this lesson is mostly review from Lesson 0.4 2. Learning Objectives • At the end of this lesson, the student should

Introduction

• Rackethasarichlibraryforworkingwithimagesandscenes.

• Wewillusethislibraryextensivelyinthiscourse.

• Inthislesson,wewillexploreafewthingsfromthelibrary.

• Note:thislessonismostlyreviewfromLesson0.4

2

Page 3: “Bootcamp” Lesson 2 - Northeastern University...• Note: this lesson is mostly review from Lesson 0.4 2. Learning Objectives • At the end of this lesson, the student should

LearningObjectives

• Attheendofthislesson,thestudentshouldbeableto:– Createsimpleimagesandscenes– Combinesimpleimagesintomorecompleximagesandscenes

– Determinethepropertiesofanimage– Testimagesandtheirproperties

3

Page 4: “Bootcamp” Lesson 2 - Northeastern University...• Note: this lesson is mostly review from Lesson 0.4 2. Learning Objectives • At the end of this lesson, the student should

ImagesareScalarData

• InRacket,imagesarescalardata• Rackethas:– Functionsforcreatingimages– Functionsforcombiningimages– Functionsforfindingpropertiesofimages

• Ingeneral,webuildcompleximagesbystartingwithsimpleimagesandthencombiningthemusingfunctions.

4

Page 5: “Bootcamp” Lesson 2 - Northeastern University...• Note: this lesson is mostly review from Lesson 0.4 2. Learning Objectives • At the end of this lesson, the student should

Loadingtheimagelibrary

Toloadtheimagelibrary,includetheline(require 2htdp/image)

inyourprogram.

5

Page 6: “Bootcamp” Lesson 2 - Northeastern University...• Note: this lesson is mostly review from Lesson 0.4 2. Learning Objectives • At the end of this lesson, the student should

FunctionsforCreatingImages(1)

bitmap : String -> ImageGIVEN:thenameofafilecontaininganimagein.png or.jpgformatRETURNS:thesameimageasaRacketvalue.

6

Page 7: “Bootcamp” Lesson 2 - Northeastern University...• Note: this lesson is mostly review from Lesson 0.4 2. Learning Objectives • At the end of this lesson, the student should

FunctionsforCreatingImages(2)

rectangle : Width Height Mode Color -> Image

GIVEN:awidthandheight(inpixels),amode(eitherthestring“solid”orthestring“outline”),andacolorRETURNS:animageofthatrectangle.[SeetheHelpDeskforinformationontherepresentationofcolorsinRacket].

7

Page 8: “Bootcamp” Lesson 2 - Northeastern University...• Note: this lesson is mostly review from Lesson 0.4 2. Learning Objectives • At the end of this lesson, the student should

FunctionsforCreatingImages(3)

circle : Radius Mode Color -> ImageLikerectangle,buttakesaradiusinsteadofawidthandheight.

Therearelotsofotherfunctionsforcreatingshapes,likeellipse,triangle,star,etc.

8

Page 9: “Bootcamp” Lesson 2 - Northeastern University...• Note: this lesson is mostly review from Lesson 0.4 2. Learning Objectives • At the end of this lesson, the student should

FunctionsforCreatingImages(4)

text : String Fontsize Color -> Image

RETURNS:animageofthegiventextatthegivenfontsizeandcolor.

9

Page 10: “Bootcamp” Lesson 2 - Northeastern University...• Note: this lesson is mostly review from Lesson 0.4 2. Learning Objectives • At the end of this lesson, the student should

CombiningImages

• Theimagelibrarycontainsmanyfunctionsforcombiningimagesintolargerimages.

• Thesefunctionsgenerallyaligntheimagesontheircenters.Thisisusuallywhatyouwant.Ifyoureallywanttoalignimagesontheiredges,therearefunctionsinthelibrarytodothat,too.Seethehelpdesk,asusual.

• Let’slookatthetwomostcommonly-usedimagecombining-functions:beside andabove.Here’sanexample:

10

Page 11: “Bootcamp” Lesson 2 - Northeastern University...• Note: this lesson is mostly review from Lesson 0.4 2. Learning Objectives • At the end of this lesson, the student should

besideandabove

11

Page 12: “Bootcamp” Lesson 2 - Northeastern University...• Note: this lesson is mostly review from Lesson 0.4 2. Learning Objectives • At the end of this lesson, the student should

Slightlymorecomplicatedimages

12

Page 13: “Bootcamp” Lesson 2 - Northeastern University...• Note: this lesson is mostly review from Lesson 0.4 2. Learning Objectives • At the end of this lesson, the student should

Slightlymorecomplicatedimages

13

Therectanglehaswidth0,soit'sinvisibleJ

Page 14: “Bootcamp” Lesson 2 - Northeastern University...• Note: this lesson is mostly review from Lesson 0.4 2. Learning Objectives • At the end of this lesson, the student should

Scenes

• Ascene isanimagethathasacoordinatesystem.

• Inascene,theorigin(0,0)isinthetopleftcorner.Thex-coordinateincreasesaswemovetotheright.They-coordinateincreasesaswemovedown.Thesearesometimescalled“computer-graphicscoordinates”

• Weuseascenewhenweneedtocombineimagesbyplacingthematspecificlocations.

14

Page 15: “Bootcamp” Lesson 2 - Northeastern University...• Note: this lesson is mostly review from Lesson 0.4 2. Learning Objectives • At the end of this lesson, the student should

SceneCoordinates

15

(0,0) x

y

Page 16: “Bootcamp” Lesson 2 - Northeastern University...• Note: this lesson is mostly review from Lesson 0.4 2. Learning Objectives • At the end of this lesson, the student should

CreatingScenes

• (empty-scene width height)– returnsanemptyscenewiththegivendimensions.

• (place-image img x y s)– returnsascenejustlikes,exceptthatimageimgisplacedwithitscenteratposition(x,y) ins

– resultingimageiscroppedtothedimensionsofs.

16

Page 17: “Bootcamp” Lesson 2 - Northeastern University...• Note: this lesson is mostly review from Lesson 0.4 2. Learning Objectives • At the end of this lesson, the student should

scene+line

• (scene+line s x1 y1 x2 y2 color)– returnsascenejustliketheoriginals,butwithalinedrawnfrom(x1,y1) to(x2,y2) inthegivencolor.

– theresultingsceneiscroppedtothedimensionsofs.

17

Page 18: “Bootcamp” Lesson 2 - Northeastern University...• Note: this lesson is mostly review from Lesson 0.4 2. Learning Objectives • At the end of this lesson, the student should

CreatingSceneswithFunctionsComposition

• Createsceneswithimagesinthembycombiningthemwithfunctions.

• Startwithanempty-scene,thenpaintimagesandlinesonthescenebyusingplace-imageandscene+line.

• Thisisallfunctional:paintinganimageonascenedoesn’tchangethescene– itproducesanewscene.

18

Page 19: “Bootcamp” Lesson 2 - Northeastern University...• Note: this lesson is mostly review from Lesson 0.4 2. Learning Objectives • At the end of this lesson, the student should

VideoDemonstration

19YouTubelink

Page 20: “Bootcamp” Lesson 2 - Northeastern University...• Note: this lesson is mostly review from Lesson 0.4 2. Learning Objectives • At the end of this lesson, the student should

MeasuringImages

• Racketalsoprovidesfunctionsfordeterminingimageproperties.Herethemostimportantones:– image-width : Image -> NonNegInt– image-height : Image -> NonNegInt– image? : Any -> Boolean

20

Inpixels

Page 21: “Bootcamp” Lesson 2 - Northeastern University...• Note: this lesson is mostly review from Lesson 0.4 2. Learning Objectives • At the end of this lesson, the student should

BoundingBox

• Theboundingbox ofanimageisthesmallestrectanglethatcompletelyenclosestheimage.

• Itswidthwillbetheimage-widthoftheimage,anditsheightwillbetheimage-heightoftheimage.

• Itiseasytodeterminewhetheranarbitrarypointisinsidetheboundingbox– let’slookatanexample.

21

Page 22: “Bootcamp” Lesson 2 - Northeastern University...• Note: this lesson is mostly review from Lesson 0.4 2. Learning Objectives • At the end of this lesson, the student should

BoundingBoxExample

22

w = (image-width CAT-IMAGE)

h =(image-height CAT-IMAGE)

(x0,y0)

(x,y)isinside therectangleiff(x0-w/2)≤x≤(x0+w/2)

and(y0-h/2)≤y≤(y0+h/2)

y=y0-h/2

y=y0+h/2

x=x0-w/2 x=x0+w/2

Page 23: “Bootcamp” Lesson 2 - Northeastern University...• Note: this lesson is mostly review from Lesson 0.4 2. Learning Objectives • At the end of this lesson, the student should

ImagesandtheDesignRecipe:Examples

• Inyourexamples,describetheimageinwords.

• EXAMPLE:Considerafunctionthattakesanimageanddoublesit.

• Inyourexamplesyoucouldwrite:(define red-circle1 (circle 20 "solid" "red"))

;; (double-image red-circle1);; = two red circles, side-by-side

23

Page 24: “Bootcamp” Lesson 2 - Northeastern University...• Note: this lesson is mostly review from Lesson 0.4 2. Learning Objectives • At the end of this lesson, the student should

ImagesandtheDesignRecipe:Tests(1)

• First,constructthecorrectimage.DoNOTusethefunctionyouaretestingtoconstructtheimage.

• EXAMPLE:(define two-red-circles(beside red-circle1 red-circle1))

• Checkitvisuallytoseethatit'scorrect– Alas,thisstepisnotautomatable.

24

Page 25: “Bootcamp” Lesson 2 - Northeastern University...• Note: this lesson is mostly review from Lesson 0.4 2. Learning Objectives • At the end of this lesson, the student should

ImagesandtheDesignRecipe:Tests(2)

• Thenyoucanusecheck-equal? ontheresultingimages:(check-equal?

(double-image red-circle1)two-red-circles)

• Thislooksalittlesillynow,butitwillbehelpfulwhenyoubuildmorecomplicatedimages.

25

Page 26: “Bootcamp” Lesson 2 - Northeastern University...• Note: this lesson is mostly review from Lesson 0.4 2. Learning Objectives • At the end of this lesson, the student should

ImagesandtheDesignRecipe:Tests(3)

• check-equal? isfairlyclever,butnotperfect.• Whichoftheimagesbelowarevisuallyequal?• Seewhichofthemcheck-equal? acceptsasequal.

(define vspace1 (rectangle 0 50 "solid" "black"))(define vspace2 (rectangle 0 50 "solid" "white"))(define vspace3 (rectangle 0 50 "solid" "red"))(define vspace4 (rectangle 0 50 "outline" "black"))(define vspace5 (rectangle 0 50 "outline" "white"))

26

Page 27: “Bootcamp” Lesson 2 - Northeastern University...• Note: this lesson is mostly review from Lesson 0.4 2. Learning Objectives • At the end of this lesson, the student should

Summary

• Imagesareordinaryscalarvalues• Createandcombinethemusingfunctions• Scenesareakindofimage– createwithempty-scene– buildwithplace-image

• 2htdp/image haslotsoffunctionsfordoingallthis.– Golookatthehelpdocs

27

Page 28: “Bootcamp” Lesson 2 - Northeastern University...• Note: this lesson is mostly review from Lesson 0.4 2. Learning Objectives • At the end of this lesson, the student should

NextSteps

• Ifyouhavequestionsorcommentsaboutthislesson,postthemonthediscussionboard.

28