Exemple #1
0
        private static double getArea(Puncte A, Puncte B, Puncte C)
        {
            double s = A.x * B.y - A.y * B.x;

            s += B.x * C.y - B.y * C.x;
            s += C.x * A.y - C.y * A.x;

            return(Math.Abs(s));
        }
Exemple #2
0
 private static void getMaxX(int n, Puncte[] P, Puncte nowP)
 {
     maxX = nowP.x;
     for (int i = 1; i <= n; ++i)
     {
         if (cmp(maxX, Math.Abs(P[i].x)) < 0)
         {
             maxX = P[i].x;
         }
     }
 }
Exemple #3
0
 private static void getMaxY(int n, Puncte[] P, Puncte nowP)
 {
     maxY = nowP.y;
     for (int i = 1; i <= n; ++i)
     {
         if (cmp(maxY, Math.Abs(P[i].y)) < 0)
         {
             maxY = P[i].y;
         }
     }
 }
Exemple #4
0
        public static void init(ref int n, Puncte[] P, Puncte nowP, int new_W, int new_H, Graphics g)
        {
            W = new_W / 2 - 7;
            H = new_H / 2 - 33;
            getMaxX(n, P, nowP);
            getMaxY(n, P, nowP);

            int pos = 1;

            ldP = P[1];
            for (int i = 2; i <= n; ++i)
            {
                if (cmpPuncte(P[i], ldP) < 0)
                {
                    pos = i;
                    ldP = P[i];
                }
            }
            ldPP = new Puncte(ldP.x, ldP.y);
            p    = new Puncte(nowP.x, nowP.y);

            for (int i = pos; i < n; ++i)
            {
                P[i] = P[i + 1];
            }
            --n;

            Array.Sort(P, 1, n, new cmpSlope());

            PP = new Puncte[n + 1];
            for (int i = 1; i <= n; ++i)
            {
                PP[i] = new Puncte(P[i].x, P[i].y);
            }

            if (maxX > W || maxY > H)
            {
                decrease(n, P);
            }
            else
            {
                increase(n, P);
            }
            translate(n, P);
            drawPolygon(n, P, g);

            /*
             * Pen blackPen = new Pen(Color.Black, 1);
             * g.DrawLine(blackPen, W, 0, W, H);
             * g.DrawLine(blackPen, W, 2 * H, W, H);
             * g.DrawLine(blackPen, 0, H, W, H);
             * g.DrawLine(blackPen, 2 * W, H, W, H);
             */
        }
Exemple #5
0
        public static string solve(int n, Puncte nowP)
        {
            if (Math.Abs(det(ldPP, PP[1], nowP)) < eps)
            {
                return("Punctul se afla pe dreapta determinata de punctele: (" + ldPP.x + ", " + ldPP.y + "), (" +
                       PP[1].x + ", " + PP[1].y + ")!");
            }
            for (int i = 1; i < n; ++i)
            {
                if (Math.Abs(det(PP[i], PP[i + 1], nowP)) < eps)
                {
                    return("Punctul se afla pe dreapta determinata de punctele: (" + PP[i].x + ", " + PP[i].y + "), (" +
                           PP[i + 1].x + ", " + PP[i + 1].y + ")!");
                }
            }
            if (Math.Abs(det(PP[n], ldPP, nowP)) < eps)
            {
                return("Punctul se afla pe dreapta determinata de punctele: (" + PP[n].x + ", " + PP[n].y + "), (" +
                       ldPP.x + ", " + ldPP.y + ")!");
            }

            int lt = 0, rt = n + 1;

            while (rt - lt > 1)
            {
                int    mid = (lt + rt) / 2;
                double cs  = cross_slope(ldPP, nowP, PP[mid]);
                if (cs < -getEps())
                {
                    rt = mid;
                }
                else
                {
                    lt = mid;
                }
            }

            if (lt == 0 || rt == n + 1)
            {
                return("Punctul se afla in exteriorul poligonului!");
            }

            double t1 = getArea(ldPP, nowP, PP[lt]);
            double t2 = getArea(PP[lt], nowP, PP[rt]);
            double t3 = getArea(PP[rt], nowP, ldPP);
            double t  = getArea(ldPP, PP[lt], PP[rt]);

            if (Math.Abs(t1 + t2 + t3 - t) < eps)
            {
                return("Punctul se afla in interiorul poligonului");
            }

            return("Punctul se afla in exteriorul poligonului!");
        }
Exemple #6
0
 public Form3(int n, Puncte[] P, Puncte nowP)
 {
     InitializeComponent();
     this.n    = n;
     this.nowP = new Puncte(nowP.x, nowP.y);
     this.P    = new Puncte[this.n + 1];
     for (int i = 1; i <= n; ++i)
     {
         this.P[i] = new Puncte(P[i].x, P[i].y);
     }
 }
Exemple #7
0
        public static void drawDiagonals(int n, Puncte[] P, Puncte nowP, Graphics g)
        {
            Pen redPen = new Pen(Color.Red, 1);

            g.DrawLine(redPen, (float)ldP.x + 1, (float)ldP.y + 1, (float)P[1].x + 1, (float)P[1].y + 1);
            for (int i = 1; i < n; ++i)
            {
                g.DrawLine(redPen, (float)ldP.x + 1, (float)ldP.y + 1, (float)P[i].x + 1, (float)P[i].y + 1);
            }
            g.DrawLine(redPen, (float)P[n].x + 1, (float)P[n].y + 1, (float)ldP.x + 1, (float)ldP.y + 1);

            Pen bluePen = new Pen(Color.Blue, 1);

            bluePen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;
            g.DrawLine(bluePen, (float)ldP.x + 1, (float)ldP.y + 1, (float)p.x + 1, (float)p.y + 1);
        }
Exemple #8
0
        public static int cmpPuncte(Puncte A, Puncte B)
        {
            if (A.x - B.x < -getEps())
            {
                return(-1);
            }
            else if (A.x - B.x > getEps())
            {
                return(1);
            }
            else
            {
                if (A.y - B.y < -getEps())
                {
                    return(-1);
                }
                else if (A.y - B.y > getEps())
                {
                    return(1);
                }
            }

            return(0);
        }
Exemple #9
0
 public static double cross_slope(Puncte A, Puncte B, Puncte C)
 {
     return((A.y - B.y) * (A.x - C.x) - (A.y - C.y) * (A.x - B.x));
 }
Exemple #10
0
 private static double det(Puncte A, Puncte B, Puncte C)
 {
     return(A.x * B.y + B.x * C.y + C.x * A.y
            - C.x * B.y - A.x * C.y - B.x * A.y);
 }
Exemple #11
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (i == n + 1)
            {
                try
                {
                    string sX = textBoxX.Text;
                    string sY = textBoxY.Text;
                    if (sX.Length == 0)
                    {
                        MessageBox.Show("Va rugam sa introduceti un numar intre [1, 10^5]!", "Error!");
                        textBoxX.Text = "";
                        return;
                    }
                    if (sY.Length == 0)
                    {
                        MessageBox.Show("Va rugam sa introduceti un numar intre [1, 10^5]!", "Error!");
                        textBoxY.Text = "";
                        return;
                    }

                    double x = Convert.ToDouble(sX);
                    double y = Convert.ToDouble(sY);
                    nowP = new Puncte(x, y);

                    Form3 form3 = new Form3(n, P, nowP);
                    form3.Show();
                    Hide();
                }
                catch
                {
                    MessageBox.Show("Va rugam sa introduceti un numar intre [1, 10^5]!", "Error!");
                    textBoxX.Text = "";
                    textBoxY.Text = "";
                }
            }

            if (i <= n)
            {
                try
                {
                    string sX = textBoxX.Text;
                    string sY = textBoxY.Text;
                    if (sX.Length == 0)
                    {
                        MessageBox.Show("Va rugam sa introduceti un numar intre [1, 10^5]!", "Error!");
                        textBoxX.Text = "";
                        return;
                    }
                    if (sY.Length == 0)
                    {
                        MessageBox.Show("Va rugam sa introduceti un numar intre [1, 10^5]!", "Error!");
                        textBoxY.Text = "";
                        return;
                    }

                    double x = Convert.ToDouble(sX);
                    double y = Convert.ToDouble(sY);
                    P[i] = new Puncte(x, y);

                    if (i < n)
                    {
                        punctLabel.Text = "Punctul " + Convert.ToString(i + 1) + ":";
                    }
                    else
                    {
                        punctLabel.Text = "Punct: ";
                    }
                    textBoxX.Text = "";
                    textBoxY.Text = "";
                    ++i;
                }
                catch
                {
                    MessageBox.Show("Va rugam sa introduceti un numar intre [1, 10^5]!", "Error!");
                    textBoxX.Text = "";
                    textBoxY.Text = "";
                }
            }
        }