Saturday, November 30, 2013

Chapter 4: The Second Day of Project #2 (fractal)

          The second problem Fractal cost me almost 2 hours to define, test and finish. The jobs are to finish two definitions in the program. The first definition was bull-eyes that we have seen in the lecture and tutorial #8. So I easily retrieved the tutorial sheet and type in the definition on that. The idea about the definition was totally the same, so I thought that as long as I copied all the elements of the definition would be okay to pass. I was wrong! I simply copy the overlay sentence "(overlay (circle 10 "outline" "blue"))" on the definition which caused the problem afterward. Since the tests about the definition passed, I moved on to the second definition.
          The second definition was very similar to the fx problem, defining the key-press expression. This time, I was expected to press direction keys to increase or decrease the depth or radius of the bull-eyes. This definition has already started with a given statement, which indicate that I could use the same formula of fx. I need to figure out a new one. I started to analyse the goal of this definition, and I found that was as simple as just to change the depth and radius according to the different direction keys. This means that when I press one key there will be a new bull-eyes formed. Therefore, the structure of the definition has to be (make-param depth radius). However, the depth and radius are dynamic which has to use the conditional command to regulate two of them. According to the given check-expect and description, I can now write the definition as follows:
(define (key-press rp k)
  (make-param
   (cond
      [(key=? k "up") (add1 (param-depth rp))]
      [(key=? k "down") (sub1 (param-depth rp))]
      [else (param-depth rp)])
   (cond
     [(key=? k "left") (sub1 (param-radius rp))]
     [(key=? k "right") (add1 (param-radius rp))]
     [else (param-radius rp)]
     )))
          When I finished both definitions, I need to test them. The problem came across when I press "right" and "left" keys. The picture did not have any change as I pressed the keys. Then I re-examined the definition of key-press k again, and there was no error showed. I confirmed that the problem would be first definitions. I found out that my definition about bull-eyes radius was not dynamic because I restricted to 10. Finally, I changed my definition of bull-eyes to:
(define (eyes rp)
  (cond
    [(> (param-depth rp) 0)
     (overlay
      (circle (image-width (eyes (make-param (sub1 (param-depth rp)) (param-radius rp)))) "outline" "blue")
      (rotate
       90
       (beside
        (eyes (make-param (sub1 (param-depth rp)) (param-radius rp)))
        (eyes (make-param (sub1 (param-depth rp)) (param-radius rp))))))]
    [else
     (circle (param-radius rp) "outline" "blue")]
Works done!

No comments:

Post a Comment