Created
May 26, 2014 18:39
-
-
Save apeace/41e3f128db362e134f79 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
; TODO: fix this so it actually works for more functions and graph views | |
(require 2htdp/image) | |
(require 2htdp/universe) | |
(require test-engine/racket-tests) | |
(define WIDTH 200) | |
(define HEIGHT 200) | |
(define XMIN 0) | |
(define XMAX (* 2 pi)) | |
(define YMIN -1) | |
(define YMAX 1) | |
(define TICK-STEP .10) | |
(struct graph (width height xmin xmax ymin ymax)) | |
(struct posn (x y)) | |
(define (posn=? p1 p2) | |
(and (= (posn-x p1) | |
(posn-x p2)) | |
(= (posn-y p1) | |
(posn-y p2)))) | |
(define GRAPH (graph WIDTH HEIGHT XMIN XMAX YMIN YMAX)) | |
(define (value-to-coord g p) | |
(posn (/ (* (posn-x p) (graph-width g)) (- (graph-xmax g) (graph-xmin g))) | |
(/ (* (- (graph-ymax g) (posn-y p)) (graph-height g)) (- (graph-ymax g) (graph-ymin g))))) | |
(check-expect (posn=? (value-to-coord GRAPH (posn 0 1)) | |
(posn 0 0)) | |
true) | |
(check-expect (posn=? (value-to-coord GRAPH (posn (/ pi 2) -1)) | |
(posn 50 200)) | |
true) | |
(define (print-posn p) | |
(string-append "x: " (number->string (posn-x p)) | |
" " | |
"y: " (number->string (posn-y p)))) | |
(define (draw-dot p) | |
(place-image (circle 5 'solid 'red) | |
(posn-x p) | |
(posn-y p) | |
(empty-scene WIDTH HEIGHT))) | |
(define (draw-scene tick) | |
(let* ([step (* tick TICK-STEP)] | |
[num-cycles (floor (/ step XMAX))] | |
[real-step (- step (* num-cycles XMAX))] | |
[x (+ XMIN real-step)] | |
[y (/ (sin x) 1.5)] | |
[p (value-to-coord GRAPH (posn x y))]) | |
(draw-dot p))) | |
(test) | |
(animate draw-scene) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment