jeudi 8 juillet 2010

Sierpinski triangle

How to construct with Dr. Geo the Sierpinski triangle?
It can be programmed recursively with a Pharo script.
Dr. Geo comes with a DSL to describe a geometric sketch with code. From a workspace -- a place to write code --  an interactive Sierpinski triangle can be computed with the following programming instructions:
| c triangle |
triangle := [:s1 :s2 :s3 :n |
    c segment: s1 to: s2;
        segment: s2 to: s3;
        segment: s3 to: s1.
    n >0 ifTrue:
        [triangle
            value: s1
            value: (c middleOf: s1 and: s2) hide
            value: (c middleOf: s1 and: s3) hide
            value: n-1.
        triangle
            value: (c middleOf: s1 and: s2) hide
            value: s2
            value: (c middleOf: s2 and: s3) hide
            value: n-1.
        triangle
            value: (c middleOf: s1 and: s3) hide
            value: (c middleOf: s2 and: s3) hide
            value: s3
            value: n-1.]].
c := DrGeoSketch new.
triangle
    value: (c point: 0 @ 3)
    value: (c point: 4 @ -3)
    value: (c point: -4 @ -3)
    value: 3.

Of course, the result is all interactive.

Aucun commentaire:

Enregistrer un commentaire