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

Published on 12 May 2024 09:40 PM

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

Source code in the fsharp programming language

open System.Windows
open System.Windows.Media

let m = Matrix(0.0, 0.5, -0.5, 0.0, 0.0, 0.0)

let step segs =
  seq { for a: Point, b: Point in segs do
          let x = a + 0.5 * (b - a) + (b - a) * m
          yield! [a, x; b, x] }

let rec nest n f x =
  if n=0 then x else nest (n-1) f (f x)

[<System.STAThread>]
do
  let path = Shapes.Path(Stroke=Brushes.Black, StrokeThickness=0.001)
  path.Data <-
    PathGeometry
      [ for a, b in nest 13 step (seq [Point(0.0, 0.0), Point(1.0, 0.0)]) ->
          PathFigure(a, [(LineSegment(b, true) :> PathSegment)], false) ]
  (Application()).Run(Window(Content=Controls.Viewbox(Child=path))) |> ignore


  

You may also check:How to resolve the algorithm User input/Text step by step in the jq programming language
You may also check:How to resolve the algorithm CSV to HTML translation step by step in the Phix programming language
You may also check:How to resolve the algorithm Prime triangle step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Display a linear combination step by step in the Tcl programming language
You may also check:How to resolve the algorithm Update a configuration file step by step in the Wren programming language