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

Published on 12 May 2024 09:40 PM

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

Source code in the processing programming language

float l = 3;
int ints = 13;

void setup() {
  size(700, 600);
  background(0, 0, 255);
  translate(150, 100);
  stroke(255);
  turn_left(l, ints);
  turn_right(l, ints);
}

void turn_right(float l, int ints) {
  if (ints == 0) {
    line(0, 0, 0, -l);
    translate(0, -l);
  } else {
    turn_left(l, ints-1);
    rotate(radians(90));
    turn_right(l, ints-1);
  }
}

void turn_left(float l, int ints) {
  if (ints == 0) {
    line(0, 0, 0, -l);
    translate(0, -l);
  } else {
    turn_left(l, ints-1);
    rotate(radians(-90));
    turn_right(l, ints-1);
  }
}


l = 3
ints = 13

def setup():
  size(700, 600)
  background(0, 0, 255)
  translate(150, 100)
  stroke(255)
  turn_left(l, ints)
  turn_right(l, ints)

def turn_right(l, ints):
    if ints == 0:
        line(0, 0, 0, -l)
        translate(0, -l)
    else:
        turn_left(l, ints - 1)
        rotate(radians(90))
        turn_right(l, ints - 1)
  
def turn_left(l, ints):
    if ints == 0:
        line(0, 0, 0, -l)
        translate(0, -l)
    else:
        turn_left(l, ints - 1)
        rotate(radians(-90))
        turn_right(l, ints - 1)


  

You may also check:How to resolve the algorithm Create a file on magnetic tape step by step in the IS-BASIC programming language
You may also check:How to resolve the algorithm Roman numerals/Decode step by step in the Tailspin programming language
You may also check:How to resolve the algorithm 15 puzzle game step by step in the Tcl programming language
You may also check:How to resolve the algorithm Runtime evaluation step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Terminal control/Ringing the terminal bell step by step in the M2000 Interpreter programming language