計算機科学のブログ

Building Abstractions with Data - Hierarchical Data and the Closure Property - Example: A Picture Language - Painters - segments to painter, frame, x, diamond shape

Structure and Interpretation of Computer Programs: JavaScript Edition(Harold Abelson(著)、Gerald Jay Sussman(著)、Julie Sussman(著)、The MIT Press)のChapter 2(Building Abstractions with Data)、2.2(Hierarchical Data and the Closure Property)、2.2.4(Example: A Picture Language)、Painters、Exercise 2.49の解答を求めてみる。

コード

// a.
const v1 = make_vect(0, 0);
const v2 = make_vect(0, 1);
const v3 = make_vect(1, 1);
const v4 = make_vect(1, 0);
const outline = segments_to_painter([
    make_segment(v1, v2),
    make_segment(v2, v3),
    make_segment(v3, v4),
    make_segment(v4, v1)
]);
// b.
const x = segments_to_painter([
    make_segment(v1, v3),
    make_segment(v2, v4)
]);
// c.
const v5 = make_vect(0, 0.5);
const v6 = make_vect(0.5, 1);
const v7 = make_vect(1, 0.5);
const v8 = make_vect(0.5, 0);
const diamond = segments_to_painter([
    make_segment(v5, v6),
    make_segment(v6, v7),
    make_segment(v7, v8),
    make_segment(v8, v5)
]);