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.