-
Notifications
You must be signed in to change notification settings - Fork 3
/
Form1.cs
169 lines (140 loc) · 5.53 KB
/
Form1.cs
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Mandelbrot
{
public partial class Form1 : Form
{
private const int WHEEL_DELTA = 120;
private const double ZOOM_FACTOR_IN = 0.8;
private const double ZOOM_FACTOR_OUT = 1.2;
private ControlForm controlForm;
public List<MandelbrotRendererBase> rendererList { get; private set; }
private MandelbrotRendererBase currentRenderer;
private UInt32[] bmpBits;
private GCHandle gcHandle;
private Bitmap bmp = null;
private bool moving = false;
private int moveX0, moveY0;
public Form1()
{
InitializeComponent();
Text = Application.ProductName;
bmpBits = new UInt32[0];
int colorPaletteSize = 1024;
//UInt32[] colorPalette = ColorScheme.CreateColorScheme(new UInt32[] { 0xffff0000, 0xff00ff00, 0xff0000ff, 0xffff00ff, 0xffff0000 }, colorPaletteSize);
UInt32[] colorPalette = ColorScheme.CreateColorScheme(new Color[] { Color.BurlyWood, Color.Chocolate, Color.Tan, Color.Sienna, Color.LightSteelBlue, Color.BurlyWood }, colorPaletteSize);
rendererList = new List<MandelbrotRendererBase>
{
new FloatRenderer.FloatRenderer(this, colorPalette, colorPaletteSize),
//new GMPRenderer.GMPRenderer(this, colorPalette, colorPaletteSize),
new BigIntegerRenderer.BigIntegerRenderer(this, colorPalette, colorPaletteSize),
new SimpleBigIntRenderer.SimpleBigIntRenderer(this, colorPalette, colorPaletteSize)
};
currentRenderer = rendererList[0];
foreach (var r in rendererList)
{
ResetInitialParams(r);
}
this.DoubleBuffered = true;
this.MouseWheel += Form1_MouseWheel;
}
private void Form1_Load(object sender, EventArgs e)
{
controlForm = new ControlForm(this);
controlForm.Show();
Form1_SizeChanged(null, null);
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
currentRenderer.TerminateThreads();
FreeStuff();
controlForm.Close();
}
private void FreeStuff()
{
if (bmp != null) { bmp.Dispose(); bmp = null; }
if (gcHandle.IsAllocated) { gcHandle.Free(); }
}
private void Form1_SizeChanged(object sender, EventArgs e)
{
if (currentRenderer == null)
{
return;
}
currentRenderer.TerminateThreads();
int screenWidth = this.ClientSize.Width;
int screenHeight = this.ClientSize.Height;
if (screenWidth < 1) { screenWidth = 1; }
if (screenHeight < 1) { screenHeight = 1; }
int bufferLength = screenWidth * screenHeight;
if (bufferLength > bmpBits.Length)
{
FreeStuff();
bmpBits = new UInt32[bufferLength * 4];
gcHandle = GCHandle.Alloc(bmpBits, GCHandleType.Pinned);
}
bmp?.Dispose();
bmp = new Bitmap(screenWidth, screenHeight, screenWidth * 4, PixelFormat.Format32bppArgb, gcHandle.AddrOfPinnedObject());
currentRenderer.UpdateBitmapBits(bmpBits);
currentRenderer.UpdateScreenDimensions(screenWidth, screenHeight);
OnParametersChanged();
}
public void SetRenderer(int index)
{
currentRenderer.TerminateThreads();
currentRenderer = rendererList[index];
Form1_SizeChanged(null, null);
}
private void ResetInitialParams(MandelbrotRendererBase renderer)
{
renderer.SetInitialParams(-2.0, -1.2, 3.0);
}
public void ResetInitialParams()
{
ResetInitialParams(currentRenderer);
}
public void OnParametersChanged()
{
currentRenderer.TerminateThreads();
currentRenderer.Draw((int)controlForm.udIterations.Value, (int)controlForm.udNumThreads.Value);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
if (bmp == null) { return; }
e.Graphics.DrawImageUnscaled(bmp, 0, 0);
}
private void timerRefresh_Tick(object sender, EventArgs e)
{
Invalidate();
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
moving = true;
moveX0 = e.X;
moveY0 = e.Y;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
controlForm.txtInfo.Text = currentRenderer.GetCoordinateStr(e.X, e.Y);
if (!moving) { return; }
currentRenderer.Move(e.X - moveX0, e.Y - moveY0);
moveX0 = e.X;
moveY0 = e.Y;
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
moving = false;
}
void Form1_MouseWheel(object sender, MouseEventArgs e)
{
double factor = 1.0;
if (e.Delta > 0) { factor = Math.Pow(ZOOM_FACTOR_IN, (double)e.Delta / (double)WHEEL_DELTA); }
else if (e.Delta < 0) { factor = Math.Pow(ZOOM_FACTOR_OUT, (double)-e.Delta / (double)WHEEL_DELTA); }
currentRenderer.Zoom(e.X, e.Y, factor);
}
}
}