How to resolve the algorithm Dragon curve step by step in the Gri programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Dragon curve step by step in the Gri programming language

Table of Contents

Problem Statement

Create and display a dragon curve fractal. (You may either display the curve directly or write it to an image file.)

Here are some brief notes the algorithms used and how they might suit various languages. This always has F at even positions and S at odd. Eg. after 3 levels F_S_F_S_F_S_F_S. The +/- turns in between bend to the left or right the same as the "successive approximation" method above. Read more at for instance Joel Castellanos' L-system page. Variations are possible if you have only a single symbol for line draw, for example the Icon and Unicon and Xfractint code. The angles can also be broken into 45-degree parts to keep the expansion in a single direction rather than the endpoint rotating around. The string rewrites can be done recursively without building the whole string, just follow its instructions at the target level. See for example C by IFS Drawing code. The effect is the same as "recursive with parameter" above but can draw other curves defined by L-systems.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Dragon curve step by step in the Gri programming language

Source code in the gri programming language

`Draw Dragon [ from .x1. .y1. to .x2. .y2. [level .level.] ]'
Draw a dragon curve going from .x1. .y1. to .x2. .y2. with recursion
depth .level.

The total number of line segments for the recursion is 2^level.
level=0 is a straight line from x1,y1 to x2,y2.

The default for x1,y1 and x2,y2 is to draw horizontally from 0,0
to 1,0.
{
    new .x1. .y1. .x2. .y2. .level.
    .x1. = \.word3.
    .y1. = \.word4.
    .x2. = \.word6.
    .y2. = \.word7.
    .level. = \.word9.
    
    if {rpn \.words. 5 >=}
        .x2. = 1
        .y2. = 0
    end if
    if {rpn \.words. 7 >=}
        .level. = 6
    end if
    
    if {rpn 0 .level. <=}
        draw line from .x1. .y1. to .x2. .y2.
    else
        .level. = {rpn .level. 1 -}

        # xmid,ymid is half way between x1,y1 and x2,y2 and up at
        # right angles away.
        #
        #            xmid,ymid             xmid = (x1+x2 + y2-y1)/2
        #            ^       ^             ymid = (x1-x2 + y1+y2)/2
        #           /    .    \
        #          /     .     \
        #     x1,y1 ........... x2,y2
        #
        new .xmid. .ymid.
        .xmid. = {rpn .x1. .x2. + .y2. .y1. - + 2 /}
        .ymid. = {rpn .x1. .x2. - .y1. .y2. + + 2 /}
        
        # The recursion is a level-1 dragon from x1,y1 to the midpoint
        # and the same from x2,y2 to the midpoint (the latter
        # effectively being a revered dragon.)
        #
        Draw Dragon from .x1. .y1. to .xmid. .ymid. level .level.
        Draw Dragon from .x2. .y2. to .xmid. .ymid. level .level.
        
        delete .xmid. .ymid.
    end if
    
    delete .x1. .y1. .x2. .y2. .level.
}

# Dragon curve from 0,0 to 1,0 extends out by 1/3 at the ends, so
# extents -0.5 to +1.5 for a bit of margin.  The Y extent is the same
# size 2 to make the graph square.
set x axis -0.5 1.5   .25
set y axis -1 1 .25

Draw Dragon

  

You may also check:How to resolve the algorithm Emirp primes step by step in the Stata programming language
You may also check:How to resolve the algorithm Remove lines from a file step by step in the Haskell programming language
You may also check:How to resolve the algorithm Modified random distribution step by step in the Wren programming language
You may also check:How to resolve the algorithm Kosaraju step by step in the 11l programming language
You may also check:How to resolve the algorithm Musical scale step by step in the Kotlin programming language