Drawing Outlines Around Organic Forms

One thing that seems especially easy to use with the Flash IDE, but which is difficult to make code with, is a sketch of an organic shape. In the IDE, you can simply use the inkbucket tool to draw a stroke around something. Using nothing but code seems a lot more complicated. One of the methods I've seen is to add a glow filter to the form in question and just ruin the power. But what if I only want to show the circuit?

What I would like to do is collect all the points that make up the edge of the form, and then just connect the points. I actually got to the point of collecting all the points with the detection of the fast and dirty edge of the script that I wrote. So now I have a vector of all the points that make up my shape. How to connect them in the correct sequence so that it really looks like the original object?

For anyone interested here, this is my edge detection script:

         // Create a new sprite which we'll use for our outline
        var sp:Sprite = new Sprite();
        var radius:int = 50;
        sp.graphics.beginFill(0x00FF00, 1);
        sp.graphics.drawCircle(0, 0, radius);
        sp.graphics.endFill();
        sp.x = stage.stageWidth / 2;
        sp.y = stage.stageHeight / 2;

        // Create a bitmap data object to draw our vector data
        var bmd:BitmapData = new BitmapData(sp.width, sp.height, true, 0);

        // Use a transform matrix to translate the drawn clip so that none of its
        // pixels reside in negative space. The draw method will only draw starting
        // at 0,0
        var mat:Matrix = new Matrix(1, 0, 0, 1, radius, radius);
        bmd.draw(sp, mat);

        // Pass the bitmap data to an actual bitmap
        var bmp:Bitmap = new Bitmap(bmd);

        // Add the bitmap to the stage
        addChild(bmp);

        // Grab all of the pixel data from the bitmap data object
        var pixels:Vector.<uint> = bmd.getVector(bmd.rect);

        // Setup a vector to hold our stroke points
        var points:Vector.<Point> = new Vector.<Point>;

        // Loop through all of the pixels of the bitmap data object and
        // create a point instance for each pixel location that isn't
        // transparent.
        var l:int = pixels.length;
        for(var i:int = 0; i < l; ++i)
        {
            // Check to see if the pixel is transparent
            if(pixels[i] != 0)
            {
                var pt:Point;

                // Check to see if the pixel is on the first or last
                // row. We'll grab everything from these rows to close the outline
                if(i <= bmp.width || i >= (bmp.width * bmp.height) - bmp.width)
                {
                    pt = new Point();
                    pt.x = int(i % bmp.width);
                    pt.y = int(i / bmp.width);
                    points.push(pt);
                    continue;
                }

                // Check to see if the current pixel is on either extreme edge
                if(int(i % bmp.width) == 0 || int(i % bmp.width) == bmp.width - 1)
                {
                    pt = new Point();
                    pt.x = int(i % bmp.width);
                    pt.y = int(i / bmp.width);
                    points.push(pt);
                    continue;
                }

                // Check to see if the previous or next pixel are transparent,
                // if so save the current one.
                if(i > 0 && i < bmp.width * bmp.height)
                {
                    if(pixels[i - 1] == 0 || pixels[i + 1] == 0)
                    {
                        pt = new Point();
                        pt.x = int(i % bmp.width);
                        pt.y = int(i / bmp.width);
                        points.push(pt);
                    }
                }
            }
        }

code>

+3
source share
1 answer

, (.. , ), - . , , , . Flash , , , , .

, : . , , , . , knockout. ( script IDE.) , , - , . , - , BitmapData.threshold, . , , , , - , , , .

- PixelBender. , C, , Flash- . . , , , Flash ActionScript.

- , , , , -, . , , , , , ( setPixel32) . , , , . , - , , .

+2

Source: https://habr.com/ru/post/1737881/


All Articles