2015-11-14 49 views
0

我有一個繪圖庫,我正在構建source,並且想繪製斜線。我有一個功能(draw-seg-list device color lst)lst arg是一個列表,其中包含一行(x0 y0 x1 y1)的開始線和停止線的列表。我想創建一個函數(make-slope-seg x y m),然後返回以(x,y)爲中心的斜率爲m的線段的點列表。製作一個函數,使線段座標以點(x y)爲中心,斜率爲m

實施例:(make-slope-seg 0 0 0) - >(-.05 0 .05 0)(make-slope-seg .1 .1 1) - >(.05 .05 .15 .15)

非工作功能我是:

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

它返回不正確的線。如果我使用:

;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)) 

它輸出以下: enter image description here

但是我它與斜率爲斜率要寬度0.1(1平方上的圖像中的網格)輸出紅線沿着x軸以.1間隔的每個點處的藍線(lambda(x)(square x))。

備註:假定draw-seg-list有效。我只需要幫助,使功能make-slope-cords產生一個正確的配對名單

回答

0

很好的實驗我能夠確定答案。

(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))) 

它確定在計算開始時的y截距(b)中,然後使用正確的截距

示例生成點:

;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)) 

輸出以下: enter image description here

相關問題