public bool SharesPointWith(MapPointLink otherLink)
 {
     return(Point0.Position == otherLink.Point0.Position ||
            Point1.Position == otherLink.Point1.Position ||
            Point0.Position == otherLink.Point1.Position ||
            Point1.Position == otherLink.Point0.Position);
 }
    List <LinkTri> CreateLinkTris(List <MapPointLink> linksList)
    {
        List <LinkTri> linkTris = new List <LinkTri>();

        for (int i = 1; i < linksList.Count; i++)
        {
            MapPointLink link0 = linksList[i - 1];
            MapPointLink link1 = linksList[i];
            if (link0.Centroid.y.RoughlyEquals(link1.Centroid.y, 2))
            {
                // on the same row
                if (link0.SharesPointWith(link1))
                {
                    // share a vertex
                    // these links must form a map triangle
                    linkTris.Add(new LinkTri(link0, link1));
                }
            }
        }

        return(linkTris);
    }
 public LinkTri(MapPointLink link0, MapPointLink link1)
 {
     Link0 = link0;
     Link1 = link1;
 }
    //void OnDrawGizmos()
    //{
    //    if (Application.isPlaying)
    //    {
    //        foreach (var l in linksListTest)
    //        {
    //            Gizmos.DrawLine(l.Point0.PositionXYZ, l.Point1.PositionXYZ);
    //        }
    //    }
    //}

    List <MapPointLink> GetLinks(MapPoint?[,] mapData)
    {
        List <MapPointLink> tempList = new List <MapPointLink>();

        // loop through rows
        for (int y = 0; y < mapData.GetLength(1) - 1; y++)
        {
            // loop through points in row
            for (int x = 0; x < mapData.GetLength(0); x++)
            {
                MapPoint?pointCurrent = mapData[x, y];

                if (pointCurrent == null)
                {
                    continue;
                }

                MapPoint?pointLeftOfCurrent = null;
                if (mapData.HasIndex(x - 1, y))
                {
                    pointLeftOfCurrent = mapData[x - 1, y];
                }

                MapPoint?pointRightOfCurrent = null;
                if (mapData.HasIndex(x + 1, y))
                {
                    pointRightOfCurrent = mapData[x + 1, y];
                }

                // don't have to check as we know a space above exists, don't know if not null though
                MapPoint?pointAboveCurrent = mapData[x, y + 1];

                MapPoint?pointAboveLeftOfCurrent = null;
                if (mapData.HasIndex(x - 1, y + 1))
                {
                    pointAboveLeftOfCurrent = mapData[x - 1, y + 1];
                }

                MapPoint?pointAboveRightOfCurrent = null;
                if (mapData.HasIndex(x + 1, y + 1))
                {
                    pointAboveRightOfCurrent = mapData[x + 1, y + 1];
                }

                // check if x values are the same
                if (pointAboveCurrent != null)
                {
                    if (pointCurrent.Value.Position.x.RoughlyEquals(pointAboveCurrent.Value.Position.x, 2))
                    {
                        // There are the only 2 cases where a left leaning link will happen
                        if (pointAboveLeftOfCurrent != null)
                        {
                            if (pointLeftOfCurrent == null)
                            {
                                // Left leaning link: Case 1
                                // o-o
                                //  \|
                                // x c
                                MapPointLink linkLeft = new MapPointLink(pointCurrent.Value, pointAboveLeftOfCurrent.Value);
                                tempList.Add(linkLeft);
                            }
                        }

                        // create link
                        MapPointLink link = new MapPointLink(pointCurrent.Value, pointAboveCurrent.Value);
                        tempList.Add(link);

                        if (pointAboveRightOfCurrent != null)
                        {
                            MapPointLink linkRight = new MapPointLink(pointCurrent.Value, pointAboveRightOfCurrent.Value);
                            tempList.Add(linkRight);
                        }
                    }
                }
                else if (pointAboveRightOfCurrent != null)
                {
                    if (pointRightOfCurrent != null)
                    {
                        if (pointRightOfCurrent.Value.Position.x.RoughlyEquals(pointAboveRightOfCurrent.Value.Position.x, 2))
                        {
                            // in this case no point exists above the current point but does to the above right
                            //
                            // x o
                            //  /|
                            // c-o

                            MapPointLink linkRight = new MapPointLink(pointCurrent.Value, pointAboveRightOfCurrent.Value);
                            tempList.Add(linkRight);
                        }
                    }
                }
                else if (pointAboveLeftOfCurrent != null)
                {
                    if (pointLeftOfCurrent != null)
                    {
                        if (pointLeftOfCurrent.Value.Position.x.RoughlyEquals(pointAboveLeftOfCurrent.Value.Position.x, 2))
                        {
                            // Left leaning link: Case 2
                            // o x
                            // |\
                            // o-c

                            MapPointLink linkLeft = new MapPointLink(pointCurrent.Value, pointAboveLeftOfCurrent.Value);
                            tempList.Add(linkLeft);
                        }
                    }
                }
            }
        }

        return(tempList);
    }