public void GenerateMeshes(JSONPuzzle puzzle)
 {
     foreach (var piece in puzzle.pieces)
     {
         var mesh     = new Mesh();
         var vertices = new Vector3[piece.corners.Length];
         var idx      = 0;
         foreach (var corner in piece.corners)
         {
             vertices[idx].x = corner.coord.x;
             vertices[idx].y = corner.coord.y;
             idx++;
         }
         mesh.vertices = vertices;
         mesh.uv       = GetComponent <MeshModel>().newUV;
         var verticesList = new List <Vector3>();
         foreach (var vertex in vertices)
         {
             verticesList.Add(vertex);
         }
         var triangles           = PolygonTriangulation.TriangulateConcavePolygon(verticesList);
         var trianglesAsIntArray = new int[triangles.Count * 3];
         idx = 0;
         foreach (var triangle in triangles)
         {
             trianglesAsIntArray[idx]     = Array.IndexOf(vertices, triangle.vertex1.GetXY());
             trianglesAsIntArray[idx + 1] = Array.IndexOf(vertices, triangle.vertex2.GetXY());
             trianglesAsIntArray[idx + 2] = Array.IndexOf(vertices, triangle.vertex3.GetXY());
             idx += 3;
         }
         mesh.triangles = trianglesAsIntArray;
         GetComponent <MeshModel>().meshes.Add(mesh);
     }
 }
    public JSONPuzzle CreatePuzzle()
    {
        JSONPuzzle puzzle = new JSONPuzzle();

        SetName(puzzle, GetComponent <DivisionModel>().nameOfPuzzle);
        SetForm(puzzle);
        SetPieces(puzzle, GetComponent <DivisionModel>().triangles);
        return(puzzle);
    }
    void SetForm(JSONPuzzle puzzle)
    {
        List <Vector3> corners = GetComponent <DivisionModel>().corners;

        puzzle.puzzle      = new Puzzle();
        puzzle.puzzle.form = new Form[corners.Count];
        for (int index = 0; index < corners.Count; index++)
        {
            puzzle.puzzle.form[index]         = new Form();
            puzzle.puzzle.form[index].coord   = new Coord();
            puzzle.puzzle.form[index].coord.x = corners[index].x;
            puzzle.puzzle.form[index].coord.y = corners[index].y;
        }
    }
 void SetPieces(JSONPuzzle puzzle, List <DivisionTriangle> triangles)
 {
     puzzle.nPieces = triangles.Count;
     puzzle.pieces  = new Piece[triangles.Count];
     for (int pieceIndex = 0; pieceIndex < triangles.Count; pieceIndex++)
     {
         puzzle.pieces[pieceIndex]         = new Piece();
         puzzle.pieces[pieceIndex].corners = new Corner[triangles[pieceIndex].vertices.Length];
         puzzle.pieces[pieceIndex].piece   = pieceIndex;
         for (int cornerIndex = 0; cornerIndex < triangles[pieceIndex].vertices.Length; cornerIndex++)
         {
             puzzle.pieces[pieceIndex].corners[cornerIndex]         = new Corner();
             puzzle.pieces[pieceIndex].corners[cornerIndex].coord   = new Coord();
             puzzle.pieces[pieceIndex].corners[cornerIndex].coord.x = triangles[pieceIndex].vertices[cornerIndex].x;
             puzzle.pieces[pieceIndex].corners[cornerIndex].coord.y = triangles[pieceIndex].vertices[cornerIndex].y;
         }
     }
 }
Пример #5
0
    public void AutoSolve()
    {
        puzzle = GetComponentInParent <PuzzleModel>().puzzle;
        pieces = GetComponentInParent <PuzzleModel>().pieces;
        FindCorners();

        currentPoint = upperLeftCorner;                                       //we start in the upper left corner of the board
        nextPoint    = upperRightCorner;                                      //and move right
        currentRow   = 0; currentColumn = 0; theta = 90.0f; indexOfTheta = 0; //starting theta is always 90 degrees
        int numberOfPieces = pieces.Count;


        while (placedPieces.Count < numberOfPieces)
        {
            Debug.Log("*********************************************************");
            Debug.Log("row and column: (" + currentRow + ", " + currentColumn + ")");
            bool changedRows = false;
            if (currentRow == 0 && currentColumn == 0)
            {
                FindPotentialPieces();
                if (potentialPieces.Count == 0)
                {
                    Debug.Log("no solutions were found");
                }
                SetActivePiece();           //thetaangles in piece are calculated here
                PlacePiece();
            }
            else
            {
                changedRows = CheckForRowChange();
                FindPotentialPieces();
                if (potentialPieces.Count == 0)
                {
                    Debug.Log("no potential pieces were found");
                    Backtrack();
                }
                SetActivePiece();
                PlacePiece();
            }

            bool overlap = OverLapsBoard();
            while (overlap == true)
            {
                Debug.Log("Overlap detected!");
                //if piece holds more angles matching theta
                //we test the next angle
                if (activePiece.GetComponent <PieceInfo>().thetaAngles.Count > 1)
                {
                    Pair temp = activePiece.GetComponent <PieceInfo>().thetaAngles[0];
                    activePiece.GetComponent <PieceInfo>().thetaAngles.Remove(temp);
                    indexOfTheta = activePiece.GetComponent <PieceInfo>().thetaAngles[0].index;
                    PlacePiece();
                    overlap = OverLapsBoard();
                    continue;
                    //if the piece only had one theta angle
                    //we'll remove this piece as the active and move on to other potential pieces
                }
                else if (activePiece.GetComponent <PieceInfo>().thetaAngles.Count == 1)
                {
                    Pair temp = activePiece.GetComponent <PieceInfo>().thetaAngles[0];
                    activePiece.GetComponent <PieceInfo>().thetaAngles.Remove(temp);
                    overlap = OverLapsBoard();
                    continue;
                }
                else
                {
                    potentialPieces.Remove(activePiece);
                    testedPieces.Add(new Triple(currentRow, currentColumn, activePiece));
                    if (potentialPieces.Count > 0)
                    {
                        SetActivePiece();
                        PlacePiece();
                        overlap = OverLapsBoard();
                    }
                    else
                    {
                        break;
                    }
                }
            }
            if (overlap == false)
            {
                placedPieces.Add(new Quadruple(currentRow, currentColumn, currentPoint, activePiece));
                testedPieces.Add(new Triple(currentRow, currentColumn, activePiece));
                Debug.Log("PLACED " + activePiece.GetComponent <PieceInfo>().name);
                pieces.Remove(activePiece);
                updateCurrentPoint(changedRows);
                currentColumn++;
            }
            else
            {
                Backtrack();
            }
        }
    }
 void SetName(JSONPuzzle puzzle, string name)
 {
     puzzle.name = name;
 }
Пример #7
0
    public List <Vector2> FindPiecesWithIdenticalLengthsAndAngles(List <float[]> lengthsOfPieces, List <float[]> anglesOfPieces, JSONPuzzle puzzle)
    {
        List <Vector2> piecesWithIdenticalArea = new List <Vector2>();

        if (lengthsOfPieces.Count > 1)
        {
            for (int outer = 0; outer < lengthsOfPieces.Count; outer++)
            {
                for (int inner = outer + 1; inner < lengthsOfPieces.Count; inner++)
                {
                    if (lengthsOfPieces[outer] == lengthsOfPieces[inner])
                    {
                        piecesWithIdenticalArea.Add(new Vector2(puzzle.pieces[outer].piece, puzzle.pieces[inner].piece));
                    }
                }
            }
        }
        return(piecesWithIdenticalArea);
    }