/// <summary> /// 已知两条直线的ABC,返回其交点坐标 /// </summary> /// <param name="zhiXian1"></param> /// <param name="zhiXian2"></param> /// <returns></returns> public static Point GetIntersectPointofLines(ABC zhiXian1, ABC zhiXian2) { Point result = null; double m = zhiXian1.A * zhiXian2.B - zhiXian2.A * zhiXian1.B; if (m == 0) { return(result); } result = new Point() { X = (zhiXian2.C * zhiXian1.B - zhiXian1.C * zhiXian2.B) / m, Y = (zhiXian1.C * zhiXian2.A - zhiXian2.C * zhiXian1.A) / m }; return(result); }
static void Main(string[] args) { List <ZhuangHaoInfo> zhuangHaoList = new List <ZhuangHaoInfo>(); // 查询数据库获取桩号数据 using (SqlConnection connection = new SqlConnection(Program.ConnectionString)) { string cmdText = "SELECT ID, ZhuangHao, MID_Y, MID_X, L_Y, L_X, R_Y, R_X FROM ZhuangHaoInfo WHERE CengHao = 0;"; SqlCommand command = new SqlCommand(cmdText, connection); connection.Open(); var reader = command.ExecuteReader(CommandBehavior.CloseConnection); while (reader.Read()) { ZhuangHaoInfo hao = new ZhuangHaoInfo() { ID = Convert.ToInt32(reader["ID"]), ZhuangHao = reader["ZhuangHao"].ToString(), MID_Y = Convert.ToDouble(reader["MID_Y"]), MID_X = Convert.ToDouble(reader["MID_X"]), L_Y = Convert.ToDouble(reader["L_Y"]), L_X = Convert.ToDouble(reader["L_X"]), R_Y = Convert.ToDouble(reader["R_Y"]), R_X = Convert.ToDouble(reader["R_X"]) }; zhuangHaoList.Add(hao); } } // 升序 var query = zhuangHaoList.OrderBy(e => e, new ZhuangHaoInfoComp()); // 右顶点、中间顶点、左顶点 Point rightTop, midTop, leftTop; // 右下点、中间底点、左下点 Point rightBottom, midBottom, leftBottom; // 两个三等分点(上面的点、下面的点) Point first_SanFen, second_SanFen; rightTop = midTop = leftTop = null; rightBottom = midBottom = leftBottom = null; // 线段 Segment segment = null; for (int i = 0; i < query.Count() - 1; i++) { ZhuangHaoInfo zhuangTop = query.ElementAt(i); ZhuangHaoInfo zhuangBottom = query.ElementAt(i + 1); rightTop = new Point() { X = zhuangTop.R_X, Y = zhuangTop.R_Y }; midTop = new Point() { X = zhuangTop.MID_X, Y = zhuangTop.MID_Y }; leftTop = new Point() { X = zhuangTop.L_X, Y = zhuangTop.L_Y }; rightBottom = new Point() { X = zhuangBottom.R_X, Y = zhuangBottom.R_Y }; midBottom = new Point() { X = zhuangBottom.MID_X, Y = zhuangBottom.MID_Y }; leftBottom = new Point() { X = zhuangBottom.L_X, Y = zhuangBottom.L_Y }; // 计算两个三等分点坐标 segment = new Segment { start = midTop, end = midBottom }; SanDengFen(segment, out first_SanFen, out second_SanFen); // 计算两条垂线 segment = new Segment { start = midTop, end = second_SanFen }; ABC first_ChuiXian = ABC_ZhongChuiXian(segment); segment = new Segment { start = first_SanFen, end = midBottom }; ABC second_ChuiXian = ABC_ZhongChuiXian(segment); // 计算两条边线 ABC rightLine = ABC_ZhiXian(rightTop, rightBottom); ABC leftLine = ABC_ZhiXian(leftTop, leftBottom); // 计算垂线与边线交点 Point first_ChuiXian_right = GetIntersectPointofLines(rightLine, first_ChuiXian); // 第一条垂线与右边线的交点 Point first_ChuiXian_left = GetIntersectPointofLines(leftLine, first_ChuiXian); // 第一条垂线与左边线的交点 Point second_ChuiXian_right = GetIntersectPointofLines(rightLine, second_ChuiXian); // 第二条垂线与右边线的交点 Point second_ChuiXian_left = GetIntersectPointofLines(leftLine, second_ChuiXian); // 第二条垂线与右边线的交点 // 分为三个监控区域 JianKongQuYu[] yus = new JianKongQuYu[] { new JianKongQuYu { ZhuangHaoID_Start = zhuangTop.ID, ZhuangHaoID_End = zhuangBottom.ID, XuHao = 1, SHAPE2 = Format2POLYGON(leftTop, midTop, rightTop, first_ChuiXian_right, first_ChuiXian_left, leftTop) }, new JianKongQuYu { ZhuangHaoID_Start = zhuangTop.ID, ZhuangHaoID_End = zhuangBottom.ID, XuHao = 2, SHAPE2 = Format2POLYGON(first_ChuiXian_left, first_ChuiXian_right, second_ChuiXian_right, second_ChuiXian_left, first_ChuiXian_left) }, new JianKongQuYu { ZhuangHaoID_Start = zhuangTop.ID, ZhuangHaoID_End = zhuangBottom.ID, XuHao = 3, SHAPE2 = Format2POLYGON(second_ChuiXian_left, second_ChuiXian_right, rightBottom, midBottom, leftBottom, second_ChuiXian_left) } }; using (SqlConnection connection = new SqlConnection(ConnectionString)) { string cmdText = @"INSERT INTO JianKongQuYuGeo (ZhuangHaoID_Start, ZhuangHaoID_End, XuHao, SHAPE2) VALUES (@ZhuangHaoID_Start, @ZhuangHaoID_End, @XuHao, @SHAPE2);"; SqlCommand command = new SqlCommand(cmdText, connection); command.Parameters.Add("@ZhuangHaoID_Start", SqlDbType.Int); command.Parameters.Add("@ZhuangHaoID_End", SqlDbType.Int); command.Parameters.Add("@XuHao", SqlDbType.Int); command.Parameters.Add("@SHAPE2", SqlDbType.NVarChar); connection.Open(); foreach (JianKongQuYu item in yus) { command.Parameters["@ZhuangHaoID_Start"].Value = item.ZhuangHaoID_Start; command.Parameters["@ZhuangHaoID_End"].Value = item.ZhuangHaoID_End; command.Parameters["@XuHao"].Value = item.XuHao; command.Parameters["@SHAPE2"].Value = item.SHAPE2; int r = command.ExecuteNonQuery(); Console.WriteLine($"向数据库表中插入{r}行数据。桩号:{zhuangTop.ZhuangHao} - {zhuangBottom.ZhuangHao},序号:{item.XuHao}。"); } } } }