首页 > 其他分享 >sicp每日一题[2.51]

sicp每日一题[2.51]

时间:2024-10-21 08:48:48浏览次数:7  
标签:2.51 1.0 0.0 每日 sicp vect new make painter

Exercise2.51

Define the below operation for painters. below takes two painters as arguments. The resulting painter, given a frame, draws with the first painter in the bottom of the frame
and with the second painter in the top. Define below in two different ways—first by writing a procedure that is analogous to the beside procedure given above, and again
in terms of beside and suitable rotation operations (from Exercise 2.50).


这道题目难度不大,理解了 2.50 那张图后,再仿照 beside 的代码,只要把3个坐标的位置调整一下就可以了; 至于利用 beside 和旋转来实现 below,就更简单了,先把2幅图向右旋转90°,然后 beside 排到一起,
最后再把排到一起的图形向左旋转90°即可。

#lang racket


(require (planet "sicp.ss" ("soegaard" "sicp.plt" 2 1)))


(define sub-vect vector-sub)

; Transforming and combining painters
(define (transform-painter painter origin corner1 corner2)
  (lambda (frame)
    (let ((m (frame-coord-map frame)))
      (let ((new-origin (m origin)))
        (painter (make-frame
                  new-origin
                  (sub-vect (m corner1) new-origin)
                  (sub-vect (m corner2) new-origin)))))))

(define (beside painter1 painter2)
  (let ((split-point (make-vect 0.5 0.0)))
    (let ((paint-left
           (transform-painter
            painter1
            (make-vect 0.0 0.0)
            split-point
            (make-vect 0.0 1.0)))
          (paint-right
           (transform-painter
            painter2
            split-point
            (make-vect 1.0 0.0)
            (make-vect 0.5 1.0))))
      (lambda (frame)
        (paint-left frame)
        (paint-right frame)))))

; analogous to beside
(define (below painter1 painter2)
  (let ((split-point (make-vect 0.0 0.5)))
    (let ((paint-up
           (transform-painter
            painter2
            split-point
            (make-vect 1.0 0.5)
            (make-vect 0.0 1.0)))
          (paint-low
           (transform-painter
            painter1
            (make-vect 0.0 0.0)
            (make-vect 1.0 0.0)
            split-point)))
      (lambda (frame)
        (paint-up frame)
        (paint-low frame)))))

(paint (below einstein (beside einstein einstein)))

(define (rotate90 painter)
  (transform-painter painter
                     (make-vect 1.0 0.0) ; new origin
                     (make-vect 1.0 1.0) ; new end of edge1
                     (make-vect 0.0 0.0))) ; new end of edge2

(define (rotate180 painter)
  (transform-painter painter
                     (make-vect 0.0 1.0) ; new origin
                     (make-vect 1.0 1.0) ; new end of edge1
                     (make-vect 0.0 0.0))) ; new end of edge2

(define (rotate270 painter)
  (transform-painter painter
                     (make-vect 0.0 1.0) ; new origin
                     (make-vect 0.0 0.0) ; new end of edge1
                     (make-vect 1.0 1.0))) ; new end of edge2

; in terms of beside and suittable rotation
(define (below- painter1 painter2)
  (rotate90 (beside (rotate270 painter1) (rotate270 painter2))))

(paint (below- einstein (beside einstein einstein)))

结果如下所示:

标签:2.51,1.0,0.0,每日,sicp,vect,new,make,painter
From: https://www.cnblogs.com/think2times/p/18488281

相关文章

  • 代码随想录算法训练营 | 739. 每日温度,496.下一个更大元素 I ,503.下一个更大元素II
    739.每日温度题目链接:739.每日温度文档讲解︰代码随想录(programmercarl.com)视频讲解︰每日温度日期:2024-10-20想法:遍历一遍数组,用栈来存数组下标做记录,因为要找更高得温度,当当前遍历的温度大于栈头存储(存的下标)的温度时,就可以知道栈头要过多少天遇到高温,低的时候直接入栈。J......
  • sicp每日一题[2.50]
    Exercise2.50Definethetransformationflip-horiz,whichflipspaintershorizontally,andtransformationsthatrotatepainterscounterclockwiseby180degreesand270degrees.这道题挺有意思的,搞明白这道题就明白了frame的3个点的位置。如上图所示,为了更好......
  • sicp每日一题[2.49]
    Exercise2.49Usesegments->paintertodefinethefollowingprimitivepainters:a.Thepainterthatdrawstheoutlineofthedesignatedframe.b.Thepainterthatdrawsan“X”byconnectingoppositecornersoftheframe.c.Thepainterthatdrawsad......
  • 20241018每日一题洛谷P2386
    普及每日一题信息学竞赛1206:放苹果把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1是同一种分法。第一行是测试数据的数目t(0<=t<=20)。以下每行均包含二个整数M和N,以空格分开。1<=M,N<=10。对输入的每组数据M和N,用一行输出相......
  • sicp每日一题[2.48]
    Exercise2.48Adirectedlinesegmentintheplanecanberepresentedasapairofvectors—thevectorrunningfromtheorigintothestart-pointofthesegment,andthevectorrunningfromtheorigintotheend-pointofthesegment.Useyourvectorrepre......
  • 每日OJ题_牛客_非对称之美_最长非回文字符串_C++_Java
    目录牛客_非对称之美_最长非回文字符串题目解析C++代码Java代码牛客_非对称之美_最长非回文字符串非对称之美(nowcoder.com)题目解析找到规律就是最长非回文字符串(判断是否全同->0,否则是n-1(回文减去1)或n)。C++代码#include<iostream>usingnamespacestd;int......
  • 每日OJ题_牛客_连续子数组最大和_线性dp_C++_Java
    目录牛客_连续子数组最大和_线性dp题目解析C++代码Java代码牛客_连续子数组最大和_线性dp连续子数组最大和_牛客题霸_牛客网(nowcoder.com)描述:        给定一个长度为 n的数组,数组中的数为整数。请你选择一个非空连续子数组,使该子数组所有数之和尽可能大,......
  • 2024-10-17每日一题题解
    最大子段和题目描述给出一个长度为\(n\)的序列\(a\),选出其中连续且非空的一段使得这段和最大。样例输入72-43-12-43样例输出4题解tips:无脑暴力法:枚举每一段区间,再对每一段区间求和,时间复杂度为\(O(n^3)\),会超时(n为1e5,则应该在\(O(nlogn)\)的时间范围内)......
  • 高级java每日一道面试题-2024年10月17日-Web篇-常见的web攻击有哪些?
    如果有遗漏,评论区告诉我进行补充面试官:常见的web攻击有哪些?我回答:常见的Web攻击种类繁多,攻击者利用各种漏洞和技术手段来入侵网站、窃取数据或破坏服务。以下是一些最常见的Web攻击类型及其简要说明:1.SQL注入(SQLInjection,SQLi)描述:攻击者通过在输入字段......
  • sicp每日一题[2.47]
    Exercise2.47Herearetwopossibleconstructorsforframes:(define(make-frameoriginedge1edge2)(listoriginedge1edge2))(define(make-frameoriginedge1edge2)(consorigin(consedge1edge2)))Foreachconstructorsupplytheappropriate......