Monday, 3 December 2012

Dr.Racket Code for Paper Folding Problem

In regards to the paper folding problem, I tried to write a code in Dr.Racket for it.

(require picturing-programs)
(define (paperfold d)
  (cond
    [(zero? d) (rotate -90 (line 0 15 "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
      (rotate 180 (paperfold (sub1 d)))
      (paperfold (sub1 d)))]))

Run into a problem in recursion. Because when d >= 3, the code takes the 180 degree rotated version of sub1 d, the image produced is disconnected in the middle. Mentioned before in the previous post, the sequence of points of N folds is equal to reverse of points of N-1 folds appended to the front of points of N-1 folds with the exception that the middle point (always will be a middle point) is opposite. This is what is causing the image produced to be disconnected, considering creating a function which specifically replaces that one faulty part and entering it into the recursion.

(paperfold 3) looks like this
/-\//\_/

(paperfold 3) should look like this
/-\_/\_/

No comments:

Post a Comment