l_system: L-system

Description Usage Arguments Details Value See Also Examples

View source: R/l_system.R

Description

Generate L-systems.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
l_system(
  axiom,
  rules,
  n = 1,
  angle,
  initial_angle = pi/2,
  draw_f = NULL,
  return_string = FALSE,
  extra_info = FALSE,
  remove_duplicates = TRUE
)

Arguments

axiom

A string of symbols representing the initial state of the system.

rules

A list of named strings forming the rules.

n

The number of iterations.

angle

The angle in radians which determines the change in direction for every "+" or "-".

initial_angle

The initial angle of the first line in radians.

draw_f

A character vector of symbols the replace with "F" in the instructions.

return_string

Logical. If TRUE the function returns the string of instructions after n iterations. Otherwise they are converted to line segments.

extra_info

Logical. If TRUE return additional information for all lines: length, angle, stack depth.

remove_duplicates

Logical. If TRUE remove duplicated lines from the result. Does not consider the direction of the lines, so a line from (0, 0) to (1, 1) is not a duplicate of a line from (1, 1) to (0, 0).

Details

List of valid instructions:

F

Draw a line in the current direction.

+ or -

Turn by angle.

[ or ]

Save or load current state.

@

Multiply the line length by the following numerical argument.

!

Flip the angle direction.

Value

Depending on the value of return_string either the string of instructions after n iterations or a data frame of class "l_system" with the columns x0, y0, x1, and y1 determining the endpoints of the line segments. Includes additional columns if extra_info is TRUE.

See Also

plot.l_system()

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# plant:
l_plant <- l_system(
    axiom = "X",
    rules = list(
        X = "F+[[X]-X]-F[-FX]+X",
        F = "FF"
    ),
    n = 7,
    angle = pi * 0.15,
    initial_angle = pi * 0.45
)
plot(l_plant, col = colorRampPalette(c("#008000", "#00FF00"))(100))

# dragon curve:
l_dragon <- l_system(
    axiom = "FX",
    rules = list(
        X = "X+YF+",
        Y = "-FX-Y"
    ),
    n = 12,
    angle = pi / 2,
    initial_angle = 0
)
plot(l_dragon, col = rainbow(nrow(l_dragon)))

# sierpinski triangle:
l_triangle <- l_system(
    axiom = "F-G-G",
    rules = list(
        F = "F-G+F+G-F",
        G = "GG"
    ),
    n = 6,
    angle = radians(120),
    initial_angle = radians(60),
    draw_f = "G"
)
plot(l_triangle)

# changing line length, flipping angle, and using extra_info = TRUE
# to vary color and line thickness:
l_tree <- l_system(
    axiom = "X",
    rules = list(X = "F[+@.7X]F![-@.6X]F"),
    n = 10,
    angle = radians(22.5),
    extra_info = TRUE
)
plot(
    l_tree,
    col = ifelse(l_tree$depth < 6, "sienna", "forestgreen"),
    lwd = l_tree$depth / max(l_tree$depth) * -2 + 3
)

BastiHz/fractalplotr documentation built on Sept. 9, 2021, 4:46 a.m.