2015-11-14 1 views
0

J'ai une bibliothèque de traçage que je construis source, et je veux tracer des lignes de pente. J'ai une fonction (draw-seg-list device color lst) avec le lst arg étant une liste contenant des listes avec les cordons de début et d'arrêt d'une ligne (x0 y0 x1 y1). Je veux faire une fonction (make-slope-seg x y m) qui retourne alors la liste des points pour un segment de droite centré sur (x, y) avec la pente m.Créer une fonction qui permet de centrer les coordonnées des segments au point (x y) avec la pente m

Exemple: (make-slope-seg 0 0 0) ->(-.05 0 .05 0) et (make-slope-seg .1 .1 1) ->(.05 .05 .15 .15)

La fonction non-travail que j'ai est:

(define (make-slope-cords x y m) 
    (list (- x .05) 
     (* y m -1) 
     (+ x .05) 
     (* y m))) 

qui renvoie les lignes incorrectes. Si j'utilise:

;makes graphics window 
(define window (make-graphics-device 'win32)) 

;plots blue line for function y = x^2 with black axis 
(make-plot window 'simple-plot (list "white" "black" "blue" (list (range -1 1 .01) square))) 

;makes list of lists containing the slope and x y cords that the slope lines 
;are supposed to be centered at 
(define cords (map (lambda (s x y) 
        (list s x y)) 
        (map (lambda (x) (* 2 x)) (range -1 1 .1)) 
        (range -1 1 .1) 
        (map square (range -1 1 .1)))) 

;plots the line segments generated by mapping make-slope-cords to the coordinate list 
(draw-seg-list window "red" 
       (map (lambda (lst) 
         (make-slope-cords (car lst) (cadr lst) (caddr lst))) 
        cords)) 

Il produit les éléments suivants: enter image description here

Mais je veux qu'il sortie des lignes rouges de largeur 0,1 (1 place sur la grille dans l'image) avec une pente étant la pente la ligne bleue (lambda (x) (carré x)) à chaque point espacé de .1 le long de l'axe des x.

NOTE: supposer que draw-seg-list œuvres. J'ai juste besoin d'aide pour faire la fonction make-slope-cords produire la bonne liste de cordinates

Répondre

0

Bien expérimentant autour de moi était capable de déterminer la réponse.

(define (make-sloped-seg x y m) 
    (define b (- y (* m x))) 
    (list (- x .03) 
     (+ (* m (- x .03)) b) 
     (+ x .03) 
     (+ (* m (+ x .03)) b))) 

Il détermine l'ordonnée à l'origine (b) au début du calcul, et génère alors les points en utilisant l'ordonnée à l'origine correcte

exemple:

;makes graphics window 
(define window (make-graphics-device 'win32)) 

;plots blue line for function y = x^2 with black axis 
(make-plot window 'simple-plot (list "white" "black" "blue" (list (range -1 1 .01) square))) 

;makes list of lists containing the slope and x y cords that the slope lines 
;are supposed to be centered at 
(define cords (map (lambda (s x y) 
        (list s x y)) 
        (map (lambda (x) (* 2 x)) (range -1 1 .1)) 
        (range -1 1 .1) 
        (map square (range -1 1 .1)))) 

;plots the line segments generated by mapping make-slope-cords to the coordinate list 
(draw-seg-list window "red" 
       (map (lambda (lst) 
         (make-slope-cords (car lst) (cadr lst) (caddr lst))) 
        cords)) 

délivre en sortie le texte suivant: enter image description here