(archive 'newLISPer)

November 14, 2010

Mystic rose

Filed under: newLISP — newlisper @ 17:26
Tags:

Draw lines joining all the vertices of a polygon. We used to draw these patterns at school.

mystic-rose1.png

It’s fun to see them cropping up on the internet now. We called them ‘mystic roses’, although Wikipedia is curiously silent on the topic so I haven’t yet had time to track down the significance of the name.

A quick attempt at coding it up persuaded me that there’s really no short and effective alternative to simple iteration for these sorts of tasks.

    (set 'number-of-points 155 'rad 190 'pi 3.141592653)
    ; make points list
    (for (point 0 (- number-of-points 1))
            (push (list  (mul rad (cos (div (mul 2 pi point) number-of-points)))
                                     (mul rad (sin (div (mul 2 pi point) number-of-points)))) points-list))
    ; draw lines connecting points
    (set 'b (- (length points-list) 1))
    (set 'n 0)

    (for (i 0 b)
    (for (j (+ i 1) b 1 (>= j (length points-list)))
       (inc n)
       (draw canvas-name (points-list i) (points-list j) (apply amb primary-colors))))

(I’ve not shown the code for the draw function that draws the lines onto an HTML5 canvas. See canvas.lsp in the official newLISP distribution for help.)

I did try to make it work without the obvious for loops but I gave up. It can’t get much simpler than this, can it? Can you persuade apply or map or series to do it in less code? Why recurse when you can iterate?

Nevertheless, I’m slightly unhappy with the lack of something I can’t quite put my finger on. Why is it always necessary to be subtracting or adding 1 to loop terminators, or checking for overflows, when I’m writing these sorts of scripts? I’m slightly jealous of the Mathematica definition which looks coolly elegant and slightly forbidding at the same time.

Ah well, it’s fun playing with the colours and alpha values. By cranking up the values and inserting some randomness, you get some quite pleasing effects.

mystic-rose2.png

And this 155-a-gon (what is its greek name?) starts to take on some mysterious textures of its own:

mystic-rose3.png

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: