Zum Inhalt
Schaubilder
Aus ProgrammingWiki
<
Bergervei
Archivierte Version vom 2011-02-12T12:16:07+00:00.
Aktuelle Version
(require-library 'sisc/libs/srfi/srfi-6) (import srfi-6) (define nr 1) (define genau 6) (define width 610) (define height 460) (define canvas (eval (string->symbol (string-append "canvas" (number->string nr))))) (define bez "x") (define black (lambda (canvas) (setcolor canvas 0 0 0))) (define red (lambda (canvas) (setcolor canvas 255 0 0))) (define brown (lambda (canvas) (setcolor canvas 100 50 0))) (define orange (lambda (canvas) (setcolor canvas 255 100 0))) (define yellow (lambda (canvas) (setcolor canvas 255 200 0))) (define green (lambda (canvas) (setcolor canvas 0 255 0))) (define blue (lambda (canvas) (setcolor canvas 0 0 255))) (define pink (lambda (canvas) (setcolor canvas 200 50 200))) (define n-tes-mod (lambda (ls anz) (list-ref ls (modulo anz (length ls))))) (define genaurunden (lambda (x anz) (let ([n 0] [erg 0]) (if (not (= x 0)) (set! n (inexact->exact (round (- anz (/ (log (abs x)) (log 10)) -0.5))))) (set! erg (/ (round (* x (expt 10 n))) (expt 10 n))) (if (integer? erg) (inexact->exact erg) (exact->inexact erg))))) (define groessenordnung (lambda (x) (if (= x 0) 1 (let ([pot (/ (log (abs x)) (log 10))]) (genaurunden (expt 10 (floor pot)) genau))))) (define min (lambda (x y) (if (< x y) (if (> x 0) 0 x) (if (> y 0) 0 y)))) (define max (lambda (x y) (if (> x y) (if (< x 0) 0 x) (if (< y 0) 0 y)))) (define ungleich-null (lambda (x y) (if (= x y 0) (+ y 1) y))) (define schaubild (lambda (fkt-list x1 x2 y1 y2 von bis koordcolor color-list) (clear canvas) (setwidth canvas 1) (let* ([textgroesse 9] [rand 50] [xu (exact->inexact (min x1 x2))] [xo (exact->inexact (max x1 x2))] [xo (ungleich-null xu xo)] [yu (exact->inexact (min y1 y2))] [yo (exact->inexact (max y1 y2))] [yo (ungleich-null yu yo)] [spmin rand] [spmax (- width rand)] [spend (+ spmax 15)] [zemin (- height rand)] [zemax rand] [zeend (- zemax 15)] [s0 (+ (* (/ (- xu) (- xo xu)) (- spmax spmin)) spmin)] [z0 (+ (* (/ (- yu) (- yo yu)) (- zemax zemin)) zemin)] [spvon (+ (* (/ (- von xu) (- xo xu)) (- spmax spmin)) spmin)] [spbis (+ (* (/ (- bis xu) (- xo xu)) (- spmax spmin)) spmin)] [mx (- xo xu)] [teilerx 1] [stepx (/ (- spmax spmin) mx teilerx)] [ex mx] [faktorx 1] [my (- yo yu)] [teilery 1] [stepy (/ (- zemin zemax) my teilery)] [ey my] [faktory 1]) (letrec ([anpassen (lambda (m) (if (> m 2) (if (<= m 20) m (anpassen (/ m 10))) (if (> m 2) m (anpassen (* m 10)))))] [teilen (lambda (m) (cond [(< m 3) 5] [(< m 10) 2] [else 1]))] [x-teilen-positiv (lambda (sx) (if (< sx (+ spmax 1)) (begin (line canvas sx z0 sx (- z0 3)) (x-teilen-positiv (+ sx stepx)))))] [x-teilen-negativ (lambda (sx) (if (> sx (- spmin 1)) (begin (line canvas sx z0 sx (- z0 3)) (x-teilen-negativ (- sx stepx)))))] [y-teilen-positiv (lambda (zy) (if (> zy (- zemax 1)) (begin (line canvas s0 zy (+ s0 3) zy) (y-teilen-positiv (- zy stepy)))))] [y-teilen-negativ (lambda (zy) (if (< zy (+ zemin 1)) (begin (line canvas s0 zy (+ s0 3) zy) (y-teilen-negativ (+ zy stepy)))))] [faktor (lambda (len step) (if (<= len step) 1 (+ 1 (faktor (- len step) step))))] [textwidth (lambda (str) (* textgroesse (string-length str)))] [textout-widthcenter (lambda (sx zy str) (if (integer? (string->number str)) (text canvas (- sx (/ (textwidth str) 2)) (+ zy (/ textgroesse 2/3)) str textgroesse) (text canvas (- sx (/ (textwidth str) 2) (- (/ textgroesse 2))) (+ zy (/ textgroesse 2/3)) str textgroesse)))] [textout-heightcenter (lambda (sx zy str) (if (integer? (string->number str)) (text canvas (- sx (textwidth str) 2) (+ zy (/ textgroesse 2)) str textgroesse) (text canvas (- sx (textwidth str) (- (/ textgroesse 2))) (+ zy (/ textgroesse 2)) str textgroesse)))] [textout-bez-widthcenter (lambda (sx zy str) (text canvas (- sx (/ (textwidth str) 4)) (- zy textgroesse) str textgroesse))] [textout-bez-heightcenter (lambda (sx zy str) (text canvas (+ sx (/ textgroesse 2)) (+ zy (/ textgroesse 2)) str textgroesse))] [x-bezeichnen-positiv (lambda (sx e) (if (< sx (+ spmax 1)) (begin (textout-widthcenter sx z0 (number->string (genaurunden e genau))) (x-bezeichnen-positiv (+ sx (* stepx faktorx)) (+ e (* ex faktorx))))))] [x-bezeichnen-negativ (lambda (sx e) (if (> sx (- spmin 1)) (begin (textout-widthcenter sx z0 (number->string (genaurunden e genau))) (x-bezeichnen-negativ (- sx (* stepx faktorx)) (- e (* ex faktorx))))))] [y-bezeichnen-positiv (lambda (zy e) (if (> zy (- zemax 1)) (begin (textout-heightcenter s0 zy (number->string (genaurunden e genau))) (y-bezeichnen-positiv (- zy (* stepy faktory)) (+ e (* ey faktory))))))] [y-bezeichnen-negativ (lambda (zy e) (if (< zy (+ zemin 1)) (begin (textout-heightcenter s0 zy (number->string (genaurunden e genau))) (y-bezeichnen-negativ (+ zy (* stepy faktory)) (- e (* ey faktory))))))] [x-achse-bezeichnen (lambda (str) (textout-bez-heightcenter spend z0 str))] [y-achse-bezeichnen (lambda (str) (let ([y-str (string-append "f(" str ")")]) (textout-bez-widthcenter s0 zeend y-str)))] [graph-startpkt (lambda (fkt pkt) (let ([sx (car pkt)] [zy (cdr pkt)]) (if (< sx (+ spbis 1)) (with-failure-continuation (lambda (error-record error-k) (graph-startpkt fkt (cons (+ sx 1) zy))) (lambda () (let* ([x (+ (* (/ (- sx spmin) (- spmax spmin)) (- xo xu)) xu)] [y (fkt x)]) (if (real? y) (begin (set! zy (+ (* (/ (- y yu) (- yo yu)) (- zemax zemin)) zemin)) (if (> zy zemin) (set! zy zemin)) (if (< zy zemax) (set! zy zemax)) (cons sx zy)) (graph-startpkt fkt (cons (+ sx 1) zy)))))) (cons sx zy))))] [graph-zeichnen (lambda (fkt pkt) (let ([sx (car pkt)] [zy (cdr pkt)]) (if (< sx (+ spbis 1)) (with-failure-continuation (lambda (error-record error-k) (graph-zeichnen fkt (graph-startpkt fkt (cons (+ sx 1) zy)))) (lambda () (let* ([sx-neu (+ sx 1)] [x (+ (* (/ (- sx-neu spmin) (- spmax spmin)) (- xo xu)) xu)] [y (fkt x)]) (if (real? y) (let ([zy-neu (+ (* (/ (- y yu) (- yo yu)) (- zemax zemin)) zemin)]) (if (> zy-neu zemin) (set! zy-neu zemin)) (if (< zy-neu zemax) (set! zy-neu zemax)) (if (and (< zy zemin) (> zy zemax) (< zy-neu zemin) (> zy-neu zemax)) (line canvas sx zy sx-neu zy-neu)) (graph-zeichnen fkt (cons sx-neu zy-neu))) (graph-zeichnen fkt (graph-startpkt fkt (cons (+ sx 1) zy))))))))))] [graph-list-zeichnen (lambda (fkt-ls anz color-ls) (if (not (null? fkt-ls)) (let ([fkt (eval (car fkt-ls))]) ((eval (n-tes-mod color-ls anz)) canvas) (graph-zeichnen fkt (graph-startpkt fkt (cons spvon z0))) (graph-list-zeichnen (cdr fkt-ls) (+ anz 1) color-ls))))]) (koordcolor canvas) (line canvas spmin z0 spend z0) (line canvas s0 zemin s0 zeend) (line canvas (- spend 6) (- z0 2) spend z0) (line canvas (- spend 6) (+ z0 2) spend z0) (line canvas (- spend 6) (- z0 2) (- spend 6) (+ z0 2)) (line canvas (- s0 2) (+ zeend 6) s0 zeend) (line canvas (+ s0 2) (+ zeend 6) s0 zeend) (line canvas (- s0 2) (+ zeend 6) (+ s0 2) (+ zeend 6)) (set! mx (anpassen mx)) (set! teilerx (teilen mx)) (set! stepx (/ (- spmax spmin) mx teilerx)) (set! my (anpassen my)) (set! teilery (teilen my)) (set! stepy (/ (- zemin zemax) my teilery)) (x-teilen-positiv (+ s0 stepx)) (x-teilen-negativ (- s0 stepx)) (y-teilen-positiv (- z0 stepy)) (y-teilen-negativ (+ z0 stepy)) (set! ex (/ ex mx teilerx)) (set! faktorx (faktor (* textgroesse (string-length (number->string (genaurunden ex genau)))) stepx)) (set! ey (/ ey my teilery)) (set! faktory (faktor textgroesse stepy)) (if (= yu 0) (x-bezeichnen-positiv s0 0) (x-bezeichnen-positiv (+ s0 (* stepx faktorx)) (* ex faktorx))) (x-bezeichnen-negativ (- s0 (* stepx faktorx)) (- (* ex faktorx))) (if (= xu 0) (y-bezeichnen-positiv z0 0) (y-bezeichnen-positiv (- z0 (* stepy faktory)) (* ey faktory))) (y-bezeichnen-negativ (+ z0 (* stepy faktory)) (- (* ey faktory))) (x-achse-bezeichnen bez) (y-achse-bezeichnen bez) (if (< spvon spmin) (set! spvon spmin)) (if (> spbis spmax) (set! spbis spmax)) (graph-list-zeichnen fkt-list 0 color-list) 'ok))))
(define fkt (lambda (x) (- (* 2 x) 1))) (define termeingabe (lambda () (let ([proz (read (open-input-string (inputDialog "Prozedur (Lambda-Term):")))]) (with-failure-continuation (lambda (error-record error-k) #f) (lambda () (eval proz))))))
Ihr Browser unterstützt Canvas nicht.
(set! fkt (termeingabe)) (if fkt (schaubild (list fkt) -10 10 -8 8 -10 10 black '(red)) (begin (display "--> Eingabefehler!") (newline) 'ok))
ausführen
Seite
Quelltext
Versionen/Autoren
Konto registrieren
Anmelden
Navigation
Startseite
Letzte Änderungen
Alle Seiten
Einführungskurs
Hilfe
Dateien
Suche
Suchen
Werkzeuge
Links auf diese Seite
Spezialseiten