Exemplo n.º 1
0
    // Takes a contour and draws its silhouette
    public Silhouette DrawSilhouette(Contour contour)
    {
        Silhouette silhouette = new Silhouette(contour.Color);

        int pixelCount = 0;

        ContourPixel dummyPixel = new ContourPixel
        {
            IsDummy = true
        };

        int sX, sY, nX, nY;

        ContourPixel startPixel = FindNextStartPixel(contour, dummyPixel);
        ContourPixel nextPixel  = startPixel;

        bool started;

        //try
        //{

        while (startPixel != null && pixelCount != contour.NumberOfPixels)
        {
            started = false;

            // Add the first pixel of the new silhouette piece
            SilhouettePiece silhouettePiece = new SilhouettePiece();
            silhouettePiece.Points.AddFirst(startPixel);

            sX = startPixel.XCoord;
            sY = startPixel.YCoord;

            nX = startPixel.XCoord;
            nY = startPixel.YCoord;

            // Check if outside window
            //---------------------------------------------------------------------------------------------
            if (VectorUtils.AdjustIteration(contour, nX, nY) == "go-left-or-right")
            {
                //Debug.Log("Over eller under");
                if ((contour.Pixels[nX - 1, nY] != null) && !contour.Pixels[nX - 1, nY].Visited)
                {
                    nX = nX - 1;
                }
                else if ((contour.Pixels[nX + 1, nY] != null) && !contour.Pixels[nX + 1, nY].Visited)
                {
                    nX = nX + 1;
                }
            }
            else if (VectorUtils.AdjustIteration(contour, nX, nY) == "go-up-or-right")
            {
                //Debug.Log("Venstre");
                if ((contour.Pixels[nX, nY + 1] != null) && !contour.Pixels[nX, nY + 1].Visited)
                {
                    nY = nY + 1;
                }
                else if ((contour.Pixels[nX + 1, nY] != null) && !contour.Pixels[nX + 1, nY].Visited)
                {
                    nX = nX + 1;
                }
            }
            else if (VectorUtils.AdjustIteration(contour, nX, nY) == "go-up-or-left")
            {
                //Debug.Log("Høyre");
                if ((contour.Pixels[nX, nY + 1] != null) && !contour.Pixels[nX, nY + 1].Visited)
                {
                    nY = nY + 1;
                }
                else if ((contour.Pixels[nX - 1, nY] != null) && !contour.Pixels[nX - 1, nY].Visited)
                {
                    nX = nX - 1;
                }
            }
            //---------------------------------------------------------------------------------------------

            while (VectorUtils.MoreToDraw(contour, nX, nY) && (!(nX == sX && nY == sY) || !started))
            {
                //previousPixel = nextPixel;

                nextPixel = StepToNextPixel(contour, nX, nY, nextPixel);

                nX = nextPixel.XCoord;
                nY = nextPixel.YCoord;
                pixelCount++;

                silhouettePiece.Points.AddLast(nextPixel);

                started = true;
            }

            startPixel = FindNextStartPixel(contour, startPixel);
            silhouette.Pieces.Add(silhouettePiece);
        }

        /*} catch (Exception e)
         * {
         *  return silhouettes;
         * }*/

        return(silhouette);
    }