예제 #1
0
        private bool isVisible(MyPoint s, MyPoint ci, MyPointDouble ni)
        {
            MyPointDouble top = new MyPointDouble();

            GetDirection(ref top, ci.x, ci.y, s.x, s.y);
            if (ScalarPro(top, ni) >= 0)
            {
                return(true);
            }
            return(false);
        }
예제 #2
0
 private void GetDirection(ref MyPointDouble v, double xb, double yb, double xe, double ye)
 {
     v.x = xe - xb;
     v.y = ye - yb;
 }
예제 #3
0
 private double ScalarPro(MyPointDouble u, MyPointDouble v)
 {
     return(u.x * v.x + u.y * v.y);
 }
예제 #4
0
        private void Execute_Click(object sender, EventArgs e)
        {
            int ka = RectList.Items.Count;
            int kc = ConfigList.Items.Count + 1;

            if (isNotConvex())
            {
                if (MessageBox.Show("Please input the convex configuration!", "Warning", MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Retry)
                {
                    clearC_Click(sender, e);
                }
                else
                {
                    Close();
                }
                return;
            }
            if (ka < 3 || kc < 4)
            {
                MessageBox.Show("Configuration does not have enough points!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            List <MyPoint> a  = new List <MyPoint>();
            List <MyPoint> b  = new List <MyPoint>();
            List <MyPoint> c  = new List <MyPoint>();
            int            Nb = 1;

            foreach (MyPoint tmp in RectList.Items)
            {
                a.Add(tmp);
            }
            foreach (MyPoint tmp in ConfigList.Items)
            {
                c.Add(tmp);
            }
            c.Add((MyPoint)ConfigList.Items[0]);
            int     i, j;
            MyPoint s, f, it;
            bool    err;

            for (i = 0; i < kc - 1 && Nb != 0; i++)
            {
                Nb = 0;
                MyPointDouble vi  = new MyPointDouble();
                MyPointDouble vi1 = new MyPointDouble();
                GetDirection(ref vi, c[i].x, c[i].y, c[i + 1].x, c[i + 1].y);
                int i2 = i + 2;
                if (i2 > kc - 1)
                {
                    i2 = 1;
                }
                GetDirection(ref vi1, c[i].x, c[i].y, c[i2].x, c[i2].y);
                MyPointDouble nvn = new MyPointDouble();
                nvn.y = 1;
                if (vi.x != 0)
                {
                    nvn.x = -vi.y / vi.x;
                }
                else
                {
                    nvn.y = 0;
                    nvn.x = 1;
                }
                if (ScalarPro(nvn, vi1) < 0)
                {
                    nvn.y *= -1;
                    nvn.x *= -1;
                }

                f = a[0];
                s = a[0];
                if (isVisible(s, c[i + 1], nvn))
                {
                    b.Add(s);
                    Nb++;
                }

                for (j = 1; j < ka; j++)
                {
                    it  = new MyPoint();
                    err = findpoint(s, a[j], c[i], c[i + 1], ref it);
                    if (!err)
                    {
                        b.Add(it);
                        Nb++;
                    }
                    s = a[j];
                    if (isVisible(s, c[i + 1], nvn))
                    {
                        b.Add(s);
                        Nb++;
                    }
                }
                if (Nb == 0)
                {
                    break;
                }
                it  = new MyPoint();
                err = findpoint(s, f, c[i], c[i + 1], ref it);
                if (!err)
                {
                    b.Add(it);
                    Nb++;
                }
                a.Clear();
                foreach (MyPoint tmp in b)
                {
                    a.Add(tmp);
                }
                ka = Nb;
                b.Clear();
            }
            //MessageBox.Show(ka.ToString());
            if (ka != 0)
            {
                //MessageBox.Show(a[0].x.ToString() + " " + a[0].y.ToString());
                a.Add(a[0]);
                Graphics g     = Graphics.FromImage(view.Image);
                Pen      myPen = new Pen(Color.Red, 2);
                for (i = 0; i < Nb; i++)
                {
                    g.DrawLine(myPen, a[i].x, a[i].y, a[i + 1].x, a[i + 1].y);
                }
            }
            view.Refresh();
        }