private void Pieces_TouchDown(object sender, TouchEventArgs e)
 {
     if (OnDragD)
         return;
     OnDragD = true;
     SelectedPiece = (Piece)sender;
     SelectedPiece.ClearImage();
     SelectedShadow = shadowPieces[SelectedPiece.Index];
     // Object Translation
     SelectedShadow.MovePiece(-300, 0);
     if (!SelectedShadow.isInCanvas)
     {
         cnvTable.Children.Add(SelectedShadow);
         SelectedShadow.isInCanvas = true;
     }
     PtFig = Mouse.GetPosition(SelectedPiece);
     ListBoxPieces.SelectedIndex = -1;
 }
        double GetDistanceTwoPices(Piece A, Piece B)
        {
            double XA = GetCoordenate(A, true);
            double XB = GetCoordenate(B, true);
            double YA = GetCoordenate(A, false);
            double YB = GetCoordenate(B, false);
            XA -= XB;
            YA -= YB;
            XA = Math.Pow(XA, 2);
            YA = Math.Pow(YA, 2);

            return Math.Sqrt(XA + YA);
        }
        double GetAngleTwoPices(Piece A, Piece B)
        {
            double AngA = A.Angle;
            double AngB = B.Angle;

            return Math.Abs(AngA - AngB);
        }
        double GetCoordenate(Piece A, bool Ax)
        {
            //If there is not elemnt
            if (A == null)
                return 0;

            //Normal way
            double XA;
            if(Ax)
                XA= A.RenderTransform.Value.OffsetX;
            else
                XA = A.RenderTransform.Value.OffsetY;

            if ((A.Parent.GetType()).Name != "Canvas")
                return GetCoordenate((Piece)A.Parent, Ax) + XA; //Offset in X for Group
            else
                return XA;
        }
        void CreatePuzzle()
        {
            //Create the diferents angles
            var angles = new int[] { 0, 90, 180, 270 };
            Random rnd = new Random();
            var connections = new int[] { (int)ConnectionType.Tab, (int)ConnectionType.Blank };

            //Create Pieces
            for (var y = 0; y < rows; y++)
            {
                for (var x = 0; x < columns; x++)
                {
                    if (x != 1000) //Max Pieces
                    {
                        int upperConnection = (int)ConnectionType.None;
                        int rightConnection = (int)ConnectionType.None;
                        int bottomConnection = (int)ConnectionType.None;
                        int leftConnection = (int)ConnectionType.None;

                        if (y != 0)
                            upperConnection = -1 * pieces[(y - 1) * columns + x].BottomConnection;

                        if (x != columns - 1)
                            rightConnection = connections[rnd.Next(2)];

                        if (y != rows - 1)
                            bottomConnection = connections[rnd.Next(2)];

                        if (x != 0)
                            leftConnection = -1 * pieces[y * columns + x - 1].RightConnection;

                        //Piece for selection bar
                        var piece = new Piece(imageSource, x, y, (int)upperConnection, (int)rightConnection, (int)bottomConnection, (int)leftConnection, pieces.Count(), 1.0);
                        //Set Feedbacks Events for each piece
                        piece.TouchDown += Pieces_TouchDown;
                        piece.TouchUp += Pieces_TouchUp;
                        piece.TouchMove += Pieces_TouchMove;
                        //Piece to play in Canvas Object
                        var shadowPiece = new Piece(imageSource, x, y, (int)upperConnection, (int)rightConnection, (int)bottomConnection, (int)leftConnection, shadowPieces.Count(), 1.5);

                        //Delete search for build validation
                        if (rightConnection == 0)
                            shadowPiece.NeighbourX = true;

                        if (bottomConnection == 0)
                            shadowPiece.NeighbourY = true;

                        //Add Pieces to the respective List
                        pieces.Add(piece);
                        shadowPieces.Add(shadowPiece);
                    }
                }
            }

            //Rotation of Pieces for game
            foreach (var p in pieces)
            {
                Random random = new Random();
                int i = random.Next(0, ListBoxPieces.Items.Count);
                p.IsSelected = false;

                double angle = angles[rnd.Next(0, 4)];
                p.Rotate(angle);
                p.MovePiece(20, 20);
                shadowPieces[p.Index].Rotate(angle);
                //shadowPieces[p.Index].RotatePiece(angle, 70, 70);
            }

            //Set List
            ListBoxPieces.ItemsSource = pieces;
        }