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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Dragon curve step by step in the Factor 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 Factor programming language

Source code in the factor programming language

USING: accessors colors colors.hsv fry kernel locals math
math.constants math.functions opengl.gl typed ui ui.gadgets
ui.gadgets.canvas ui.render ;

IN: dragon

CONSTANT: depth 12

TUPLE: turtle
    { angle fixnum }
    { color float }
    { x float }
    { y float } ;

TYPED: nxt-color ( turtle: turtle -- turtle )
    [ [ 360 2 depth ^ /f + ] keep
      1.0 1.0 1.0 <hsva> >rgba-components glColor4d
    ] change-color ; inline

TYPED: draw-fwd ( x1: float y1: float x2: float y2: float -- )
    GL_LINES glBegin glVertex2d glVertex2d glEnd ; inline

TYPED:: fwd ( turtle: turtle l: float -- )
    turtle x>>
    turtle y>>
    turtle angle>> pi * 180 / :> ( x y angle )
    l angle [ cos * x + ] [ sin * y + ] 2bi :> ( dx dy )
    turtle x y dx dy [ draw-fwd ] 2keep [ >>x ] [ >>y ] bi* drop ; inline

TYPED: trn ( turtle: turtle d: fixnum -- turtle )
    '[ _ + ] change-angle ; inline

TYPED:: dragon' ( turtle: turtle l: float s: fixnum d: fixnum -- )
    s zero? [
        turtle nxt-color l fwd ! don't like this drop
    ] [
        turtle d  45 * trn l 2 sqrt / s 1 -  1 dragon'
        turtle d -90 * trn l 2 sqrt / s 1 - -1 dragon'
        turtle d  45 * trn drop
    ] if ;

: dragon ( -- )
    0 0 150 180 turtle boa 400 depth 1 dragon' ;

TUPLE: dragon-canvas < canvas ;

M: dragon-canvas draw-gadget* [ drop dragon ] draw-canvas ;
M: dragon-canvas pref-dim* drop { 640 480 } ;

MAIN-WINDOW: dragon-window { { title "Dragon Curve" } }
    dragon-canvas new-canvas >>gadgets ;

MAIN: dragon-window


  

You may also check:How to resolve the algorithm Apply a callback to an array step by step in the Bracmat programming language
You may also check:How to resolve the algorithm Scope/Function names and labels step by step in the C programming language
You may also check:How to resolve the algorithm User input/Text step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Even or odd step by step in the Java programming language
You may also check:How to resolve the algorithm Factorial step by step in the SequenceL programming language