<commit_before># Control flow, user defined data types and macros


## Exercise

To apply what we've learned about control flow, in this lesson we are going to render the mandelbrot set in GLSL. Given any complex number `c`, define the sequence of `z(n)` to be:

```
z(0)   = 0.0
z(n+1) = z(n) * z(n) + c
```

Then we say that the point `c` is in the mandelbrot set if this sequence converges

```
    lim        |z(n)|   < infinity
n -> infinity
```

Since we can't calculate z(infinity) directly, we will instead make do with a finite approximation.  Here you should write a shader program which computes `z(100)` and tests if the magnitude of `z` is less than 2.  You should implement the function called `mandelbrot` in the <a href="/open/intro-3">`mandelbrot.glsl` file in this project's directory</a>.

### Hints

Remember the definition of complex multiplication:

```
(a + b*i) * (c + d*i) = (a * c - b * d) + (a * d + b * d) * i
```

And the magnitude of a complex number is:

```
| a + b*i | = sqrt(a*a + b*b)
```

***

In this lesson, we will finish up our discussion of the basic GLSL programming language, covering control flow statements, user defined data types and the preprocessor.
<commit_msg>simplified intro-3 problem, lots more hints<commit_after># Control flow, user defined data types and macros


## Exercise

Implement a function which tests whether a point is inside the Mandelbrot set after 100 iterations.  The Mandelbrot set is a fractal, which is defined by iterating a chaotic map on the complex plane and testing which points converge.  Given an input point `c`, one iteration of the Mandelbrot function is defined as follows:

```glsl
vec2 mandelbrot(vec2 z, vec2 c) {
  return vec2(z.x * z.x - z.y * z.y, 2.0 * z.x * z.y) + c;
}
```

And some iterations of the map are given as follows:

```glsl
vec2 z0 = vec2(0.0,0.0);        //No iterations
vec2 z1 = mandelbrot(z0, c);    // 1 iteration
vec2 z2 = mandelbrot(z1, c);    // 2 iterations

// ... and so on ...

vec2 zn = mandelbrot(zn_1, c);  // n iterations
```

As a general principle, say that a point in the Mandelbrot set diverges if it has a magnitude greater than 2:

```glsl
bool mandelbrotDiverges(vec2 z) {
  return length(z) >= 2.0;
}
```

The Mandelbrot set is the collection of all points which do not diverge. Write a function which given a test point `c` tests if after 100 iterations it is still inside the Mandelbrot set.  To get started, a <a href="/open/intro-3">file `mandelbrot.glsl` has been created in this project's directory.</a>

***

In this lesson, we will finish up our discussion of the basic GLSL programming language, covering control flow statements, user defined data types and the preprocessor.

{'fraction_non_alphanumeric': 0.10092081031307551, 'fraction_numerical': 0.016206261510128914, 'mean_word_length': 3.674698795180723, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4905119', 'n_tokens_mistral': 904, 'n_tokens_neox': 844, 'n_words': 403}