【发布时间】:2011-03-28 23:47:01
【问题描述】:
有没有人成功地使用 MIT 方案让画线工作?
https://mitpress.mit.edu/sites/default/files/sicp/full-text/book/book-Z-H-15.html#%_sec_2.2.4
【问题讨论】:
标签: scheme mit-scheme
有没有人成功地使用 MIT 方案让画线工作?
https://mitpress.mit.edu/sites/default/files/sicp/full-text/book/book-Z-H-15.html#%_sec_2.2.4
【问题讨论】:
标签: scheme mit-scheme
关键词是:
例如,假设我们有一个程序 draw-line 在屏幕上的两个指定点之间绘制一条线。
换句话说,不存在draw-line——这纯粹是假设。
【讨论】:
我使用了 Gerald Sussman 提供的 scmutils。这是他的经典力学课程中使用的数值和代数包,对plotting graphs 有低级支持。
假设您已经定义了make-vect 和make-seg
(define line (make-seg (make-vect 2 3) (make-vect 4 1)))
(define win1 (frame -5 5 -5 5))
(define (draw-line frame a b)
(plot-line frame (xcor a) (ycor a) (xcor b) (ycor b))
)
这是我实现图片语言后得到的
【讨论】:
有一个actual MIT Scheme package 是本书作者为本书那部分的图片语言制作的 (SICP section 2.2.4)。但是,该软件包是在 1993 年编写的,我没有任何运气让它在我的 Mac OS High Sierra 上运行。
但是,其他人最近专门为本书的该部分制作了一个包,其中包含所有功能,包括画线,您实际上可以制作图片并玩弄它们。下载球拍,然后运行以下命令:
(require (planet "sicp.ss" ("soegaard" "sicp.plt" 2 1)))
欲了解更多信息,请参阅the user manual for this very awesome package。
【讨论】:
在阅读本书的这一部分时,我决定让draw-line 生成可以在 HTML 画布上绘制的 JavaScript 代码。
(define (draw-line v1 v2)
(newline)
(display "ctx.beginPath();")
(display (string-append "ctx.moveTo(" (number->string (xcor-vect v1)) "," (number->string (ycor-vect v1)) ");"))
(display (string-append "ctx.lineTo(" (number->string (xcor-vect v2)) "," (number->string (ycor-vect v2)) ");"))
(display "ctx.stroke();"))
毕竟draw-line 的具体实现方式对于使用它的程序并不重要,我们只需要一种可视化结果的方法。唯一的缺点是 HTML 画布坐标从左上角开始,而不是从左下角开始,但这也可以通过稍微调整生成的代码来处理。
这是完整的方案代码https://github.com/antivanov/scip-exercises/blob/master/scheme/ch2/2.48.49.scmhttps://github.com/antivanov/scip-exercises/blob/master/scheme/ch2/2.52.scm
按照相同的方法,我能够可视化本章中的其他示例/练习。
【讨论】:
我为我的一门课完成了这项画家作业。对于 2.47,你真的不需要担心画线。您需要做的就是制作构造函数和选择器:
(define (make-frame origin edge1 edge2)
(list origin edge1 edge2))
(define (origin-frame frame)
(car frame))
(define (edge1-frame frame)
(cadr frame))
(define (edge2-frame frame)
(caddr frame))
【讨论】: