Tuesday 4 December 2012

Trouble with Recursion - Snowflake

When talking about repetitive patterns, I had some trouble trying to understand the fact that with three Koch Curves (an example of a fractal) we can form a snowflake. First, to approach the problem, I did some reading on the issue of fractals, that in fact gave me the idea that it is a kind of system that shows self-similarity. Having an idea of what the Koch Curve was, I started making possible connections with the appearance of a snowflake. In this process I took the silhouette of a snowflake, to have an idea of what the end product of my work should be, and I also took a piece of paper and a pen to start drawing the Koch curve. Moreover, I was following a piece of code in DrRacket that the professor gave us about the snowflake. Then, from this piece of code I had in mind that I need just 3 Koch curves to compose my snowflake. So, I started to draw three Koch curves in the first stage of the fractal, that is to say _/\_ , and I got a 'David Star'. From here, I started to do recursion, that is to use the same curve in each of its fragments, until I got what would be the snowflake. In overall, to solve my problem I used "Deep Simplicity" by John Gribbin (which I highly recommend if you are interested in the nature of fractals and life), and I also used the piece of code given by the professor. Also, as I am a read-write learner, I had to follow every step of the fractal in a piece of paper to better absorb the progress of my understanding regarding fractals.

Sunday 18 November 2012

Swapping Colors in DrRacket


I think that the fact we can exchange the colours in an entire image is very interesting and useful. At the beginning, it is a hard concept to learn, since first we have to be able to apply the function, let’s say ‘swap-red-blue-green’, to a single pixel in the image. After we are sure that it works, then we proceed to apply the function to the whole image through a tool named ‘map’ followed by the function we want to try.  
The following code is an example of how to apply a function like ‘swap-red-blue-green’ to all the pixels in an image.

       (require picturing-programs)

       (define c (make-color 1 2 3 4))   ; pick any numbers for a colour

       (check-expect (rotate-red-blue-green (make-color 2 3 4 5))  (make-color 3 4 2 5))

        (define (rotate-red-blue-green c) (make-color 
                                                        (color-green c)
                                                        (color-blue c)
                                                        (color-red c)
                                                        (color-alpha c)
                                                         )
          )

         (map-image rotate-red-blue-green  )

Finally, as a result for having swapped the colours, we get the following image:


Saturday 3 November 2012

Algorithm : Paper Folding

1. Understand the problem.
What we should be able to do, is predict the sequence of creases pointing up and pointing down for any number of foldings.
2. Device a plan.
Every time we fold the paper, we record how many creases are pointing up, or how many are pointing down. Then, we analyze the results and see if there is a sequence. If this is the case, we can stop folding the paper and proceed to make a prediction of what is going to be the result for the next number of foldings.
3. Carry out the plan.
*Place a piece of paper between your two hands, so when you first fold it the left end of the paper is on top on the right end of it.
*Unfold it, and record the direction of the crease(s).
*Repeat this step, until you notice a sequence in your observations.
4. Look back.
Using "d" for vertex pointing down, and "u" for vertex pointing up, from our recording we have:
First Folding:                        l"d"l
Second:                          l"u"l"d"l"d"l
Third:                l"u"l"u"l"d"l"d"l"u"l"d"l"d"l
 -> here we notice that the first half of the observation for the third fold is the inverted mirror image of the previous number of foldings. That is 'u' 'd' 'd' -> 'd' 'u' 'u'; the middle character is "d" because it was the result of the first fold, and it is not going to change. The second half of the recording is going to be exactly the same as the full observation of the previous number of foldings.