> For the complete documentation index, see [llms.txt](https://trizen.gitbook.io/perl6-rosettacode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://trizen.gitbook.io/perl6-rosettacode/programming_tasks/d/dragon_curve.md).

# Dragon curve

We'll use a L-System role, and draw the dragon in SVG.

```perl
use SVG;

role Lindenmayer {
    has %.rules;
    method succ { 
	    self.comb.map( { %!rules{$^c} // $c } ).join but Lindenmayer(%!rules)
    }
}

my $dragon = "FX" but Lindenmayer( { X => 'X+YF+', Y => '-FX-Y' } );

$dragon++ xx ^15;

my @points = 215, 350;

for $dragon.comb {
    state ($x, $y) = @points[0,1];
    state $d = 2 + 0i;
    if /'F'/ { @points.append: ($x += $d.re).round(.1), ($y += $d.im).round(.1) }
    elsif /< + - >/ { $d *= "{$_}1i" }
}

say SVG.serialize(
    svg => [
        :600width, :450height, :style<stroke:rgb(0,0,255)>,
        :rect[:width<100%>, :height<100%>, :fill<white>],
        :polyline[ :points(@points.join: ','), :fill<white> ],
    ],
);
```
