-- The given program implements an animation of a circle that grows
-- over time. Modify it so that the circle switches to shrinking after
-- a certain amount of time and eventually stops changing at all,
-- keeping its size constant from then on.
-- The exact sizes the balloon reaches and the rates at which it grows
-- and shrinks are up to you, just try to not let it grow bigger than
-- the screen.
-- Additionally, give the circle a different color during each stage
-- (one color while growing, one color while shrinking, one color in
-- the stable state).
/tmp/Template-810ce2841c78b2ec/Task09.hs:28:1: error: [GHC-62161] [-Wincomplete-patterns (in -Wextra), Werror=incomplete-patterns]
Pattern match(es) are non-exhaustive
In an equation for ‘balloon’:
Patterns of type ‘Double’ not matched: _
Assistant
You have received an error related to non-exhaustive pattern matches in your balloon function. Haskell's compiler warns about this because there could potentially be Double values for t that don't fit any of your specified cases. To address this issue and to refine the behavior of the animation, let's explicitly handle all possible cases of and ensure that they match smoothly as per the behaviors described.
To help you build the partial circle animation and wrap them into a loading animation, I'll provide an implementation for both the partialCircle and functions.
t
Let's improve upon the growth phase, shrink phase, and stable phase of the circle's animation and ensure to cover all possible values of t.
Here is the revised version of the balloon function and an updated main function:
module Task09 where
import CodeWorld
balloon :: Double -> Picture
balloon t
| t < 5 = colored red (solidCircle (min 10 t)) -- Grows until t reaches 5 or radius reaches 10
| t < 10 = colored (RGB 0 0.5 1) (solidCircle (max 5 (10 - (t - 5)))) -- Begins shrinking after t=5 until t=10
| otherwise = colored green (solidCircle 5) -- Maintains a constant size after t=10
main :: IO ()
main = animationOf balloon
Clarification and Improvements:
Growth Phase: The circle should continue to grow while t is less than 5, but should not exceed a maximum radius. In this example, 10 is chosen as the maximum radius it will grow to within the first 5 seconds.
Shrinking Phase: Once t is equal to or greater than 5 but less than 10, the circle will begin to shrink. This is implemented by starting from the largest size achieved at t=5 and shrinking based on the formula (10 - (t - 5)). The max 5 (...) ensures that it doesn't shrink beyond radius 5.
Stable Phase: Once 10 is reached, the circle's size stabilizes to radius 5.
Color Changes: The colors change according to the phase: Red during growth, a bluish (using RGB codes) during shrinking, and finally green when the size remains stable.
This should provide a smooth and clear transition between different stages of the animation without causing non-exhaustiveness warnings. By handling the otherwise case, we ensure that even for t values unexpectedly outside our range, the animation remains robust and doesn't run into matching errors.
-- The 'partialCircle' should result in a rotating circle with two
-- gaps at opposing sides. It takes a five-tuple and the animation
-- time as parameters. The five-tuple consists of the radius and
-- thickness of the circle, the size of the gaps in the circle (with
-- no particular unit, a bigger gap value just means a bigger gap),
-- the speed of the rotation (negative speeds invert the direction of
-- rotation), and the color of the circle.
-- The 'circles' function takes a list of parameters for partial
-- circles and should combine them into one animation.
-- (Hint: A list comprehension is useful here.)
-- Optional: For a little challenge, try modifing your program to
-- produce an animation like:
-- https://code.world/run.html?mode=haskell&dhash=DIDA_jQC__jKNt9NWimiMkA
-- or even:
-- https://code.world/run.html?mode=haskell&dhash=DPWdRCwl_iNJcP3NOwceFxg
-- (If you do so, please provide both the basic and the modified
-- version in your submission.)
-- Hint: The difficulty of this modification heavily depends on how
-- you realized movement in the basic version. There is a way to do it
-- such that only very little adjustment is needed for the advanced
-- version.
main :: IO ()
main = animationOf loading
loading :: Double -> Picture
loading t =
styledLettering Plain SansSerif (pack "LOADING")
& circles
[(3,0.2,1.5,2,black)
,(4,0.15,3,-0.5,red)
,(5,0.1,4.5,0.25,black)
] t
The partialCircle function will take a tuple describing the characteristics of a rotating circle with gaps, along with the current animation time, and produce the respective image of the circle at that timestamp.
2. Combining Partial Circles
The circles function will take a list of these tuples and combine the resulting circles as part of the single animated frame.
3. Implementation
Let's start by implementing the partialCircle function, followed by the circles function:
module Task08 where
import CodeWorld
partialCircle :: (Double, Double, Double, Double, Color) -> Double -> Picture
partialCircle (radius, thickness, gap, speed, color) t =
rotated (t * speed) $
colored color $
solidCircle radius & colored white (solidCircle (radius - thickness))
& translated 0 (radius - gap / 2) (colored white (solidRectangle (radius * 2) (gap)))
& rotated pi (translated 0 (radius - gap / 2) (colored white (solidRectangle (radius * 2) (gap))))
circles :: [(Double, Double, Double, Double, Color)] -> Double -> Picture
circles specs t = pictures [partialCircle spec t | spec <- specs]
main :: IO ()
main = animationOf loading
loading :: Double -> Picture
loading t =
styledLettering Plain SansSerif (pack "LOADING")
& circles
[(3, 0.2, 1.5, 2, black)
,(4, 0.15, 3, -0.5, red)
,(5, 0.1, 4.5, 0.25, black)
] t
Explanation
partialCircle Function:
rotated (t * speed): Animates the circle by rotating it based on time t and a given speed.
colored color (solidCircle radius): Creates a circle with the given radius and color.
colored white (solidCircle (radius - thickness)): Removes the circle's center to form a ring, using the thickness.
translated 0 (radius - gap / 2) ...: Places two white rectangles at opposing sides to form the gaps.
circles Function:
Uses a list comprehension to generate all the partial circles from the provided specification list.
This setup assumes you have properly configured CodeWorld's environment. You can directly compile and run this code in the Haskell CodeWorld environment to view your animation.