Mini F GrabDot (Large)

Count the dots as they are covered by rectangles. This is too long to be a mini project.

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

  1. Write a data defintion for:

    1. Rect
    2. Might
    3. GrabDot
  2. Write the following functions:

     width :: Rect -> Double
     height :: Rect -> Double
     center :: Rect -> Double
    
  3. Write a function drawr :: Rect -> Picture that produces a Picture representing the given rectangle.

  4. Write a function upperleft that takes in a Rect and 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)
       ]
    
  5. Write a similar function lowerright.

  6. Write an example ex1 of the GrabDot record with

    • a lastPt of Did (2,3)

    • a currentPt of Did (4,1)

    • a score of 8

    • a rects list of

        [Rect (-6,3) (-2,-4), Rect (9,1) (4,8)]
      
    • a dots list of

        [(5,3),(7,6),(-4.5,2),(-4.5,2),(-4.5,1),(-4.5,0), (-4.5,-1),(-4,5,-2)]
      
  7. Write an example ex2 of the GrabDot record that is like ex1 except the lastPt is DidNot.

  8. Write a function score to determine how many dots are contained in a given rectangle. On the boundary counts as in.

     score :: [Point] -> Rect -> Int
    
  9. Write a function render1 that draws small solid blue circles with each of the dots as centers.

     render1 :: GrabDot -> Picture
    

    Test your code with a drawingOf or animationOf.

  10. Write a function render2 that draws light pink rectangles for each of the elements in the rects list. (You might need to write pink as (RGB 0.99 0.60 0.80).)

     render2 :: GrabDot -> Picture
    
  11. Write a function render3 that draws a rectangle with corners lastPt and currentPt in a transparent version of Rosewood (#9e4244, also known as (RGBA 0.58 0.26 0.27 xxx); adjust xxx so it looks good). If the lastPt is DidNot, do not draw the rectangle.

  12. 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 render4 that puts the total score somewhere in the upper right corner of the standard Picture.

  13. Make a composite function that combines the render functions 1, 2, 3, and 4.

  14. Write an event handler so that:

    • PointerPress (x,y) puts the current mouse position into the lastPt field.
    • PointerMovement (x,y) puts the current mouse position into the currentPt field.
    • PointerRelease (x,y) adds a Rect to the start of the rects list with corners lastPt and (x,y). It also sets lastPt to DidNot.
Last modified October 31, 2025: Haskell quizzes including CodeWorld. (c5c8b02)