Giter Site home page Giter Site logo

fp1's Introduction

My Library: (2htdp/image)

My name: Brendan Bousquet

For my first project exploration I decided to take a little blast from the past and relive Computing 4 with some fractals. Specifically Sierpinski fractals using squares and triangles. Then also using some squares, the Koch curve. I found it really awesome how easy this was to do with Racket using the 2htdp/image library. With all the shapes already defined, all I really had to do was read some documentation on how the procedures like above, beside, and rotate worked and the implementations were easy to realize. For the squares and the koch curve I did have the benefit of looking at some of the examples that the Racket documentation had for these. I then made additions and changes based on what seemed useful or more suited to my style so I could get a better undertanding for what the new procedures were doing. For example in the sierpinski squares procedure, each recursive call defines an x (relative to last square drawn) and a y (the new square to be drawn) and uses the above and beside procedures to draw the sqaures in the fractal pattern.

(define (sierpinski-squares n)
  (if (zero? n)
      (square 5 "solid" "black")
      (local ((define x (sierpinski-squares (- n 1)))
              (define y (square (image-width x) "solid" "yellow")))
        (freeze (above (beside x x x)
                       (beside x y x)
                       (beside x x x))))))

(sierpinski-squares 4)

squares.png

The method was obviously similar for sierpinski triangles which used the same two procedure to draw the fractal, just in a slightly different way.

(define (sierpinski n)
 (if (zero? n)
   (triangle 10 "solid" "blue")
   (let ((x (sierpinski (- n 1))))
     (freeze (above x (beside x x))))))

(sierpinski 4)

triangles.png

I used the freeze procedure for the sierpinski triangles and square because the documentation claimed that for an image of sub-images it was very efficient. Fractals seemed like a perfect instance for that. I did not use it for the koch curve becuase it would make the image not show up on the bottom. I assume this is because the nature of the fractal is different from sierpinski and less suited to it's use. The koch curve was where I messed around with rotate. This was a little tricky to wrap my head around but since I had the koch curve snowflake implementation in the Racket documentation to look at I eventually got something that was pretty solid using beside/align to align the squares, despite rotating them. It was very interesting to see how easy this was to learn and understand because I remember how hard it was to wrap my head around doing something similar in C++ just a year ago.

(define (koch n)
(if (zero? n)
    (square 10 "solid" "red")
    (local ((define smaller (koch (- n 1))))
      (beside/align "bottom"
                 smaller
                 (rotate 60 smaller)
                 (rotate -60 smaller)
                 smaller))))

(koch 3)

koch.png

fp1's People

Contributors

brendanbousquet avatar marksherman avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.