C#面积加权,最短距离

C#面积加权,最短距离_第1张图片

string sql9 = "select * from test";                                              //dt 存储Triange表中的原始数据
            OleDbDataAdapter dbDataAdapter9 = new OleDbDataAdapter(sql9, oleDb);
            DataTable dt9 = new DataTable();
            dbDataAdapter9.Fill(dt9);
            DataTable dt0 = new DataTable();
            dt0.Columns.Add("AverageGf"); dt0.Columns.Add("Averagewaf");
            Console.WriteLine(dt9.Rows.Count);

            for (int i = 0; i < 14; i++)
            {
                double sumS = 0; double sumG = 0; double AverageGf = 0; double swaf = 0; double swbf = 0; double Averagewaf = 0; double Averagewbf = 0;
                for (int j = 0; j < 21; j++)
                {
                    if (Convert.ToInt32(dt9.Rows[j]["PID1"]) == i + 1 || Convert.ToInt32(dt9.Rows[j]["PID2"]) == i + 1 || Convert.ToInt32(dt9.Rows[j]["PID3"]) == i + 1)
                    {
                        sumS += Convert.ToDouble(dt9.Rows[j]["s"]);
                        sumG += Convert.ToDouble(dt9.Rows[j]["s"]) * Convert.ToDouble(dt9.Rows[j]["gf"]);
                    }
                    if (Convert.ToInt32(dt9.Rows[j]["PID1"]) == i + 1)
                    {
                        swaf += Convert.ToDouble(dt9.Rows[j]["s"]) * Convert.ToDouble(dt9.Rows[j]["waf1"]);
                        //swbf += Convert.ToDouble(dt9.Rows[j]["s"]) * Convert.ToDouble(dt9.Rows[j]["wbf1"]);
                    }
                    if (Convert.ToInt32(dt9.Rows[j]["PID2"]) == i + 1)
                    {
                        swaf += Convert.ToDouble(dt9.Rows[j]["s"]) * Convert.ToDouble(dt9.Rows[j]["waf2"]);
                        //swbf += Convert.ToDouble(dt9.Rows[j]["s"]) * Convert.ToDouble(dt9.Rows[j]["wbf2"]);
                    }
                    if (Convert.ToInt32(dt9.Rows[j]["PID3"]) == i + 1)
                    {
                        swaf += Convert.ToDouble(dt9.Rows[j]["s"]) * Convert.ToDouble(dt9.Rows[j]["waf3"]);
                        //swbf += Convert.ToDouble(dt9.Rows[j]["s"]) * Convert.ToDouble(dt9.Rows[j]["wbf3"]);
                    }
                }
                AverageGf = sumG / sumS; Averagewaf = swaf / sumS; //Averagewbf = swbf / sumS;
                DataRow dr = dt0.NewRow(); dr["AverageGf"] = AverageGf; dr["Averagewaf"] = Averagewaf; dt0.Rows.Add(dr);
            }


            string result1 = @"F:\vs\000.txt";                   //输出数据
            FileStream fs = new FileStream(result1, FileMode.Append);
            StreamWriter wr = null;
            wr = new StreamWriter(fs);
            wr.WriteLine("PID,AverageGf,Averagewaf");
            for (int i = 0; i < dt0.Rows.Count; i++)
            {
                wr.WriteLine("{0},{1},{2}", i + 1, dt0.Rows[i]["AverageGf"], dt0.Rows[i]["Averagewaf"]);
            }
            wr.Close();

 注:1.每执行一轮循环后,sumS = 0……都要归零

2.需要新建一个表来存储结果,不能附加在原始数据上,因为数据格式不同。

C#面积加权,最短距离_第2张图片

string sql8 = "select * from A";        //dt8---Fracture  存储断裂面上三角点的指标
            OleDbDataAdapter dbDataAdapter8 = new OleDbDataAdapter(sql8, oleDb);
            DataTable dt8 = new DataTable();
            dbDataAdapter8.Fill(dt8);
            List Fracture = new List();            
            
            for (int i = 0; i < dt8.Rows.Count; i++)
            {
                Point3D Fracture_P = new Point3D();  // 需要在循环体内,否则会被覆盖
                Fracture_P.X = Convert.ToDouble(dt8.Rows[i]["X"]);
                Fracture_P.Y = Convert.ToDouble(dt8.Rows[i]["Y"]);
                Fracture_P.Z = Convert.ToDouble(dt8.Rows[i]["Z"]);
                Fracture.Add(Fracture_P);
            }           

            string sql9 = "select * from B";    //dt9---Know  存储代求块体
            OleDbDataAdapter dbDataAdapter9 = new OleDbDataAdapter(sql9, oleDb);
            DataTable dt9 = new DataTable();
            dbDataAdapter9.Fill(dt9);
            dt9.Columns.Add("PID");
            dt9.Columns.Add("df");
            List Know = new List();
            
            for (int i = 0; i < dt9.Rows.Count; i++)
            {
                Point3D Know_P = new Point3D();
                Know_P.X = Convert.ToDouble(dt9.Rows[i]["X"]);
                Know_P.Y = Convert.ToDouble(dt9.Rows[i]["Y"]);
                Know_P.Z = Convert.ToDouble(dt9.Rows[i]["Z"]);
                Know.Add(Know_P);
            }
            Console.WriteLine(Know.Count);


            for (int i = 0; i < Know.Count; i++)
            {
                double temp = 0; double max = double.MaxValue;
                for (int j = 0; j < Fracture.Count; j++)
                {
                    temp = triangle.PP_DistancePow(Know[i], Fracture[j]);  //计算两点间的距离
                    max = max > temp ? temp : max;  //把max往min迁移
                    if (max == temp)
                    {
                        dt9.Rows[i]["PID"] = j + 1;  //存储最短距离值和对应的点号
                        dt9.Rows[i]["df"] = temp;
                    }
                }
            }

            string result1 = @"F:\vs\测试.txt";                   //输出数据
            FileStream fs = new FileStream(result1, FileMode.Append);
            StreamWriter wr = null;
            wr = new StreamWriter(fs);

            wr.WriteLine("ID,PointID,df");
            for (int i = 0; i < dt9.Rows.Count; i++)
            {
                wr.WriteLine("{0},{1},{2}", dt9.Rows[i]["ID"], dt9.Rows[i]["PID"], dt9.Rows[i]["df"]);
            }
            wr.Close();

 

你可能感兴趣的:(c#)