private void saveFileToolStripMenuItem_Click(object sender, EventArgs e) { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; saveFileDialog.Title = "Save an Output File"; // if (saveVertices.Count != 0) { this.dataNumber++; Voronoi voronoi = new Voronoi(saveVertices.Count); List <Vertex> sortedVertices = saveVertices.OrderBy(o => o.x).ToList(); sortedVertices = voronoi.y_sorting(sortedVertices); for (int i = 0; i < sortedVertices.Count; i++) { voronoi.setVertex(sortedVertices[i].x, sortedVertices[i].y); } this.voronoiList.Add(voronoi); } if (saveFileDialog.ShowDialog() == DialogResult.OK) { System.IO.StreamWriter writer = new System.IO.StreamWriter(saveFileDialog.OpenFile()); for (int j = 0; j < voronoiList[dataNumber].Num; j++) { writer.WriteLine("P " + voronoiList[dataNumber].getVertex(j).x + " " + voronoiList[dataNumber].getVertex(j).y); } if (voronoiList[dataNumber].HPs.Count != 0) { for (int j = 0; j < voronoiList[dataNumber].HPs.Count; j++) { if (voronoiList[dataNumber].HPs[j].is_line) { writer.WriteLine("E " + voronoiList[dataNumber].HPs[j].start_vertex.x + " " + voronoiList[dataNumber].HPs[j].start_vertex.y + " " + voronoiList[dataNumber].HPs[j].end_vertex.x + " " + voronoiList[dataNumber].HPs[j].end_vertex.y); } if (voronoiList[dataNumber].HPs[j].start_vertex.infinity && voronoiList[dataNumber].HPs[j].end_vertex.infinity) { continue; } writer.WriteLine("E " + voronoiList[dataNumber].HPs[j].start_vertex.x + " " + voronoiList[dataNumber].HPs[j].start_vertex.y + " " + voronoiList[dataNumber].HPs[j].end_vertex.x + " " + voronoiList[dataNumber].HPs[j].end_vertex.y); } } writer.Dispose(); writer.Close(); } canvas.Clear(Color.White); saveVertices.Clear(); voronoiList.Clear(); Es.Clear(); Ps.Clear(); }
private void openFileToolStripMenuItem_Click(object sender, EventArgs e) { this.toolStripButton1.Enabled = false; canvas.Clear(Color.White); voronoiList.Clear(); saveVertices.Clear(); Ps.Clear(); Es.Clear(); this.dataNumber = -1; OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; dialog.Title = "選擇檔案"; dialog.InitialDirectory = ".\\"; if (dialog.ShowDialog() != DialogResult.OK) { return; } is_open_file = true; int index = 0; string line; //encoding.default避免中文亂碼 System.IO.StreamReader file = new System.IO.StreamReader(dialog.FileName, Encoding.Default); while ((line = file.ReadLine()) != null) { index++; if (line.Contains('#')) { continue; } else { int num = 0; string[] words = line.Split(' '); Voronoi voronoi; if (words[0] == "0") { break; } else if (words[0] != "" && words.Length == 1 && words[0] != "P" && words[0] != "E") { num = Int32.Parse(words[0]); voronoi = new Voronoi(num); for (int i = 0; i < num; i++) { line = file.ReadLine(); string[] points = line.Split(); voronoi.setVertex(Int32.Parse(points[0]), Int32.Parse(points[1])); } this.voronoiList.Add(voronoi); } else if (words[0] == "P") { Vertex tmp_v = new Vertex(); tmp_v.x = Int32.Parse(words[1]); tmp_v.y = Int32.Parse(words[2]); Ps.Add(tmp_v); } else if (words[0] == "E") { Edge tmp_edge = new Edge(); tmp_edge.start_vertex.x = Int32.Parse(words[1]); tmp_edge.start_vertex.y = Int32.Parse(words[2]); tmp_edge.end_vertex.x = Int32.Parse(words[3]); tmp_edge.end_vertex.y = Int32.Parse(words[4]); Es.Add(tmp_edge); } } } this.dataNumber++; //自己點的檔案格式 if (Ps.Count != 0) { Voronoi voronoi = new Voronoi(Ps.Count); for (int i = 0; i < Ps.Count; i++) { voronoi.setVertex(Ps[i].x, Ps[i].y); canvas.FillRectangle(brush, Ps[i].x, Ps[i].y, 2, 2); } voronoiList.Add(voronoi); } else { for (int i = 0; i < voronoiList[dataNumber].Num; i++) { canvas.FillRectangle(brush, voronoiList[dataNumber].getVertex(i).x, voronoiList[dataNumber].getVertex(i).y, 2, 2); } } this.voronoiList[dataNumber].sort(); //若有邊要畫 if (Es.Count != 0) { for (int i = 0; i < Es.Count; i++) { this.canvas.DrawLine(pen, Es[i].start_vertex.x, Es[i].start_vertex.y, Es[i].end_vertex.x, Es[i].end_vertex.y); } } //MessageBox.Show(index.ToString()); this.toolStripStatusLabel1.Text = "Number " + (dataNumber + 1) + " of Data"; if (voronoiList.Count > 1) { this.toolStripButton1.Enabled = true; } input_palette(); this.toolStripButton2.Enabled = true; this.toolStripButton3.Enabled = true; this.pictureBox1.MouseClick -= pictureBox1_MouseClick; file.Dispose(); file.Close(); }