Update
[less_retarded_wiki.git] / julia_set.md
blob75f6d4fd7739b34bd3b6158efe6073837d0a25c2
1 # Julia Set
3 Julia sets (named after mathematician *Gaston Julia*) are sets of 2D points that are very similar to [Mandelbrot set](mandelbrot_set.md) and just as Mandelbrot set they typically (but not always) have a [fractal](fractal.md) shape. While there is only one Mandelbrot set, there are [infinitely](infinity.md) many Julia sets because in the equation defining Julia set (which has the same format as for Mandelbrot set, just with different variables) there is a parameter we can change to get a different set. Specifically for any [complex number](complex_number.md) (which we may see as a point in 2D plane) there is one Julia set.
5 The **definition** of Julia set will follow (there is actually a more general one, but we'll stick to the narrower, most common one), notice how the equation is similar to that of Mandelbrot set. First we pick a constant complex number *c* that will define the whole set; then for each complex number *z* (a point in 2D plane for which we want to see if it belongs to the set or not) we consider the following iteration:
7 *z[n + 1] = z[n]^2 + c*
9 Then we see if under infinitely many iterations this series diverges towards infinity or if it stays bounded. If the point didn't diverge, it belongs to the set, otherwise not. When visualizing the set with a [computer](computer.md) we [approximate](approximation.md) this infinite iteration by performing just a big number of iterations.
11 The following is a picture of one possible Julia set:
13 ```
14  ___________________________________________________________________
15 | Julia Set for -0.34 - 0.63i       :.                              |
16 |                                ..':. ..                           |
17 |                                '':.:'''      ..       ..          |
18 |                                 :':::.. ''  ::.    .. :.'         |
19 |                                  '::::. :: :::. .   :.'': .       |
20 |                              ......':::.::.:: ...::.:::.::.. .    |
21 |                              :::::::::':'.':.::::::::':.::''::..  |
22 |                   .             '::::'':':::':'':::'  ::''  '     |
23 |                   ':.       .   .. ..::'::':::.   '   :'          |
24 |                 . :: :'     ::..::::::: ::: ':::..     '          |
25 |                   :'::::   '.:::::::::'.::::'  ''                 |
26 |                    .:::::' ':::::::::. ''::::'.                   |
27 |                  :. '::::'.::::::::::.  '::':.'                   |
28 |          . .   '':::. ::: ::::::::'::'    .::::                   |
29 |         :':.  ... ':::.:':::''  '  '        ''.                   |
30 |        ..::  .::::::...':.::::::.:                                |
31 |   :::...' '.::::::::'.: .:.:'::::'':                              |
32 |    '' :. : .:''':' :::'::':::.   ' '                              |
33 |         '::'': '' '::: ::'':::::                                  |
34 |          ::       ':.  '' '':::.:                                 |
35 |         ' '       '        ::.:.'.'                               |
36 |                              ::'                                  |
37 |                              '                                    |
38 |___________________________________________________________________|
39 ```
41 # Code
43 The following code is a simple [C](c.md) program that renders given Julia set into terminal (for demonstrative purposes, it isn't efficient or do any [antialiasing](antialiasing.md)).
45 ```
46 #include <stdio.h>
48 #define ROWS 30
49 #define COLS 70
50 #define SET_X -0.36 // Julia set parameter
51 #define SET_Y -0.62 // Julia set parameter
52 #define FROM_X -1.5
53 #define FROM_Y 1.0
54 #define STEP (3.0 / ((double) COLS))
56 unsigned int julia(double x, double y)
58   double cx = x, cy = y, tmp;
60   for (int i = 0; i < 1000; ++i)
61   {
62     tmp = cx * cx - cy * cy + SET_X;
63     cy = 2 * cx * cy + SET_Y;
64     cx = tmp;
66     if (cx * cx + cy * cy > 10000000000)
67       return 0;
68   }
70   return 1;
73 int main(void)
75   double cx, cy = FROM_Y;
77   for (int y = 0; y < ROWS; ++y)
78   {
79     cx = FROM_X;
81     for (int x = 0; x < COLS; ++x)
82     {
83       unsigned int point = 
84         julia(cx,cy) + (julia(cx,cy + STEP) * 2);   
86       putchar(point == 3 ? ':' : (point == 2 ? '\'' : 
87         (point == 1 ? '.' : ' ')));
89       cx += STEP;
90     }
92     putchar('\n');
94     cy -= 2 * STEP;
95   }
97   return 0;
99 ```