Skip to content

Instantly share code, notes, and snippets.

@RenaissanceBug
Created December 7, 2014 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RenaissanceBug/10388f70cae20b34c4d6 to your computer and use it in GitHub Desktop.
Save RenaissanceBug/10388f70cae20b34c4d6 to your computer and use it in GitHub Desktop.
Beginner Racket solar-system model
(require 2htdp/image)
(require 2htdp/universe)
; Sun and background
(define SPACE (rectangle 900 900 "solid" "black"))
(define SUN (star 20 "solid" "gold"))
;Key
; (+ [center of ellipse] (* [determines width/height of ellipse]
; (cos (/ fn [adjusts the speed of rotation])))))
; ;[current speed (for testing purposes)]
; Mercury
(define MERCURY (circle 5 "solid" "darkgray"))
(define (MERCURYx fn) (+ 450 (* 53 (cos (/ fn 8)))));8
(define (MERCURYy fn) (+ 450 (* 41 (sin (/ fn 8)))))
; Venus
(define VENUS (circle 9 "solid" (make-color 240 230 240)))
(define (VENUSx fn) (+ 450 (* 74 (cos (/ fn 10.6)))));10.6
(define (VENUSy fn) (+ 450 (* 60 (sin (/ fn 10.6)))))
; Earth
(define EARTH (circle 10 "solid" (make-color 0 90 205)))
(define (EARTHx fn) (+ 450 (* 100 (cos (/ fn 12)))));12
(define (EARTHy fn) (+ 450 (* 86 (sin (/ fn 12)))))
; Mars
(define MARS (circle 7 "solid" "darkorange"))
(define (MARSx fn) (+ 450 (* 135 (cos (/ fn 18)))));18
(define (MARSy fn) (+ 450 (* 122 (sin (/ fn 18)))))
; Asteriod
(define ASTEROID (overlay (circle 160 "solid" "white")
(circle 155 "solid" (make-color 145 135 85))
(circle 150 "solid" "brown")
(circle 145 "solid" (make-color 145 135 85))
(circle 140 "outline" "white")
(circle 139 "solid" "black")))
; Jupiter
(define JUPITER (circle 25 "solid" "orange"))
(define (JUPITERx fn) (+ 450 (* 200 (cos (/ fn 24)))));24
(define (JUPITERy fn) (+ 450 (* 186 (sin (/ fn 24)))))
; Saturn
(define SATURN (overlay
(circle 35 "solid" "gray")
(circle 34 "solid" "brown")
(circle 33 "solid" "orange")
(circle 32 "solid" "white")
(circle 31 "solid" (make-color 145 135 85))
(circle 30 "solid" "darkorange")
(circle 29 "solid" "gray")
(circle 28 "solid" "brown")
(circle 27 "solid" "lightgray")
(circle 26 "solid" "gray")
(circle 25 "solid" "black")
(circle 18.5 "solid" (make-color 145 135 85))))
(define (SATURNx fn) (+ 450 (* 275 (cos (/ fn 29)))));29
(define (SATURNy fn) (+ 450 (* 261 (sin (/ fn 29)))))
; Uranus
(define URANUS (circle 19 "solid" "blue"))
(define (URANUSx fn) (+ 450 (* 350 (cos (/ fn 34)))));34
(define (URANUSy fn) (+ 450 (* 336 (sin (/ fn 34)))))
; Neptune
(define NEPTUNE (circle 20 "solid" (make-color 20 140 255)))
(define (NEPTUNEx fn) (+ 450 (* 410 (cos (/ fn 60)))));60
(define (NEPTUNEy fn) (+ 450 (* 396 (sin (/ fn 60)))))
;; solar-system: number -> image
;; consumes: the frame number
;; produces: a representation of the solar system
;; (not to scale)
(define (solar-system fn)
(scale 1/2 (place-image SUN 450 450
(place-image MERCURY
(MERCURYx fn) (MERCURYy fn)
(place-image VENUS
(VENUSx fn) (VENUSy fn)
(place-image EARTH
(EARTHx fn) (EARTHy fn)
(place-image MARS
(MARSx fn) (MARSy fn)
(place-image ASTEROID
450 450
(place-image JUPITER
(JUPITERx fn) (JUPITERy fn)
(place-image SATURN
(SATURNx fn) (SATURNy fn)
(place-image URANUS
(URANUSx fn) (URANUSy fn)
(place-image NEPTUNE
(NEPTUNEx fn) (NEPTUNEy fn)
SPACE))))))))))))
(big-bang 0
(on-tick add1 0.05)
(on-draw solar-system))
;; For maximum effect, run and let the animation sit for 15-20 seconds. This makes it seem more realistic.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment