Esempio n. 1
0
    public bool IsValidConnection(GameObject thisSystem, GameObject targetSystem)                                       //Returns true if no intersection
    {
        Vector3 lineA = MathsFunctions.ABCLineEquation(thisSystem.transform.position, targetSystem.transform.position); //Line equation from current system to target system

        Vector3 roughCentre = (thisSystem.transform.position + targetSystem.transform.position) / 2f;

        if (roughCentre.x < 65f && roughCentre.x > 55f)        //If the intersection exists in the galactic core, ignore it
        {
            if (roughCentre.y < 65f && roughCentre.y > 55f)
            {
                return(false);
            }
        }

        for (int i = 0; i < coordinateList.Count; ++i)                                                    //For all existing connections
        {
            if (coordinateList[i].systemOne == thisSystem && coordinateList[i].systemTwo == targetSystem) //If a connection already exists between the current system and the target system, continue to the next connection
            {
                continue;
            }
            if (coordinateList[i].systemTwo == thisSystem && coordinateList[i].systemOne == targetSystem)            //Continuation of above
            {
                continue;
            }
            if (coordinateList[i].systemOne == thisSystem || coordinateList[i].systemTwo == thisSystem)            //If the connection contains this system it will not make intersections with the temporary connection
            {
                continue;
            }
            if (coordinateList[i].systemOne == targetSystem || coordinateList[i].systemTwo == targetSystem)            //If the connection contains the target system it will not make intersections with the temporary connection
            {
                continue;
            }

            //if(CheckIfIntersectionCouldOccur(thisSystem, targetSystem, coordinateList[i].systemOne, coordinateList[i].systemTwo) == false)
            //{
            //	continue;
            //

            Vector3 lineB = MathsFunctions.ABCLineEquation(coordinateList[i].systemOne.transform.position, coordinateList[i].systemTwo.transform.position); //Get the line equation between of the connection

            Vector2 intersection = MathsFunctions.IntersectionOfTwoLines(lineA, lineB);                                                                     //Find the intersection of the two lines

            if (intersection == Vector2.zero)                                                                                                               //If the lines are parallel the method returns a zero vector continue to the next connection
            {
                continue;
            }

            if (MathsFunctions.PointLiesOnLine(thisSystem.transform.position, targetSystem.transform.position, intersection))                                     //If the intersection lies on the temporary connection
            {
                if (MathsFunctions.PointLiesOnLine(coordinateList[i].systemOne.transform.position, coordinateList[i].systemTwo.transform.position, intersection)) //And it lies on the current permanent connection
                {
                    return(false);                                                                                                                                //Return true, an intersection does exist
                }
            }
        }

        return(true);
    }
    public bool CheckIsDelaunay(Triangle triOne, Triangle triTwo)
    {
        List <GameObject> sharedSides = MasterScript.triangulation.CheckIfSharesSide(triOne, triTwo);                                                                  //Find if triangles share a side (this actually returns the 2 shared and 2 unshared vertices)

        if (sharedSides.Count == 4)                                                                                                                                    //If 4 vertices are shared
        {
            GameObject sharedPointA = sharedSides[0];                                                                                                                  //Assign the shared points
            GameObject sharedPointB = sharedSides[1];
            GameObject unsharedPointA = sharedSides[2];                                                                                                                //Assign the unshared points
            GameObject unsharedPointB = sharedSides[3];
            float      angleAlpha = 0f, angleBeta = 0f;                                                                                                                //Set some angles to 0

            angleAlpha = MathsFunctions.AngleBetweenLineSegments(unsharedPointA.transform.position, sharedPointA.transform.position, sharedPointB.transform.position); //First angle is at one unshared point

            angleBeta = MathsFunctions.AngleBetweenLineSegments(unsharedPointB.transform.position, sharedPointA.transform.position, sharedPointB.transform.position);  //Second angle is at the other unshared point

            Vector3 sharedPointLine   = MathsFunctions.ABCLineEquation(sharedPointA.transform.position, sharedPointB.transform.position);                              //Get the line between the shared points
            Vector3 unsharedPointLine = MathsFunctions.ABCLineEquation(unsharedPointA.transform.position, unsharedPointB.transform.position);                          //Get the line between the unshared points
            Vector2 intersection      = MathsFunctions.IntersectionOfTwoLines(sharedPointLine, unsharedPointLine);                                                     //Find the intersection of the two lines

            if (MathsFunctions.PointLiesOnLine(sharedPointA.transform.position, sharedPointB.transform.position, intersection) == false)                               //If the intersection does not lie between the shared points then this is a non convex hull
            {
                return(true);                                                                                                                                          //So it cannot be flipped, continue to the next triangle
            }

            if (angleAlpha + angleBeta > 180f)                                        //If the polygon is convex, and the two angles combine to be greater than 180 degrees, the triangles are non delaunay, so we flip them
            {
                int triPosOne = MasterScript.triangulation.triangles.IndexOf(triOne); //Find the position of the two triangles in the triangles list
                int triPosTwo = MasterScript.triangulation.triangles.IndexOf(triTwo);

                triOne.points[0] = unsharedPointA;                 //Reassign the vertices of tri one to make the previously unshared vertices the shared vertices. One of the previously shared vertices is now the unshared vertex.
                triOne.points[1] = unsharedPointB;
                triOne.points[2] = sharedPointA;
                triOne.lines[0]  = MathsFunctions.ABCLineEquation(triOne.points[0].transform.position, triOne.points[1].transform.position);                 //Get the line equations for all the sides
                triOne.lines[1]  = MathsFunctions.ABCLineEquation(triOne.points[1].transform.position, triOne.points[2].transform.position);
                triOne.lines[2]  = MathsFunctions.ABCLineEquation(triOne.points[2].transform.position, triOne.points[0].transform.position);
                MasterScript.triangulation.triangles[triPosOne] = triOne; //Replace the original triangle with this new, flipped triangle

                triTwo.points[0] = unsharedPointA;                        //Do the same for tri two
                triTwo.points[1] = unsharedPointB;
                triTwo.points[2] = sharedPointB;
                triTwo.lines[0]  = MathsFunctions.ABCLineEquation(triTwo.points[0].transform.position, triTwo.points[1].transform.position);
                triTwo.lines[1]  = MathsFunctions.ABCLineEquation(triTwo.points[1].transform.position, triTwo.points[2].transform.position);
                triTwo.lines[2]  = MathsFunctions.ABCLineEquation(triTwo.points[2].transform.position, triTwo.points[0].transform.position);
                MasterScript.triangulation.triangles[triPosTwo] = triTwo;

                ++flips;                 //Increase the number of flips that have been made this pass.

                return(false);           //Return false (a flip has been made)
            }
        }

        return(true);        //Otherwise continue to the next triangle
    }
