Skip to content

Instantly share code, notes, and snippets.

@msafadieh
Last active August 23, 2019 15:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msafadieh/2a4241effd9aedfd359bf3d5ec9c2dc7 to your computer and use it in GitHub Desktop.
Save msafadieh/2a4241effd9aedfd359bf3d5ec9c2dc7 to your computer and use it in GitHub Desktop.
more cool racket graphics
#lang racket
(require 2htdp/image)
(require 2htdp/universe)
;; structure used
(struct crcl (radius color))
;; constants
(define SIZE 500)
(define COUNT 5)
(define CENTER (/ SIZE 2))
(define RADIUS (/ (* SIZE (sqrt 1/2)) COUNT))
(define FPS 60)
(define STEP (/ RADIUS FPS))
(define BACKGROUND (square SIZE 'solid 'white))
;; creates a new crcl struct with a random color
;; num -> crcl
(define (new-circle r)
(crcl (* r RADIUS) (color (random 256) (random 256) (random 256))))
;; renders a list of circle structs
;; list-of-crcl -> image
(define (render circle-list)
(foldr (lambda (x y) (place-image (circle (crcl-radius x) 'solid (crcl-color x)) CENTER CENTER y))
BACKGROUND circle-list))
;; moves the circles on each tick (and creates/deletes ones when necessary)
;; list-of-crcls -> list-of-crcls
(define (move-circles crcls)
(if (>= (crcl-radius (first crcls)) RADIUS) (cons (new-circle 0) (drop-right crcls 1))
(map (lambda (x) (struct-copy crcl x [radius (+ STEP (crcl-radius x))])) crcls)))
;; big-bang function call that runs this infinitely
(big-bang (build-list (add1 COUNT) new-circle)
[name "Circles"]
[on-tick move-circles (/ 1 FPS)]
[to-draw render])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment