Esempio n. 1
0
        private void area_MouseUp(object sender, MouseEventArgs e)
        {
            Pen p = new Pen(Color.Black);
            Pen r = new Pen(Color.Red);

            if (lineButton.Checked)
            {
                LastPoint = e.Location;
                if (lineOptionsBox.SelectedIndex == 2 && polyPoints.Count > 1)
                {
                    Point A = new Point(CurrentPoint.X, CurrentPoint.Y);
                    Point B = new Point(LastPoint.X, LastPoint.Y);
                    Point C = new Point(this.polyPoints[polyPoints.Count - 2].X, this.polyPoints[polyPoints.Count - 2].Y);
                    Point D = new Point(this.polyPoints[polyPoints.Count - 1].X, this.polyPoints[polyPoints.Count - 1].Y);

                    g.DrawLine(p, A, B);

                    bool f1 = false, f2 = false;
                    if (RightCheck(A, B, C) && !(RightCheck(A, B, D)))
                    {
                        f1 = true;
                    }
                    if (!(RightCheck(A, B, C)) && (RightCheck(A, B, D)))
                    {
                        f1 = true;
                    }

                    if (RightCheck(C, D, A) && !(RightCheck(C, D, B)))
                    {
                        f2 = true;
                    }
                    if (!(RightCheck(C, D, A)) && (RightCheck(C, D, B)))
                    {
                        f2 = true;
                    }

                    if (f1 == true && f2 == true)
                    {
                        label3.Text = "cross";
                    }
                    else
                    {
                        label3.Text = "no cross";
                    }

                    /*
                     * g.DrawLine(p, CurrentPoint, LastPoint);
                     *
                     * double a1 = (CurrentPoint.Y - LastPoint.Y) / (CurrentPoint.X - LastPoint.X);
                     * double a2 = (polyPoints[0].Y - polyPoints[1].Y) / (polyPoints[0].X - polyPoints[1].X);
                     *
                     * double b1 = CurrentPoint.Y - a1 * CurrentPoint.X;
                     * double b2 = polyPoints[0].Y - a2 * polyPoints[0].X;
                     *
                     * int x = (int)((b2 - b1) / (a1 - a2));
                     *
                     *
                     * if (((x < Math.Max(CurrentPoint.X, polyPoints[0].X)) || (x > Math.Min(LastPoint.X, polyPoints[1].X)))
                     || ((x < Math.Max(polyPoints[0].X, CurrentPoint.X)) || (x > Math.Min(polyPoints[1].X, LastPoint.X))))
                     ||{
                     || x = (int)(-((CurrentPoint.X * LastPoint.Y - LastPoint.X * CurrentPoint.Y) * (polyPoints[1].X - polyPoints[0].X) - (polyPoints[0].X * polyPoints[1].Y - polyPoints[1].X * polyPoints[0].Y) * (LastPoint.X - CurrentPoint.X)) / ((CurrentPoint.Y - LastPoint.Y) * (polyPoints[1].X - polyPoints[0].X) - (polyPoints[0].Y - polyPoints[1].Y) * (LastPoint.X - CurrentPoint.X)));
                     || int y = ((polyPoints[0].Y - polyPoints[1].Y) * (-x) - (polyPoints[0].X * polyPoints[1].Y - polyPoints[1].X * polyPoints[0].Y)) / (polyPoints[1].X - polyPoints[0].X);
                     || g.FillEllipse(new SolidBrush(Color.Red), new Rectangle(x, y, 3, 3));
                     ||
                     ||}*/
                    area.Invalidate();
                }
                else if (lineOptionsBox.SelectedIndex == 3 && polyPoints.Count > 1)
                {
                    int ox = this.polyPoints.First().X;
                    int oy = this.polyPoints.First().Y;
                    int xa = this.polyPoints.ElementAt(1).X;
                    int ya = this.polyPoints.ElementAt(1).Y;
                    int xb = this.CurrentPoint.X;
                    int yb = this.CurrentPoint.Y;

                    int oxm = 0;
                    int oym = 0;
                    int xam = xa - ox;
                    int yam = ya - oy;
                    int xbm = xb - ox;
                    int ybm = yb - oy;

                    /*
                     * double sinB = yb / Math.Sqrt((xb - ox) ^ 2 + (yb - oy) ^ 2);
                     * double sinA = ya / Math.Sqrt((xa - ox) ^ 2 + (ya - oy) ^ 2);
                     * double cosA = xa / Math.Sqrt((xa - ox) ^ 2 + (ya - oy) ^ 2);
                     * double cosB = xb / Math.Sqrt((xb - ox) ^ 2 + (yb - oy) ^ 2);*/


                    if ((ybm * xam - xbm * yam) > 0)
                    {
                        g.FillEllipse(new SolidBrush(Color.Blue), new Rectangle(this.CurrentPoint.X, this.CurrentPoint.Y, 3, 3));
                        label3.Text = "right";
                    }
                    else if ((ybm * xam - xbm * yam) < 0)
                    {
                        g.FillEllipse(new SolidBrush(Color.Red), new Rectangle(this.CurrentPoint.X, this.CurrentPoint.Y, 3, 3));
                        label3.Text = "left";
                    }
                    else
                    {
                        label3.Text = "No";
                    }
                    area.Invalidate();
                }

                else if (lineOptionsBox.SelectedIndex == 4 && polyPoints.Count > 1)
                {
                    int ox = this.polyPoints.First().X;
                    int oy = this.polyPoints.First().Y;
                    int xa = this.polyPoints.ElementAt(1).X;
                    int ya = this.polyPoints.ElementAt(1).Y;

                    for (int i = 1; i < area.Width; i += 5)
                    {
                        for (int j = 1; j < area.Height; j += 5)
                        {
                            int xb = i; //-this.CurrentPoint.X;
                            int yb = j; //-this.CurrentPoint.Y;

                            int oxm = 0;
                            int oym = 0;
                            int xam = xa - ox;
                            int yam = ya - oy;
                            int xbm = xb - ox;
                            int ybm = yb - oy;

                            /*
                             * double sinB = yb / Math.Sqrt((xb - ox) ^ 2 + (yb - oy) ^ 2);
                             * double sinA = ya / Math.Sqrt((xa - ox) ^ 2 + (ya - oy) ^ 2);
                             * double cosA = xa / Math.Sqrt((xa - ox) ^ 2 + (ya - oy) ^ 2);
                             * double cosB = xb / Math.Sqrt((xb - ox) ^ 2 + (yb - oy) ^ 2);*/


                            if ((ybm * xam - xbm * yam) > 0)
                            {
                                g.FillEllipse(new SolidBrush(Color.Blue), new Rectangle(i, j, 3, 3));
                                label3.Text = "right";
                            }
                            else if ((ybm * xam - xbm * yam) < 0)
                            {
                                g.FillEllipse(new SolidBrush(Color.Red), new Rectangle(i, j, 3, 3));
                                label3.Text = "left";
                            }
                            else
                            {
                                label3.Text = "No";
                            }
                            area.Invalidate();
                        }
                    }
                }
                else
                {
                    polyPoints.Clear();
                    g.DrawLine(p, CurrentPoint, LastPoint);
                    polyPoints.Add(CurrentPoint);
                    polyPoints.Add(LastPoint);
                    area.Invalidate();
                }
            }
            else if (dotButton.Checked)
            {
                LastPoint = e.Location;
                g.FillEllipse(new SolidBrush(Color.Black), new Rectangle(LastPoint.X, LastPoint.Y, 3, 3));
                area.Invalidate();
            }

            else if (polygonButton.Checked)
            {
                if (polygonOptionsBox.SelectedIndex == 2 && polyPoints.Count > 0)
                {
                    var dialogX = new InputBox("Введите угол");

                    int alpha = 0;
                    if (dialogX.ShowDialog() == DialogResult.Cancel)
                    {
                        return;
                    }
                    if (!int.TryParse(dialogX.ResultText, out alpha))
                    {
                        return;
                    }

                    LastPoint = e.Location;
                    g.DrawLine(new Pen(Color.White), polyPoints[0], polyPoints[polyPoints.Count - 1]);
                    for (int i = 0; i < polyPoints.Count - 1; i++)
                    {
                        g.DrawLine(new Pen(Color.White), polyPoints[i], polyPoints[i + 1]);
                    }

                    for (int i = 0; i < polyPoints.Count; i++)
                    {
                        if (i != polyPoints.Count - 1)
                        {
                            polyPoints[i] = rotate(LastPoint, alpha, polyPoints[i]);
                            g.DrawLine(new Pen(Color.Black), polyPoints[i], rotate(LastPoint, alpha, polyPoints[i + 1]));
                        }
                        else
                        {
                            polyPoints[i] = rotate(LastPoint, alpha, polyPoints[i]);
                            g.DrawLine(new Pen(Color.Black), polyPoints[i], polyPoints[0]);
                        }
                    }
                    area.Invalidate();
                }
                else if (polygonOptionsBox.SelectedIndex == 4 && polyPoints.Count > 0)
                {
                    var dialogX = new InputBox("Введите коэффициент X сжатия/растяжения");
                    var dialogY = new InputBox("Введите коэффициент Y сжатия/растяжения");

                    double kx = 0.0, ky = 0.0;
                    if (dialogX.ShowDialog() == DialogResult.Cancel)
                    {
                        return;
                    }
                    if (!double.TryParse(dialogX.ResultText, out kx))
                    {
                        return;
                    }
                    if (dialogY.ShowDialog() == DialogResult.Cancel)
                    {
                        return;
                    }
                    if (!double.TryParse(dialogY.ResultText, out ky))
                    {
                        return;
                    }

                    LastPoint = e.Location;
                    g.DrawLine(new Pen(Color.White), polyPoints[0], polyPoints[polyPoints.Count - 1]);
                    for (int i = 0; i < polyPoints.Count - 1; i++)
                    {
                        g.DrawLine(new Pen(Color.White), polyPoints[i], polyPoints[i + 1]);
                    }

                    for (int i = 0; i < polyPoints.Count; i++)
                    {
                        if (i != polyPoints.Count - 1)
                        {
                            polyPoints[i] = scale(LastPoint, kx, ky, polyPoints[i]);
                            g.DrawLine(new Pen(Color.Black), polyPoints[i], scale(LastPoint, kx, ky, polyPoints[i + 1]));
                        }
                        else
                        {
                            polyPoints[i] = scale(LastPoint, kx, ky, polyPoints[i]);
                            g.DrawLine(new Pen(Color.Black), polyPoints[i], polyPoints[0]);
                        }
                    }
                    area.Invalidate();
                }
                // Check convex polygon(RightCheck)
                else if (polygonOptionsBox.SelectedIndex == 5 && polyPoints.Count > 0)
                {
                    LastPoint = e.Location;

                    Point P     = CurrentPoint;//new Point(a, b);
                    int   c     = 0;
                    Point start = this.polyPoints.First();
                    Point old   = this.polyPoints.First();
                    for (int i = 1; i < polyPoints.Count(); i++)
                    {
                        if (this.RightCheck(old, this.polyPoints.ElementAt(i), P) == true)
                        {
                            c += 1;
                        }
                        old = this.polyPoints.ElementAt(i);
                    }
                    if (this.RightCheck(old, start, P) == true)
                    {
                        c += 1;
                    }

                    if (c == 0 || c == this.polyPoints.Count())
                    {
                        g.FillEllipse(new SolidBrush(Color.Green), new Rectangle(P.X, P.Y, 4, 4));
                    }
                    else
                    {
                        g.FillEllipse(new SolidBrush(Color.Red), new Rectangle(P.X, P.Y, 4, 4));
                    }
                    area.Invalidate();
                }

                // Check convex polygon(RightCheckFilling)
                else if (polygonOptionsBox.SelectedIndex == 6 && polyPoints.Count > 0)
                {
                    LastPoint = e.Location;

                    for (int a = 1; a < area.Width; a += 5)
                    {
                        for (int b = 1; b < area.Height; b += 5)
                        {
                            Point P     = new Point(a, b);
                            int   c     = 0;
                            Point start = this.polyPoints.First();

                            Point old = this.polyPoints.First();
                            for (int i = 1; i < polyPoints.Count(); i++)
                            {
                                if (this.RightCheck(old, this.polyPoints.ElementAt(i), P) == true)
                                {
                                    c += 1;
                                }
                                old = this.polyPoints.ElementAt(i);
                            }

                            if (this.RightCheck(old, start, P) == true)
                            {
                                c += 1;
                            }

                            if (c == 0 || c == this.polyPoints.Count())
                            {
                                g.FillEllipse(new SolidBrush(Color.Green), new Rectangle(P.X, P.Y, 4, 4));
                            }
                            else
                            {
                                g.FillEllipse(new SolidBrush(Color.Red), new Rectangle(P.X, P.Y, 4, 4));
                            }


                            area.Invalidate();
                        }
                    }
                }
                else if (polygonOptionsBox.SelectedIndex == 7 && polyPoints.Count > 1)
                {
                    int   count_cross = 0;
                    Point P           = CurrentPoint;//new Point(a, b);
                    Point start       = this.polyPoints.First();
                    Point old         = this.polyPoints.First();
                    Point D           = new Point(area.Width - 1, P.Y);
                    for (int i = 1; i < polyPoints.Count(); i++)
                    {
                        if (this.Check_Cross(old, this.polyPoints.ElementAt(i), P, D) == true)
                        {
                            count_cross += 1;
                        }
                        old = this.polyPoints.ElementAt(i);
                    }

                    if (this.Check_Cross(old, start, P, D) == true)
                    {
                        count_cross += 1;
                    }
                    if (count_cross % 2 == 1)
                    {
                        g.FillEllipse(new SolidBrush(Color.Green), new Rectangle(P.X, P.Y, 4, 4));
                    }
                    else
                    {
                        g.FillEllipse(new SolidBrush(Color.Red), new Rectangle(P.X, P.Y, 4, 4));
                    }
                    area.Invalidate();
                }
                else if (polygonOptionsBox.SelectedIndex == 8 && polyPoints.Count > 1)
                {
                    for (int a = 1; a < area.Width; a += 5)
                    {
                        for (int b = 1; b < area.Height; b += 5)
                        {
                            int   count_cross = 0;
                            Point P           = new Point(a, b);
                            Point start       = this.polyPoints.First();
                            Point old         = this.polyPoints.First();
                            Point D           = new Point(area.Width - 1, P.Y);
                            for (int i = 1; i < polyPoints.Count(); i++)
                            {
                                if (this.Check_Cross(old, this.polyPoints.ElementAt(i), P, D) == true)
                                {
                                    count_cross += 1;
                                }
                                old = this.polyPoints.ElementAt(i);
                            }

                            if (this.Check_Cross(old, start, P, D) == true)
                            {
                                count_cross += 1;
                            }
                            if (count_cross % 2 == 1)
                            {
                                g.FillEllipse(new SolidBrush(Color.Green), new Rectangle(P.X, P.Y, 4, 4));
                            }
                            else
                            {
                                g.FillEllipse(new SolidBrush(Color.Red), new Rectangle(P.X, P.Y, 4, 4));
                            }
                            area.Invalidate();
                        }
                    }
                }
                if (can_draw)
                {
                    if (first)
                    {
                        polyPoints.Clear();
                        LastPoint = e.Location;
                        polyPoints.Add(LastPoint);
                        first = false;
                    }
                    else
                    {
                        g.DrawLine(p, LastPoint, CurrentPoint);
                        LastPoint = CurrentPoint;
                        polyPoints.Add(CurrentPoint);
                        area.Invalidate();
                    }
                }
            }
        }
Esempio n. 2
0
        private void PolygonOptionsBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (polygonButton.Checked && polyPoints.Count > 0)
            {
                if (polygonOptionsBox.SelectedIndex == 0)
                {
                    var dialogX = new InputBox("Введите смещение по X (целое число)");
                    var dialogY = new InputBox("Введите смещение по Y (целое число)");
                    int dx = 0, dy = 0;
                    if (dialogX.ShowDialog() == DialogResult.Cancel)
                    {
                        return;
                    }
                    if (!int.TryParse(dialogX.ResultText, out dx))
                    {
                        return;
                    }
                    if (dialogY.ShowDialog() == DialogResult.Cancel)
                    {
                        return;
                    }
                    if (!int.TryParse(dialogY.ResultText, out dy))
                    {
                        return;
                    }

                    g.DrawLine(new Pen(Color.White), polyPoints[0], polyPoints[polyPoints.Count - 1]);
                    for (int i = 0; i < polyPoints.Count - 1; i++)
                    {
                        g.DrawLine(new Pen(Color.White), polyPoints[i], polyPoints[i + 1]);
                    }

                    for (int i = 0; i < polyPoints.Count; i++)
                    {
                        if (i != polyPoints.Count - 1)
                        {
                            polyPoints[i] = move(polyPoints[i], dx, dy);
                            g.DrawLine(new Pen(Color.Black), polyPoints[i], move(polyPoints[i + 1], dx, dy));
                        }
                        else
                        {
                            polyPoints[i] = move(polyPoints[i], dx, dy);
                            g.DrawLine(new Pen(Color.Black), polyPoints[i], polyPoints[0]);
                        }
                    }
                    area.Invalidate();
                }
                else if (polygonOptionsBox.SelectedIndex == 1 && polyPoints.Count > 0)
                {
                    var dialogX = new InputBox("Введите угол");

                    int alpha = 0;
                    if (dialogX.ShowDialog() == DialogResult.Cancel)
                    {
                        return;
                    }
                    if (!int.TryParse(dialogX.ResultText, out alpha))
                    {
                        return;
                    }

                    LastPoint = mass_center();
                    g.DrawLine(new Pen(Color.White), polyPoints[0], polyPoints[polyPoints.Count - 1]);
                    for (int i = 0; i < polyPoints.Count - 1; i++)
                    {
                        g.DrawLine(new Pen(Color.White), polyPoints[i], polyPoints[i + 1]);
                    }

                    for (int i = 0; i < polyPoints.Count; i++)
                    {
                        if (i != polyPoints.Count - 1)
                        {
                            polyPoints[i] = rotate(LastPoint, alpha, polyPoints[i]);
                            g.DrawLine(new Pen(Color.Black), polyPoints[i], rotate(LastPoint, alpha, polyPoints[i + 1]));
                        }
                        else
                        {
                            polyPoints[i] = rotate(LastPoint, alpha, polyPoints[i]);
                            g.DrawLine(new Pen(Color.Black), polyPoints[i], polyPoints[0]);
                        }
                    }
                    area.Invalidate();
                }
                else if (polygonOptionsBox.SelectedIndex == 3 && polyPoints.Count > 0)
                {
                    var dialogX = new InputBox("Введите коэффициент X сжатия/растяжения");
                    var dialogY = new InputBox("Введите коэффициент Y сжатия/растяжения");

                    double kx = 0.0, ky = 0.0;
                    if (dialogX.ShowDialog() == DialogResult.Cancel)
                    {
                        return;
                    }
                    if (!double.TryParse(dialogX.ResultText, out kx))
                    {
                        return;
                    }
                    if (dialogY.ShowDialog() == DialogResult.Cancel)
                    {
                        return;
                    }
                    if (!double.TryParse(dialogY.ResultText, out ky))
                    {
                        return;
                    }

                    LastPoint = mass_center();
                    g.DrawLine(new Pen(Color.White), polyPoints[0], polyPoints[polyPoints.Count - 1]);
                    for (int i = 0; i < polyPoints.Count - 1; i++)
                    {
                        g.DrawLine(new Pen(Color.White), polyPoints[i], polyPoints[i + 1]);
                    }

                    for (int i = 0; i < polyPoints.Count; i++)
                    {
                        if (i != polyPoints.Count - 1)
                        {
                            polyPoints[i] = scale(LastPoint, kx, ky, polyPoints[i]);
                            g.DrawLine(new Pen(Color.Black), polyPoints[i], scale(LastPoint, kx, ky, polyPoints[i + 1]));
                        }
                        else
                        {
                            polyPoints[i] = scale(LastPoint, kx, ky, polyPoints[i]);
                            g.DrawLine(new Pen(Color.Black), polyPoints[i], polyPoints[0]);
                        }
                    }
                    area.Invalidate();
                }
            }
        }