Esempio n. 3
0
    private bool IsIllegalIntersection(int externalPoint, int unvisitedPoint, Vector3 curToExtLine)
    {
        for (int j = 0; j < triangles.Count; ++j)        //For every triangle
        {
            if (triangles[j].isInternal == true)         //If the triangle is internal we don't need to worry about it, so move onto the next triangle
            {
                continue;
            }

            for (int k = 0; k < 3; ++k)            //For each line in that triangle
            {
                GameObject pointA = null;          //Get the points at each end of the line
                GameObject pointB = null;

                if (k == 0)
                {
                    pointA = triangles[j].points[0];
                    pointB = triangles[j].points[1];
                }
                if (k == 1)
                {
                    pointA = triangles[j].points[1];
                    pointB = triangles[j].points[2];
                }
                if (k == 2)
                {
                    pointA = triangles[j].points[2];
                    pointB = triangles[j].points[0];
                }

                if (pointA.name == externalPoints[externalPoint].name || pointB.name == externalPoints[externalPoint].name)                //If the line contains the external point we can ignore it
                {
                    continue;
                }

                Vector2 intersection = MathsFunctions.IntersectionOfTwoLines(curToExtLine, triangles[j].lines[k]);                                                     //Get the intersection of the line with the current star to external point line

                if (MathsFunctions.PointLiesOnLine(externalPoints[externalPoint].transform.position, unvisitedStars[unvisitedPoint].transform.position, intersection)) //If the point lies elsewhere on the line
                {
                    if (MathsFunctions.PointLiesOnLine(pointA.transform.position, pointB.transform.position, intersection))
                    {
                        return(true);                        //This IS an illegal intersection so return that, otherwise keep checking
                    }
                }
            }
        }

        return(false);
    }
Esempio n. 4
0
    private void CheckForNonDelaunayTriangles()
    {
        List <Triangle> numberofnondelaunay = new List <Triangle>();

        for (int i = 0; i < triangles.Count; ++i)
        {
            for (int j = 0; j < triangles.Count; ++j)
            {
                if (i == j)
                {
                    continue;
                }

                List <GameObject> sharedSides = CheckIfSharesSide(triangles[i], triangles[j]);

                if (sharedSides.Count == 4)
                {
                    GameObject sharedPointA = sharedSides[0];
                    GameObject sharedPointB = sharedSides[1];
                    GameObject unsharedPointA = sharedSides[2];
                    GameObject unsharedPointB = sharedSides[3];
                    float      angleAlpha = 0f, angleBeta = 0f;

                    angleAlpha = MathsFunctions.AngleBetweenLineSegments(unsharedPointA.transform.position, sharedPointA.transform.position, sharedPointB.transform.position);

                    angleBeta = MathsFunctions.AngleBetweenLineSegments(unsharedPointB.transform.position, sharedPointA.transform.position, sharedPointB.transform.position);

                    Vector3 sharedPointLine   = MathsFunctions.ABCLineEquation(sharedPointA.transform.position, sharedPointB.transform.position);
                    Vector3 unsharedPointLine = MathsFunctions.ABCLineEquation(unsharedPointA.transform.position, unsharedPointB.transform.position);
                    Vector2 intersection      = MathsFunctions.IntersectionOfTwoLines(sharedPointLine, unsharedPointLine);

                    if (MathsFunctions.PointLiesOnLine(sharedPointA.transform.position, sharedPointB.transform.position, intersection) == false)                    //Is non-convex
                    {
                        continue;
                    }

                    if (angleBeta + angleAlpha > 180)
                    {
                        numberofnondelaunay.Add(triangles[i]);
                    }
                }
            }
        }

        Debug.Log(numberofnondelaunay.Count + " | " + triangles.Count);
    }