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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
private void makeMandelbrotSet()
        {
            
            Graphics gScreen = this.CreateGraphics();
            Size sz = this.ClientSize;
            bmp = new Bitmap(sz.Width, sz.Height);
            Graphics gBitmap = Graphics.FromImage(bmp);
            Pen p = Pens.Black;
 
            Pen[] colorPens = new Pen[72];
            colorPens[0] = Pens.AliceBlue;
            colorPens[1] = Pens.AntiqueWhite;
            colorPens[2] = Pens.Aqua;
            colorPens[3] = Pens.Aquamarine;
            colorPens[4] = Pens.Azure;
            colorPens[5] = Pens.Beige;
            colorPens[6] = Pens.Bisque;
            colorPens[7] = Pens.BlanchedAlmond;
            colorPens[8] = Pens.Blue;
            colorPens[9] = Pens.BlueViolet;
            colorPens[10] = Pens.Brown;
            colorPens[11] = Pens.BurlyWood;
            colorPens[12] = Pens.CadetBlue;
            colorPens[13] = Pens.Chartreuse;
            colorPens[14] = Pens.Chocolate;
            colorPens[15] = Pens.Coral;
            colorPens[16] = Pens.CornflowerBlue;
            colorPens[17] = Pens.Cornsilk;
            colorPens[18] = Pens.Crimson;
            colorPens[19] = Pens.Cyan;
            colorPens[20] = Pens.DarkBlue;
            colorPens[21] = Pens.DarkCyan;
            colorPens[22] = Pens.DarkGoldenrod;
            colorPens[23] = Pens.DarkGray;
            colorPens[24] = Pens.DarkGreen;
            colorPens[25] = Pens.DarkKhaki;
            colorPens[26] = Pens.DarkMagenta;
            colorPens[27] = Pens.DarkOliveGreen;
            colorPens[28] = Pens.DarkOrange;
            colorPens[29] = Pens.DarkOrchid;
            colorPens[30] = Pens.DarkRed;
            colorPens[31] = Pens.DarkSalmon;
            colorPens[32] = Pens.DarkSeaGreen;
            colorPens[33] = Pens.DarkSlateBlue;
            colorPens[34] = Pens.DarkSlateGray;
            colorPens[35] = Pens.DarkTurquoise;
            colorPens[36] = Pens.DarkViolet;
            colorPens[37] = Pens.DeepPink;
            colorPens[38] = Pens.DeepSkyBlue;
            colorPens[39] = Pens.DimGray;
            colorPens[40] = Pens.DodgerBlue;
            colorPens[41] = Pens.Firebrick;
            colorPens[42] = Pens.FloralWhite;
            colorPens[43] = Pens.ForestGreen;
            colorPens[44] = Pens.Fuchsia;
            colorPens[45] = Pens.Gainsboro;
            colorPens[46] = Pens.GhostWhite;
            colorPens[47] = Pens.Gold;
            colorPens[48] = Pens.Goldenrod;
            colorPens[49] = Pens.Green;
            colorPens[50] = Pens.GreenYellow;
            colorPens[51] = Pens.Honeydew;
            colorPens[52] = Pens.HotPink;
            colorPens[53] = Pens.IndianRed;
            colorPens[54] = Pens.Indigo;
            colorPens[55] = Pens.Ivory;
            colorPens[56] = Pens.Khaki;
            colorPens[57] = Pens.Lavender;
            colorPens[58] = Pens.LavenderBlush;
            colorPens[59] = Pens.LawnGreen;
            colorPens[60] = Pens.LemonChiffon;
            colorPens[61] = Pens.LightBlue;
            colorPens[62] = Pens.LightCoral;
            colorPens[63] = Pens.LightCyan;
            colorPens[64] = Pens.LightGoldenrodYellow;
            colorPens[65] = Pens.LightGray;
            colorPens[66] = Pens.LightYellow;
            colorPens[67] = Pens.Lime;
            colorPens[68] = Pens.LimeGreen;
            colorPens[69] = Pens.Maroon;
            colorPens[70] = Pens.MediumAquamarine;
            colorPens[71] = Pens.Orange;
 
 
            const double xLBound = -1.0;
            const double xUBound = -0.5;
            const double yLBound = -0.50;
            const double yUBound = 0.50;
            const int maxIter = 50;
 
            Complex TopLeft = new Complex();
            TopLeft.a = xLBound;
            TopLeft.b = yUBound;
 
            Complex BottomRight = new Complex();
            BottomRight.a = xUBound;
            BottomRight.b = yLBound;
 
            int xPixels = this.ClientSize.Width;
            int yPixels = this.ClientSize.Height;
 
            double xDelta = (BottomRight.a - TopLeft.a) / xPixels;
            double yDelta = (TopLeft.b - BottomRight.b) / yPixels;
 
            Complex current = TopLeft;
            bool inSet;
 
            for (int x = 0; x < xPixels; x++)
            {
                for (int y = 0; y < yPixels; y++)
                {
                    inSet = true;
                    Complex complexTest = current;
 
                    int numIter = 0;
                    for (int iterations = 1; iterations <= maxIter; iterations++)
                    {
                        Complex cs = ComplexMath.Square(complexTest);
                        Complex cf = ComplexMath.Sum(cs, current);
 
                        if ( (cf.a * cf.a) + (cf.b * cf.b) > 4 )
                        {
                            inSet = false;
                            if (iterations < colorPens.Length)
                            {
                                numIter = iterations;
                            }
                            else
                            {
                                numIter = colorPens.Length;
                            }
                            break;
                        }
                        complexTest = cf;
                    }
 
                    if (inSet)
                    {
                        gScreen.DrawLine(p, x, y, x, y + 1);
                        gBitmap.DrawLine(p, x, y, x, y + 1);
                    }
                    else
                    {
                        gScreen.DrawLine(colorPens[numIter-1], x, y, x, y + 1);
                        gBitmap.DrawLine(colorPens[numIter-1], x, y, x, y + 1);
                    }
                    current.b -= yDelta;
                }
                current.b = TopLeft.b;
                current.a += xDelta;
            }
        }