public void Merge() { //check validness (1) 2 polygon, (2) mergechance > 1; if (m_selected_convexhulls.Count < 2) { return; } foreach (GameObject hullObject in m_selected_convexhulls) { if (!hullObject.GetComponent <P2Hull>().mergeChance) { return; } } //Compute new convex hull, add new convex hull to current player, update score operated = true; Polygon2D newhull = ConvexHull.ComputeConvexHull(m_selected_convexhulls[0].GetComponent <P2Hull>().hull, m_selected_convexhulls[1].GetComponent <P2Hull>().hull); Debug.Log(m_selected_convexhulls[0].GetComponent <P2Hull>().hull.Area + ", " + m_selected_convexhulls[1].GetComponent <P2Hull>().hull.Area + "=" + newhull.Area); PlayerPolygons[playerIndex].Add(newhull); foreach (var v in newhull.Vertices) { PlayerPointsOnPolygons[playerIndex].Add(v); } CanvasPlayerPolygons[playerIndex].Add(PolygonOnCanvas(newhull)); foreach (var v in newhull.Vertices) { PlayerPoints[playerIndex].Add(v); } foreach (var seg in newhull.Segments) { PlayerSegments[playerIndex].Add(new P2Segment(seg.Point1, seg.Point2, player1Turn)); } //disable merges m_selected_convexhulls[0].GetComponent <P2Hull>().mergeChance = false; m_selected_convexhulls[1].GetComponent <P2Hull>().mergeChance = false; foreach (Polygon2D polygon in PlayerPolygons[1 - playerIndex]) { int i = PlayerPolygons[1 - playerIndex].IndexOf(polygon); if (Intersector.ifIntersect(PolygonOnCanvas(polygon), PolygonOnCanvas(newhull))) { PlayerPolygonObjects[1 - playerIndex][i].GetComponent <P2Hull>().mergeChance = false; } } UpdateMesh(newhull, playerIndex, false); if (newhull.VertexCount > 0) { PlayerScore[playerIndex] += newhull.Area; } UpdateScore(); UpdateTrapezoidalDecomposition(); }
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(); }