Scheme 生成一个函数,使线段坐标以斜率m为中心,位于点(x y)处
我正在构建一个绘图库,想要绘制坡度线。我有一个函数draw seg list device color lst,其中lst arg是一个列表,包含x0 y0 x1 y1线的起始线和终止线的列表。我想创建一个函数make slope seg x y m,然后返回以x,y为中心的线段的点列表,该线段的斜率为m 示例:使坡度分段为0 0->-.05 0.05 0,并使坡度分段为1.1 1->.05.05.15.15 我的非工作功能是:Scheme 生成一个函数,使线段坐标以斜率m为中心,位于点(x y)处,scheme,mit-scheme,Scheme,Mit Scheme,我正在构建一个绘图库,想要绘制坡度线。我有一个函数draw seg list device color lst,其中lst arg是一个列表,包含x0 y0 x1 y1线的起始线和终止线的列表。我想创建一个函数make slope seg x y m,然后返回以x,y为中心的线段的点列表,该线段的斜率为m 示例:使坡度分段为0 0->-.05 0.05 0,并使坡度分段为1.1 1->.05.05.15.15 我的非工作功能是: (define (make-slope-cords x y m)
(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))
它的输出如下:
但我希望它在图像中的网格上输出宽度为.1平方的红线,斜率是蓝线λx平方x在沿x轴的每个点上的斜率,间隔为.1
注意:假设draw seg list有效。我只是需要帮助,使函数生成一个正确的cordinate列表经过周密的实验,我能够确定答案
(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))
产出如下: