Mini F GrabDot (Large)
Setup
The Rect data type contains:
- pt1 and pt2, both of type Point
A Rect represents a rectangle by two opposite corner points (e.g.,
upper left and lower right).
The Might data type is either DidNot or Did Point.
A GrabDot game record contains the following:
- a list of Point called dots
- a list of Rect called rects
- an Int called score
- a Might called lastPt
- a Might called currentPt
Exercises
-
Write a data defintion for:
RectMightGrabDot
-
Write the following functions:
width :: Rect -> Double height :: Rect -> Double center :: Rect -> Double -
Write a function
drawr :: Rect -> Picturethat produces aPicturerepresenting the given rectangle. -
Write a function upperleft that takes in a
Rectand returns the upper left corner using the standard math coordinate system.upperleft :: Rect -> Point upperleft r = (0,0) check_upperleft = [ upperleft (Rect (-6,3) (-2,4)) == (-6,3), upperleft (Rect (9,1) (4,8)) == (4,8), upperleft (Rect (9,8) (4,1)) == (4,8), upperleft (Rect (-6,-7) (-4,3)) == (-6,3) ] -
Write a similar function
lowerright. -
Write an example
ex1of theGrabDotrecord with-
a
lastPtofDid (2,3) -
a
currentPtofDid (4,1) -
a
scoreof 8 -
a
rectslist of[Rect (-6,3) (-2,-4), Rect (9,1) (4,8)] -
a
dotslist of[(5,3),(7,6),(-4.5,2),(-4.5,2),(-4.5,1),(-4.5,0), (-4.5,-1),(-4,5,-2)]
-
-
Write an example
ex2of theGrabDotrecord that is likeex1except thelastPtisDidNot. -
Write a function
scoreto determine how many dots are contained in a given rectangle. On the boundary counts as in.score :: [Point] -> Rect -> Int -
Write a function
render1that draws small solid blue circles with each of thedotsas centers.render1 :: GrabDot -> PictureTest your code with a
drawingOforanimationOf. -
Write a function
render2that draws light pink rectangles for each of the elements in therectslist. (You might need to write pink as(RGB 0.99 0.60 0.80).)render2 :: GrabDot -> Picture -
Write a function
render3that draws a rectangle with cornerslastPtandcurrentPtin a transparent version of Rosewood (#9e4244, also known as(RGBA 0.58 0.26 0.27 xxx); adjust xxx so it looks good). If thelastPtisDidNot, do not draw the rectangle. -
The total score of a game is computed by finding the total of the number of points enclosed by each one of the rectangles. It is OK to count points twice. Write a function
render4that puts the total score somewhere in the upper right corner of the standardPicture. -
Make a composite function that combines the render functions 1, 2, 3, and 4.
-
Write an event handler so that:
PointerPress (x,y)puts the current mouse position into thelastPtfield.PointerMovement (x,y)puts the current mouse position into thecurrentPtfield.PointerRelease (x,y)adds aRectto the start of therectslist with cornerslastPtand(x,y). It also setslastPttoDidNot.