Esempio n. 1
0
 bool SegIntersectPolygon(P2Segment seg, int playerIndex)
 {
     foreach (Polygon2D p in PlayerPolygons[playerIndex])
     {
         if (p.Contains(seg.Segment.Point1))
         {
             return(true);
         }
         if (p.Contains(seg.Segment.Point2))
         {
             return(true);
         }
     }
     return(false);
 }
Esempio n. 2
0
        public void Connect()
        {
            // check validness (1) 2pts, (2) line segment not exist; (3) not crossed with existing segments (5) not intersecting existing polygon

            if (m_selected_points.Count < 2)
            {
                return;
            }
            var seg   = new P2Segment(m_selected_points[0].Pos, m_selected_points[1].Pos, player1Turn);
            var segLS = new LineSegment(m_selected_points[0].Pos, m_selected_points[1].Pos);


            //Debug.Log(seg.Segment.Point1 + "," + seg.Segment.Point2);


            if (m_segments.Contains(segLS))
            {
                return;
            }



            foreach (P2Segment s in PlayerSegments[playerIndex])
            {
                if (LineSegment.IntersectProper(s.Segment, seg.Segment) != null)
                {
                    return;
                }
            }

            if (SegIntersectPolygon(seg, playerIndex))
            {
                return;
            }

            //points on existing polygon cannot be selected.
            //Some explanations: If we can select points on existing polygon, we need to deal with the case: one line segement connecting two polygon
            //If we want to find a largest circle, this is NP-hard problem(Hamilton Cycle)



            operated = true;

            //add both vertices and segment to current player
            PlayerPoints[playerIndex].Add(seg.Segment.Point1);
            PlayerPoints[playerIndex].Add(seg.Segment.Point2);
            PlayerPoints[playerIndex] = PlayerPoints[playerIndex].Distinct().ToList();

            PlayerSegments[playerIndex].Add(seg);
            m_segments.Add(segLS);

            //draw line segment,
            var mesh = Instantiate(PlayerLineMeshesPrefab[playerIndex], Vector3.forward, Quaternion.identity) as GameObject;

            mesh.transform.parent = LineMeshCollection[playerIndex].transform;
            mesh.GetComponent <P2Segment>().CopySegment(seg);

            instantObjects.Add(mesh);
            PlayerLineMeshes[playerIndex].Add(mesh);

            var meshScript = mesh.GetComponent <ReshapingMesh>();

            meshScript.CreateNewMesh(seg.Segment.Point1, seg.Segment.Point2);



            // add new convex hull (if exist) to current player, draw convex hull, update score

            Polygon2D newpolygon = FindPolygon(playerIndex, seg.Segment.Point1);



            if (newpolygon != null)
            {
                Debug.Log("new polygon v cnt: " + newpolygon.VertexCount.ToString());


                PlayerPolygons[playerIndex].Add(newpolygon);
                CanvasPlayerPolygons[playerIndex].Add(PolygonOnCanvas(newpolygon));


                string printtext = "";
                foreach (var v in newpolygon.Vertices)
                {
                    printtext += "(" + v.x + "," + v.y + ")  ";
                }

                Debug.Log(printtext);

                UpdateMesh(newpolygon, playerIndex, true);

                if (newpolygon.VertexCount > 0)
                {
                    PlayerScore[playerIndex] += newpolygon.Area;
                }

                //For the other player, disable the merging for polygon with intersection

                foreach (Polygon2D polygon in PlayerPolygons[1 - playerIndex])
                {
                    int i = PlayerPolygons[1 - playerIndex].IndexOf(polygon);

                    Debug.Log("Polygon in oppo1: " + PolygonOnCanvas(polygon).VertexCount);
                    Debug.Log("Polygon in oppo2: " + PolygonOnCanvas(newpolygon).VertexCount);

                    if (Intersector.ifIntersect(PolygonOnCanvas(polygon), PolygonOnCanvas(newpolygon)))
                    {
                        PlayerPolygonObjects[1 - playerIndex][i].GetComponent <P2Hull>().mergeChance = false;
                    }
                }

                UpdateScore();
            }
            UpdateTrapezoidalDecomposition();
        }
Esempio n. 3
0
 public void CopySegment(P2Segment seg)
 {
     Segment         = seg.Segment;
     belongToPlayer1 = seg.belongToPlayer1;
 }