📄 form.cs
字号:
using (StreamReader sr = new StreamReader(dlg.OpenFile())) { String line; bool first = true; while ((line = sr.ReadLine()) != null) { line.Trim(); if (line.Length > 0 && line[0] != '#') { ExamplePoint node = new ExamplePoint(); if (node.FromString(line)) { if (first) { demo_list.Clear(); // new list replaces the old first = false; } demo_list.Add(node); } } } sr.Close(); points_comboBox.Items.Clear(); for (int i=0; i<demo_list.Count; i++) { //MessageBox.Show(((ExamplePoint)demo_list[i]).ToString(), String.Format("demo: {0}", i)); points_comboBox.Items.Add(((ExamplePoint)demo_list[i]).ToString()); } points_comboBox.SelectedIndex = 0; if (connect_button.Enabled == false) { go_stop_button.Enabled = true; } } } } private void go_stop_button_Click(object sender, System.EventArgs e) { if (bDemoMode) { go_stop_button.Text = "Go"; bDemoMode = false; } else { go_stop_button.Text = "Stop"; bDemoMode = true; if (!bDrawing && thread != null) { thread.Join(); bDrawing = true; ThreadStart threadProc = new ThreadStart(work_thread); thread = new Thread(threadProc); thread.Start(); rBox = new Rectangle(0, 0, 0, 0); outputBox.Invalidate(); } } } private void points_comboBox_SelectedValueChanged(object sender, System.EventArgs e) { if (!bDrawing && thread != null) { thread.Join(); ExamplePoint p = new ExamplePoint(); if (p.FromString(points_comboBox.SelectedItem.ToString())) { xmin = p.xmin; xmax = p.xmax; ymin = p.ymin; ymax = p.ymax; nMax = p.max_iter; if (p.name != null && p.name != "") Text = "Mandelbrot Viewer - " + p.name; else Text = "Mandelbrot Viewer"; bDrawing = true; ThreadStart threadProc = new ThreadStart(work_thread); thread = new Thread(threadProc); thread.Start(); rBox = new Rectangle(0, 0, 0, 0); outputBox.Invalidate(); } } } private void points_comboBox_DropDown(object sender, System.EventArgs e) { ComboBox senderComboBox = (ComboBox)sender; int width = senderComboBox.DropDownWidth; Graphics g = senderComboBox.CreateGraphics(); Font font = senderComboBox.Font; int vertScrollBarWidth = 0; if (senderComboBox.Items.Count>senderComboBox.MaxDropDownItems) vertScrollBarWidth = SystemInformation.VerticalScrollBarWidth; int newWidth; foreach (string s in ((ComboBox)sender).Items) { newWidth = (int) g.MeasureString(s, font).Width + vertScrollBarWidth; if (width < newWidth ) { width = newWidth; } } senderComboBox.DropDownWidth = width; } } public class ExamplePoint { public double xmin; public double ymin; public double xmax; public double ymax; public int max_iter; public string name; public ExamplePoint() { xmin = -1; xmax = 1; ymin = -1; ymax = 1; max_iter = 100; name = null; } public ExamplePoint(double x0, double y0, double x1, double y1, int m) { xmin = x0; ymin = y0; xmax = x1; ymax = y1; max_iter = m; name = null; } public ExamplePoint(double x0, double y0, double x1, double y1, int m, string n) { xmin = x0; ymin = y0; xmax = x1; ymax = y1; max_iter = m; name = n; } public ExamplePoint(string s) { xmin = -1; xmax = 1; ymin = -1; ymax = 1; max_iter = 100; name = null; FromString(s); } public bool FromString(string s) { double xcenter = 0; double ycenter = 0; double radius = 0; xmin = -2; xmax = 2; ymin = -2; ymax = 2; max_iter = 100; name = null; bool bxmin=false, bxmax=false, bymin=false, bymax=false; int index = 0; string [] ss; int n_range = 0; StringArgParser p = new StringArgParser(); ss = p.ParseStrings(s); if (ss != null && ss.Length > 0) { while (index < ss.Length) { string str; str = ss[index]; str.Trim(); index++; if (str.Length > 1 && str[0] == '(' && str[str.Length-1] == ')') { double x,y; string inside = str.Substring(1, str.Length-2); if (inside != null && inside.Length > 0) { string [] range_strings; range_strings = inside.Split(','); if (range_strings != null && range_strings.Length == 2) { x = Convert.ToDouble(range_strings[0]); y = Convert.ToDouble(range_strings[1]); if (n_range == 0) { xmin = x; ymin = y; bxmin = bymin = true; } else if (n_range == 1) { xmax = x; ymax = y; bxmax = bymax = true; } n_range++; } } } else if (str == "-rmin") { if (index < ss.Length) { xmin = Convert.ToDouble(ss[index]); bxmin = true; index++; } } else if (str == "-rmax") { if (index < ss.Length) { xmax = Convert.ToDouble(ss[index]); bxmax = true; index++; } } else if (str == "-imin") { if (index < ss.Length) { ymin = Convert.ToDouble(ss[index]); bymin = true; index++; } } else if (str == "-imax") { if (index < ss.Length) { ymax = Convert.ToDouble(ss[index]); bymax = true; index++; } } else if (str == "-rcenter") { if (index < ss.Length) { xcenter = Convert.ToDouble(ss[index]); index++; } } else if (str == "-icenter") { if (index < ss.Length) { ycenter = Convert.ToDouble(ss[index]); index++; } } else if (str == "-radius") { if (index < ss.Length) { radius = Convert.ToDouble(ss[index]); index++; } } else if (str == "max_iter" || str == "-max_iter" || str == "-maxiter") { if (index < ss.Length) { max_iter = Convert.ToInt32(ss[index]); index++; } } else if (str == "name" || str == "-name") { if (index < ss.Length) { name = ss[index]; index++; } } } } if (radius != 0) { xmin = xcenter - radius; xmax = xcenter + radius; ymin = ycenter - radius; ymax = ycenter + radius; bxmin = bymin = bxmax = bymax = true; } if (xmin > xmax) { double d; d = xmin; xmin = xmax; xmax = d; } if (ymin > ymax) { double d; d = ymin; ymin = ymax; ymax = d; } if (xmin < -2) xmin = -2; if (xmax > 2) xmax = 2; if (ymin < -2) ymin = -2; if (ymax > 2) ymax = 2; if (max_iter < 1) max_iter = 100; if (max_iter > 10000) max_iter = 100; if (bxmin == true && bymin == true && bxmax == true && bymax == true) return true; return false; } public override string ToString() { if (name == null) { return String.Format("({0},{1}) ({2},{3}) max_iter {4}", xmin, ymin, xmax, ymax, max_iter); } else { return String.Format("({0},{1}) ({2},{3}) max_iter {4} name \"{5}\"", xmin, ymin, xmax, ymax, max_iter, name); } } public string ToShortString() { if (name == null) { return String.Format("({0},{1}) ({2},{3}) max_iter {4}", xmin.ToString("#.##E0"), ymin.ToString("#.##E0"), xmax.ToString("#.##E0"), ymax.ToString("#.##E0"), max_iter); } else { return String.Format("({0},{1}) ({2},{3}) max_iter {4} name \"{5}\"", xmin.ToString("#.##E0"), ymin.ToString("#.##E0"), xmax.ToString("#.##E0"), ymax.ToString("#.##E0"), max_iter, name); } } } public class ColorRainbow { static public Color getColor(double fraction, double intensity) { /* fraction is a part of the rainbow (0.0 - 1.0) = (Red-Yellow-Green-Cyan-Blue-Magenta-Red) intensity (0.0 - 1.0) 0 = black, 1 = full color, 2 = white */ double red, green, blue; int r,g,b; double dtemp; //fraction = Math.Abs(modf(fraction, &dtemp)); fraction = Math.Abs(fraction - Math.Floor(fraction)); if (intensity > 2.0) intensity = 2.0; if (intensity < 0.0) intensity = 0.0; dtemp = 1.0/6.0; if (fraction < 1.0/6.0) { red = 1.0; green = fraction / dtemp; blue = 0.0; } else { if (fraction < 1.0/3.0) { red = 1.0 - ((fraction - dtemp) / dtemp); green = 1.0; blue = 0.0; } else { if (fraction < 0.5) { red = 0.0; green = 1.0; blue = (fraction - (dtemp*2.0)) / dtemp; } else { if (fraction < 2.0/3.0) { red = 0.0; green = 1.0 - ((fraction - (dtemp*3.0)) / dtemp); blue = 1.0; } else { if (fraction < 5.0/6.0) { red = (fraction - (dtemp*4.0)) / dtemp; green = 0.0; blue = 1.0; } else { red = 1.0; green = 0.0; blue = 1.0 - ((fraction - (dtemp*5.0)) / dtemp); } } } } } if (intensity > 1) { intensity = intensity - 1.0; red = red + ((1.0 - red) * intensity); green = green + ((1.0 - green) * intensity); blue = blue + ((1.0 - blue) * intensity); } else { red = red * intensity; green = green * intensity; blue = blue * intensity; } r = (int)(red * 255.0); g = (int)(green * 255.0); b = (int)(blue * 255.0); return Color.FromArgb(r,g,b); } public static void Make_color_array(int num_colors, Color[] colors) { double fraction, intensity; int i; int max; max = colors.Length; intensity = 1.0; for (i=0; i<max; i++) { fraction = (double)(i % num_colors) / (double)num_colors; colors[i] = getColor(fraction, intensity); } } } public class StringArgParser { public string [] ParseStrings(string str) { string [] stemp; string [] return_strings; StringCollection s = new StringCollection(); if (str.Length < 1) return null; while (str != null) { stemp = ParseString(str); if (stemp != null) { s.Add(stemp[0]); str = stemp[1]; } else { str = null; } } return_strings = new string[s.Count]; for (int i=0; i<s.Count; i++) return_strings[i] = s[i]; return return_strings; } string [] ParseString(string str) { int index = 0; StringBuilder s = new StringBuilder(); String s2; string [] return_string; if (str == null) return null; while (index < str.Length && str[index] == ' ') index++; if (index == str.Length) return null; if (str[index] == '"') { // parse quoted index++; // step over the first quote character while (index < str.Length && str[index] != '"') { s.Append(str[index]); index++; } if (index < str.Length) index++; // step over the second quote } else { // parse literal while (index < str.Length && str[index] != ' ') { s.Append(str[index]); index++; } } while (index < str.Length && str[index] == ' ') index++; if (index < str.Length) s2 = str.Substring(index, str.Length - index); else s2 = null; return_string = new string[2]; return_string[0] = s.ToString(); return_string[1] = s2; return return_string; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -