Skip to content

Instantly share code, notes, and snippets.

@rdivyanshu
Last active August 6, 2023 05:48
Show Gist options
  • Save rdivyanshu/91a0765cc94cc4970b8547fb31c2a36e to your computer and use it in GitHub Desktop.
Save rdivyanshu/91a0765cc94cc4970b8547fb31c2a36e to your computer and use it in GitHub Desktop.
Generative Art
#lang racket
(require racket/draw)
(require pict)
(define (cubic-disarray width
height
size)
(dc (lambda (dc dx dy)
(define old-brush (send dc get-brush))
(define old-pen (send dc get-pen))
(send dc set-brush "white" 'transparent)
(for ([i (range 0 width size)])
(for ([j (range 0 height size)])
(define old-transformation (send dc get-transformation))
(define path (new dc-path%))
(define rotation-sign (if (>= (random) 0.5) 1 -1))
(define displacement-sign (if (>= (random) 0.5) 1 -1))
(send dc set-origin (+ dx i (* displacement-sign (random) (/ j height))) (+ dy j))
(send dc set-rotation (* rotation-sign (random) (/ j height) (/ pi size)))
(send path rectangle 0 0 size size)
(send dc draw-path path)
(send dc set-transformation old-transformation)))
(send dc set-brush old-brush)
(send dc set-pen old-pen))
width
height))
(linewidth 0.7 (inset (scale (cubic-disarray 100 100 10) 4) 15))
#lang racket
(require racket/draw)
(require pict)
(define (tiled-lines width
height
step)
(dc (lambda (dc dx dy)
(define old-brush (send dc get-brush))
(define old-pen (send dc get-pen))
(send dc set-brush "white" 'transparent)
(for ([i (range 0 width step)])
(for ([j (range 0 height step)])
(define path (new dc-path%))
(if (>= (random) 0.5)
(begin (send path move-to (+ dx i) (+ dy j))
(send path line-to (+ dx i step) (+ dy j step)))
(begin (send path move-to (+ dx i step) (+ dy j))
(send path line-to (+ dx i) (+ dy j step))))
(send dc draw-path path)))
(send dc set-brush old-brush)
(send dc set-pen old-pen))
width
height))
(linewidth 0.7 (scale (tiled-lines 100 100 5) 4))
#lang racket
(require pict)
(struct rect (x y w h) #:transparent)
(define (contains? rectangle dir coord)
(or (and (eq? dir 'x)
(< (rect-x rectangle) coord)
(< coord (+ (rect-x rectangle) (rect-w rectangle))))
(and (eq? dir 'y)
(< (rect-y rectangle) coord)
(< coord (+ (rect-y rectangle) (rect-h rectangle))))))
(define (break-apart dir coord rects)
(foldr append '()
(map (lambda (rectangle)
(if (and (>= (random) 0.5)
(contains? rectangle dir coord))
(if (eq? dir 'x)
(list (struct-copy rect rectangle
[w (- coord (rect-x rectangle))])
(struct-copy rect rectangle
[w (- (+ (rect-x rectangle) (rect-w rectangle)) coord)]
[x coord]))
(list (struct-copy rect rectangle
[h (- coord (rect-y rectangle))])
(struct-copy rect rectangle
[h (- (+ (rect-y rectangle) (rect-h rectangle)) coord)]
[y coord])))
(list rectangle)))
rects)))
(define (piet-mondrian size
step)
(dc (lambda (dc dx dy)
(define old-brush (send dc get-brush))
(define old-pen (send dc get-pen))
(define old-transformation (send dc get-transformation))
(send dc set-brush "white" 'solid)
(send dc set-origin dx dy)
(define rectangles
(foldl (lambda (coord rects)
(break-apart 'y coord (break-apart 'x coord rects)))
(list (rect 0 0 size size))
(range step size step)))
(define colors (make-vector (length rectangles) "white"))
(for ([c (list "red" "yellow" "blue")])
(vector-set! colors (exact-floor (* (random) (length rectangles))) c))
(for ([r rectangles]
[c colors])
(send dc set-brush c 'solid)
(send dc draw-rectangle (rect-x r) (rect-y r) (rect-w r) (rect-h r)))
(send dc set-transformation old-transformation)
(send dc set-brush old-brush)
(send dc set-pen old-pen))
size
size))
(linewidth 2 (scale (piet-mondrian 180 30) 2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment