Monday, 3 December 2012

Code for Paper Folding Problem Cont'd

(require picturing-programs)
(check-expect (paperfold 0)
              (rotate -90 (line 0 16 "black")))
(check-expect (paperfold 1)
              (beside/align
               "bottom"
               (rotate -60 (paperfold 0))
               (rotate 60 (paperfold 0))))
(define (paperfold d)
  (cond
    [(zero? d) (rotate -90 (line 0 16 "black"))]
    [(= 1 d)
     (beside/align
      "bottom"
      (rotate -60 (paperfold 0))
      (rotate 60 (paperfold 0)))]
    [(= 2 d)
     (beside/align
      "bottom"
      (rotate 180 (paperfold 1))
      (paperfold 0)
      (rotate 60 (paperfold 0)))]
    [else
     (beside/align
      "bottom"
      (beside/align
       "bottom"
       (crop-right (rotate 180 (paperfold (sub1 d))) 7)
       (rotate -90 (line 0 16 "black")))
      (paperfold (sub1 d)))]))

I tried creating a new function before the definition of the "paperfold" function to fix the problem of the disconnecting link, but that did not work out because it requires using "paperfold" and "paperfold" function requires the link function in its definition.Therefore instead of creating a new function and inserting it into the "paperfold" function, I directly inserted the definition into the recursion of the "paperfold" function.

"crop-right" and "beside/align" replaces the end of the broken link in the sequence. Conveniently the middle link is always a *-U-U-*. The use of "(crop-right (... ...) 7)" is incorrect, it does not fully cut off the incorrect link before it is attached to the second part of the sequence. However, the "notches" left from the incorrect crop can be used to indicate sections with more than two same points (e.g. D-D-D).

The code seems to work and produces expected results up to five folds. Cannot confirm the code beyond that because I cannot fold a strip of paper more than five times.

No comments:

Post a Comment