创建一个函数,使线段坐标以点 (x y) 为中心,斜率为 m
Make a function that makes line segment coordinates centered at point (x y) with slope m
我正在构建一个绘图库 source,我想绘制斜线。我有一个函数 (draw-seg-list device color lst)
,其中 lst
arg 是一个列表,其中包含带有一行 (x0 y0 x1 y1)
的开始和停止线的列表。我想创建一个函数 (make-slope-seg x y m) 然后 returns 以 (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)))
其中 returns 行不正确。如果我使用:
;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 的红线(图像中网格上的 1 个正方形),斜率是蓝线的斜率(lambda (x) (square x)) 在每个点间隔沿 x 轴 .1。
注意:假设 draw-seg-list
有效。我只需要帮助使函数 make-slope-cords
生成正确的坐标列表
经过反复试验,我确定了答案。
(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))
输出如下:
我正在构建一个绘图库 source,我想绘制斜线。我有一个函数 (draw-seg-list device color lst)
,其中 lst
arg 是一个列表,其中包含带有一行 (x0 y0 x1 y1)
的开始和停止线的列表。我想创建一个函数 (make-slope-seg x y m) 然后 returns 以 (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)))
其中 returns 行不正确。如果我使用:
;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 的红线(图像中网格上的 1 个正方形),斜率是蓝线的斜率(lambda (x) (square x)) 在每个点间隔沿 x 轴 .1。
注意:假设 draw-seg-list
有效。我只需要帮助使函数 make-slope-cords
生成正确的坐标列表
经过反复试验,我确定了答案。
(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))
输出如下: