예제 #1
0
        private bool IsPointInTriangle(Point P, Point A, Point B, Point C)
        {
            float areaOfTriangle = Math.Abs(Matematics.Area(A, B, C));
            float area_APB       = Math.Abs(Matematics.Area(A, P, B));
            float area_APC       = Math.Abs(Matematics.Area(A, P, C));
            float area_BPC       = Math.Abs(Matematics.Area(B, P, C));

            return(areaOfTriangle == area_APB + area_APC + area_BPC);
        }
예제 #2
0
        private void button_Triangulate_Click(object sender, EventArgs e)
        {
            double area  = 0;
            int    count = Engine.points.Count;

            if (count < 3)
            {
                return;
            }
            if (count == 3)
            {
                area = Matematics.Area(Engine.points[0], Engine.points[1], Engine.points[2]); return;
            }

            List <Point> points    = Engine.points;// lista temporara
            List <int>   indexList = new List <int>();

            for (int i = 0; i < points.Count; i++)
            {
                indexList.Add(i); //initializarea listei de indecsi de la 0 pana la nr de puncte
            }

            Point A;
            Point B;
            Point C;

            while (indexList.Count > 3)
            {
                for (int i = 0; i < indexList.Count; i++)
                {
                    int a = indexList[i];              // the index of current Item
                    int b = GetItem(indexList, i - 1); // the index of leftside neighbour
                    int c = GetItem(indexList, i + 1); // the index of righside neighbour

                    A = points[a];
                    B = points[b];
                    C = points[c];

                    int clockwise = GetTurnType(A, B, C);
                    if (clockwise != -1)
                    {
                        continue;       // make a salt to next index
                    }
                    bool isEar = true;  //presupunem ca este o ureche valida daca este
                    for (int j = 0; j < points.Count; j++)
                    {
                        if (j == a || j == b || j == c)
                        {
                            continue; //excluding the current index and the neighbours from test
                        }

                        if (IsPointInTriangle(points[j], A, B, C))
                        {
                            isEar = false;
                            break;
                        }
                    }


                    if (isEar)
                    {
                        Engine.triangles.Add(new Triangle(A, B, C)); //add the triangle in the list, if is a valid ear.

                        indexList.RemoveAt(i);                       //remove the current index from the indexlist

                        //drawing the line to make a triangle visualy
                        Pen pen = new Pen(Color.Green);
                        myGraphics.gfx.DrawLine(pen, C.X, C.Y, B.X, B.Y);
                        myGraphics.refreshGraph();
                        break;
                    }
                }
            }


            //adding last 3 points what makes a triangle
            A = points[indexList[0]];
            B = points[indexList[1]];
            C = points[indexList[2]];
            Engine.triangles.Add(new Triangle(A, B, C));

            // calculate the polygon area using triangle's area.
            foreach (Triangle triangle in Engine.triangles)
            {
                area += Math.Abs(Matematics.Area(triangle.A, triangle.B, triangle.C));
            }



            points.Clear();
            indexList.Clear();

            output.Text = "Area:" + Environment.NewLine + area;
            status      = "triangulation completed";
        }