Ejemplo n.º 1
0
        private void btnLoadData_Click(object sender, EventArgs e)
        {
            OpenFileDialog file = new OpenFileDialog();

            file.InitialDirectory = "C:\\Workspace\\作业\\毛";
            if (file.ShowDialog() == DialogResult.OK)
            {
                data = new List <MyPoint>();
                StreamReader sr = File.OpenText(file.FileName);
                while (!sr.EndOfStream)
                {
                    string   line    = sr.ReadLine();
                    string[] sArrays = line.Split(',');
                    if (sArrays.Length == 5)
                    {
                        int    id = -1;
                        string name;
                        double x = 0, y = 0, z = 0;
                        bool   isvalid = false;

                        int.TryParse(sArrays[0], out id);
                        name    = sArrays[1];
                        isvalid = double.TryParse(sArrays[2], out x) &
                                  double.TryParse(sArrays[3], out y) &
                                  double.TryParse(sArrays[4], out z);


                        if (isvalid)
                        {
                            MyPoint tmp = new MyPoint(id, name, new PointD(x, y), z);
                            data.Add(tmp);

                            if (boundary[0] > x)
                            {
                                boundary[0] = x;
                            }
                            if (boundary[1] > y)
                            {
                                boundary[1] = y;
                            }
                            if (boundary[2] < x)
                            {
                                boundary[2] = x;
                            }
                            if (boundary[3] < y)
                            {
                                boundary[3] = y;
                            }
                        }
                    }
                }
                sr.Close();
                scale        = Math.Min(pbMain.Width / (boundary[2] - boundary[0]), pbMain.Height / (boundary[3] - boundary[1])) / 1.05;
                blShowPoints = true;
                offsetX      = boundary[0];
                offsetY      = boundary[1];
                MoveToCenter();
                scale_back   = scale;
                offsetX_back = offsetX;
                offsetY_back = offsetY;
                pbMain.Refresh();
                file.Dispose();
            }
        }
Ejemplo n.º 2
0
        public static bool LineIntersect(MyPoint p1, MyPoint p2, MyPoint q1, MyPoint q2, bool contain_end = false)
        {
            if (p1.Id == q1.Id || p1.Id == q2.Id || p2.Id == q1.Id || p2.Id == q2.Id)
            {
                return(contain_end);
            }

            //排斥试验,判断p1p2在q1q2为对角线的矩形区之外
            if (Math.Max(p1.X, p2.X) < Math.Min(q1.X, q2.X))
            {//P1P2中最大的X比Q1Q2中的最小X还要小,说明P1P2在Q1Q2的最左点的左侧,不可能相交。
                return(false);
            }

            if (Math.Min(p1.X, p2.X) > Math.Max(q1.X, q2.X))
            {//P1P2中最小的X比Q1Q2中的最大X还要大,说明P1P2在Q1Q2的最右点的右侧,不可能相交。
                return(false);
            }

            if (Math.Max(p1.Y, p2.Y) < Math.Min(q1.Y, q2.Y))
            {//P1P2中最大的Y比Q1Q2中的最小Y还要小,说明P1P2在Q1Q2的最低点的下方,不可能相交。
                return(false);
            }

            if (Math.Min(p1.Y, p2.Y) > Math.Max(q1.Y, q2.Y))
            {//P1P2中最小的Y比Q1Q2中的最大Y还要大,说明P1P2在Q1Q2的最高点的上方,不可能相交。
                return(false);
            }

            //跨立试验
            double A, B;

            if (p1.X != p2.X)
            {
                A = (p2.Y - p1.Y) / (p2.X - p1.X);
                B = (p1.Y * p2.X - p2.Y * p1.X) / (p2.X - p1.X);
                double tmp = (q2.Y - A * q2.X - B);
                if ((q1.Y - A * q1.X - B) * (q2.Y - A * q2.X - B) > 0)
                {
                    return(false);
                }
            }
            else
            {
                if ((p1.X - q1.X) * (p1.X - q2.X) > 0)
                {
                    return(false);
                }
            }
            if (q1.X != q2.X)
            {
                A = (q2.Y - q1.Y) / (q2.X - q1.X);
                B = (q1.Y * q2.X - q2.Y * q1.X) / (q2.X - q1.X);
                if ((p1.Y - A * p1.X - B) * (p2.Y - A * p2.X - B) > 0)
                {
                    return(false);
                }
            }
            else
            {
                if ((q1.X - p1.X) * (q1.X - p2.X) > 0)
                {
                    return(false);
                }
            }
            return(true);
        }