创建一个函数,使线段坐标以点(x y)为中心,斜率为m

时间:2015-11-14 02:37:41

标签: scheme mit-scheme

我有一个正在构建source的绘图库,并希望绘制斜率线。我有一个函数(draw-seg-list device color lst),其中lst arg是一个列表,其中包含一行(x0 y0 x1 y1)的起始和终止线。我想创建一个函数(make-slope-seg 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

但是我希望它输出宽度为.1的红线(图像中网格上的1个方格),斜率是每个间隔点的蓝线(lambda(x)(方形x))的斜率。 1沿x轴。

注意 :假设draw-seg-list有效。我只需要帮助使函数make-slope-cords生成一个正确的坐标列表

1 个答案:

答案 0 :(得分: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