(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.
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
/-\_/\_/
(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
/-\_/\_/
Subscribe to:
Posts (Atom)