perjantai 21. elokuuta 2015

Mondrian Art - Racket Turtle

Kuulin tästä ohjelmointioppitunnista, jossa tarkoituksena on ohjelmoida Mondrian Art - kuvia (katso myös Artsy.net). Pitihän sitä kokeilla myös omalla Racket Turtlella. Tein ensin pohjan johon generoin satunnaisesti eri kokoisia ja eri värisiä laatikoita eri paikkoihin. Sitten väritin reunat Racket Turtlella.

Tässä oma näkemykseni Mondrian Art:ista:

Mondrian Art ohjelmoituna DrRacket:llä 

Täsäs koodi jolla generoin pohjakuvan:

(require teachpacks/racket-turtle)
(require 2htdp/image)

(define BG (empty-scene 500 500))
(define colors (list "red" "yellow" "blue" "black"))
 

(define (get-color i)
  (list-ref colors i))

(define (place-rectangle x y bg)
  (place-image (rectangle (* 40 (random 5)) 

                          (* 40 (random 5)) 
                          "solid" 
                          (get-color (random 4)))
                           x y bg))

(define (create-bg x y times bg)
  (if (< times 0)
      bg
      (create-bg (+ x (* 20 (random 5))) 

                 (+ y (* 20 (random 5))) 
                 (sub1 times) 
                 (place-rectangle x y bg))))

(define IMG (create-bg 0 250 10 

                   (create-bg  300 0 10 
                          (create-bg 0 0 100 BG))))

Tämä koodi aikaansai siis seuraavan kuvan:

Pohjakuva (BG-IMG1)
 Tämän päälle piti enää lisätä mustia viivoja Racket Turtlen avulla. 

(define TURTLE-BG-IMG1
(list (set-bg-image BG-IMG1)
      (change-color "black")
      (pen-up) (go-to 20 300)
      ;horizontal rows
      (pen-down)(change-pen-size 5)(go-to 20 500)

      (pen-up)(go-to 40 0)
      (pen-down)(change-pen-size 3)(go-to 40 300)

      (pen-up)(go-to 120 0)
      (pen-down)(change-pen-size 4)(go-to 120 300)

      (pen-up)(go-to 140 180)
      (pen-down)(change-pen-size 3)(go-to 140 500)

      (pen-up)(go-to 160 420)
      (pen-down)(change-pen-size 2)(go-to 160 500)

      (pen-up)(go-to 180 340)
      (pen-down)(change-pen-size 2)(go-to 180 500)

      (pen-up)(go-to 220 0)
      (pen-down)(change-pen-size 3)(go-to 220 340)

      (pen-up)(go-to 260 0)
      (pen-down)(change-pen-size 4)(go-to 260 500)

      (pen-up)(go-to 280 30)
      (pen-down)(change-pen-size 2)(go-to 280 150)

      (pen-up)(go-to 300 360)
      (pen-down)(change-pen-size 3)(go-to 300 500)

      (pen-up)(go-to 320 20)
      (pen-down)(change-pen-size 3)(go-to 320 120)

      (pen-up)(go-to 360 0)
      (pen-down)(change-pen-size 4)(go-to 360 360)

      (pen-up)(go-to 380 280)
      (pen-down)(change-pen-size 3)(go-to 380 500)

      (pen-up)(go-to 420 280)
      (pen-down)(change-pen-size 3)(go-to 420 360)

      (pen-up)(go-to 460 280)
      (pen-down)(change-pen-size 3)(go-to 460 500)

      (pen-up)(go-to 320 20)
      ; vertical rows
      (pen-down)(change-pen-size 4)(go-to 500 20)

      (pen-up)(go-to 400 20)
      (pen-down)(change-pen-size 2)(go-to 500 20)

      (pen-up)(go-to 120 30)
      (pen-down)(change-pen-size 4)(go-to 280 30)

      (pen-up)(go-to 280 80)
      (pen-down)(change-pen-size 4)(go-to 500 80)

      (pen-up)(go-to 280 120)
      (pen-down)(change-pen-size 4)(go-to 320 120)

      (pen-up)(go-to 40 110)
      (pen-down)(change-pen-size 3)(go-to 120 110)

      (pen-up)(go-to 120 150)
      (pen-down)(change-pen-size 4)(go-to 280 150)

      (pen-up)(go-to 120 180)
      (pen-down)(change-pen-size 5)(go-to 220 180)

      (pen-up)(go-to 40 230)
      (pen-down)(change-pen-size 4)(go-to 120 230)

      (pen-up)(go-to 20 300)
      (pen-down)(change-pen-size 2)(go-to 140 300)

      (pen-up)(go-to 260 280)
      (pen-down)(change-pen-size 4)(go-to 500 280)

      (pen-up)(go-to 140 340)
      (pen-down)(change-pen-size 3)(go-to 220 340)

      (pen-up)(go-to 300 360)
      (pen-down)(change-pen-size 4)(go-to 380 360)

      (pen-up)(go-to 380 440)
      (pen-down)(change-pen-size 5)(go-to 500 440)

      (pen-up)(go-to 400 450)))
(define image1 (draw TURTLE-BG-IMG1))
(crop 0 0 500 500 image1) 

Ei kommentteja:

Lähetä kommentti