Kanako's Programming



  

正9角形を描くプログラム(C#のソース)





using System;
using System.IO;
using System.Drawing;
using System.Windows;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Data;
using System.Collections;

     class nonagondata : Form{
         public static void Main(){
                Application.EnableVisualStyles();
                Application.Run(new nonagondata());
         }
        
         private Point[] p;
         private Dictionary<int,string[]> solddic;
         private Dictionary<int,string[]> brokendic;
         private Dictionary<int,string[]> daubdic;

         public nonagondata(){
                p = new Point[47];

        	FileStream data = new FileStream("nonagondata.txt",FileMode.Open);
        	StreamReader r = new StreamReader(data);
        	string P_data;
        	DataTable dt = new DataTable();
        	while((P_data = r.ReadLine()) != null){
                	string[] arr = P_data.Split(',');
                 	string arr1 = String.Copy(arr[1]);
                 	string arr2 = String.Copy(arr[2]);

                 	arr1 = conv_math(arr1);
                 	arr1 = conv_x(arr1);
                 	Object arr3 = dt.Compute(arr1,"");

                 	arr2 = conv_math(arr2);
                 	arr2 = conv_x(arr2);
                	Object arr4 = dt.Compute(arr2,"");

                 	p[int.Parse(arr[0])] = new Point(Convert.ToInt32(arr3),400-Convert.ToInt32(arr4));
        	}
        	FileStream solddata = new FileStream("nonagondata-sold.txt",FileMode.Open);
        	StreamReader soldr = new StreamReader(solddata);
        	string Sold_data;
        	solddic = new Dictionary<int,string[]>();
       	 	while((Sold_data = soldr.ReadLine())!=null){
              		string[] arrsold = Sold_data.Split(',');
              		int firsts = int.Parse(arrsold[0]);
              		Array.Clear(arrsold,0,1);
              		solddic.Add(firsts,arrsold);
        	}
       		FileStream brokendata = new FileStream("nonagondata-broken.txt",FileMode.Open);
       		StreamReader brokenr = new StreamReader(brokendata);
       		string broken_data;
       		brokendic = new Dictionary<int,string[]>();
       		while((broken_data = brokenr.ReadLine())!=null){
              		string[] arrbroken = broken_data.Split(',');
              		int firstb = int.Parse(arrbroken[0]);
              		Array.Clear(arrbroken,0,1);
              		brokendic.Add(firstb,arrbroken);
              	}
      		FileStream daubdata = new FileStream("nonagondata-daub.txt",FileMode.Open);
       		StreamReader daubr = new StreamReader(daubdata);
       		string daub_data;
       		daubdic = new Dictionary<int,string[]>();
       		while((daub_data = daubr.ReadLine())!=null){
              		string[] arrdaub = daub_data.Split(',');
              		int firstd = int.Parse(arrdaub[0]);
              		Array.Clear(arrdaub,0,1);
              		daubdic.Add(firstd,arrdaub);
              	}
          	Paint += new PaintEventHandler(nonagondata_Paint);
     	}
     
     	static string conv_math(string xy){
            Regex regex = new Regex(@"Math.Sqrt\(([0-9]+)\)");
            var mat = new Regex(@"Math.Sqrt\(([0-9]+)\)");
            if (regex.IsMatch(xy)){
                  for (Match m = regex.Match(xy); m.Success; m =
                            m.NextMatch()){
                           int x2 = int.Parse(m.Groups[1].Value);
                           double x1 = Math.Sqrt(x2);
                           xy = xy.Replace(m.Value,x1.ToString());
                  }
            }
            return xy;
     	}
 
     	static string conv_x(string xy){
            Regex regex = new Regex(@"(sin|cos|tan)\([0-9]{1,3}\)");
            if (regex.IsMatch(xy)){
                  for (Match m = regex.Match(xy); m.Success; m = m.NextMatch()){
                        string math = m.Value;
                        string value = Regex.Replace(math,@"[^\d]","");
                        if(math.Contains("tan")){
                          	double num1 = double.Parse(value);
                          	double num2 = Math.Tan(Math.PI/180*num1);
                          	xy = xy.Replace(m.Value,num2.ToString());
                   	}else if(math.Contains("cos")){
                          	double num1 = double.Parse(value);
                          	double num2 = Math.Cos(Math.PI/180*num1);
                          	xy = xy.Replace(m.Value,num2.ToString());
                   	}else if(math.Contains("sin")){
                          	double num1 = double.Parse(value);
                          	double num2 = Math.Sin(Math.PI/180*num1);
                          	xy = xy.Replace(m.Value,num2.ToString());
                   	}
            	  }
    	     }
    	     return xy;
	}

	void nonagondata_Paint(object sender,PaintEventArgs e){
     		this.Size = new Size(500,500);
     		int time = 500;
     		Pen blackPen = new Pen (Color.Black,5);
     		SolidBrush Yellow = new SolidBrush(Color.Yellow);
     		Pen RedPen = new Pen(Color.Red,3);
     		RedPen.DashStyle = DashStyle.DashDot;

     		Graphics g = this.CreateGraphics();

     		for (int i=0; i<solddic.Count; ++i){
             		g.Clear(Color.White);
             		if(!(brokendic[i][1]).Equals("N")){
                  		for (int bj=1; bj<brokendic[i].Length-1; bj=bj+2){
                       			int bx = int.Parse(brokendic[i][bj]);
                       			int by = int.Parse(brokendic[i][bj+1]);
                       			g.DrawLine(RedPen,p[bx],p[by]);
                  		}
             		}
             		for(int sj=1;sj<solddic[i].Length-1; sj=sj+2){
                 		int sx = int.Parse(solddic[i][sj]);
                 		int sy = int.Parse(solddic[i][sj+1]);
                 		g.DrawLine(blackPen,p[sx],p[sy]);
            		}
             		if(!(daubdic[i][1]).Equals("N")){
                  		Point[] dpoints = new Point[daubdic[i].Length];
                  		for (int dj=1; dj<daubdic[i].Length; ++dj){
                            		int d = int.Parse(daubdic[i][dj]);
                            		dpoints[dj] = new Point(p[d].X,p[d].Y);
                  		}
                  		g.FillPolygon(Yellow,dpoints,FillMode.Winding);
             		}
             		System.Threading.Thread.Sleep(time);
       		}
       		blackPen.Dispose();
      		RedPen.Dispose();
      		Yellow.Dispose();
       		Application.Exit();
 	}               
}

BACK(Kanako Suto's Pageへ)
このページへの質問・コメントを歓迎致します。