views:

303

answers:

3

EDIT (for clarification):
I have a vector image with a simple contour, an irregular closed polygon.
I need to import it into Flash in a way that I can then programmatically access each of the segments that form the polygon.
Importing the vector image into the library as a MovieClip wasn't good because all I get is a shape from which I can take no geometry information at all.
My goal is being able to calculate the polygon's area and also calculating the intersection between the polygon and another polygon.
I guess I could write an Illustrator script that reads all the segments and writes a CSV files with their coordinates, but there has to be a simpler way, I mean, they're both vectorial, they should understand each other.

Thanks!

.

-- Old Post: --

I have a contour in vector graphics that I imported to the Flash library as a movieclip.
I Instanciate the movieclip and it has a Shape child which is the actual contour.
I need to be able to access the contour segments, i.e. the polygon's sides, to be able to get their starting and ending points, is there a way?
the Graphics class only allows to draw but what you draw, as with the Shape class, are not objects, it's not a polygon with sides or whatever.
Am I being clear? Thanks

+1  A: 

I believe the best you can do is to retrieve a rectangular bounding box on the Shape object. Depending on how you imported it, you may or may not have direct access to the Shape object as an instance variable; however, if you do, you can call shapeVar.transform.getBounds() or shapeVar.transform.getRect() (bounds returns a rectangle inclusive of strokes on the shape, rect does not).

I'm curious, so I'm doing a bit of research on alternate means of getting some pixel bounds. I'll edit this further if I find something useful.

Tegeril
+2  A: 

There is no way to read the data of a Graphics object (which is essentially what contains the information that you are after.) This applies to any vector graphics object that has already been drawn, either by the Graphics/drawing API itself, or in Flash CS3/CS4, or was embedded using the [Embed] meta-tag.

Your best bet if you need to calculate the algebraic area, or for some other reason retain the vectors in your algorithms, is definitely exporting an SVG or some single-purpose format (like a CSV of the points) from Illustrator, and parsing that in ActionScript.

Another option is to use a BitmapData, and draw the Shape object onto that, then counting the colored (opaque) pixels to numerically calculate it's area.

var bmp : BitmapData = new BitmapData(myShape.width, myShape.height, true, 0);
bmp.draw(myShape);

var i : uint;
var area : uint = 0;
var num_pixels : uint = bmp.width*bmp.height;
for (i=0; i<num_pixels; i++) {

  var px : uint = bmp.getPixel32(i%bmp.width, Math.floor(i/bmp.height));

  // Determine from px color/alpha whether it's part of the shape or not.
  // This particular if statement should determine whether the alpha
  // component (first 8 bits of the px integer) are greater than zero, i.e.
  // not transparent.
  if ((px >> 24) > 0)
    area++;
}

trace('number of opaque pixels (area): '+area);

Depending on your application, you might also be able to use the BitmapData.hitTest() method for your collision detection.

richardolsson
that's the way to go ... i think SVG is the most elegant way to go, if you really want to work on the vector data (calculate line/curve intersections etc.) ... there's a number of SVG libraries out there for parsing and rendering ...
back2dos
Thanks! great answer. I think I'll go with an illustrator script that exports the segments to a CSV or something like that. I already thought of a very simple algorithm to split the polygon in triangles and add all their areas. Or is there a better way? Thanks again.
Petruza
Great, have fun! :) Regarding the area, there are generalized algorithms for polygon area on wikipeda: http://en.wikipedia.org/wiki/Polygon#Area_and_centroid
richardolsson
Holmes and Watson are on a train going through pastures. Holmes mentions casually: "216 cows, Watson". Watson says, "That's amazing, Holmes! How can you possible know that just by glancing out the window?" "Straightforward, Watson: just count the legs and divide by four."I know that counting pixels is a perfectly reasonable way to get the area: computers are very good at that sort of thing.Cheers
Richard Haven
+1  A: 

Check out my answer to a very similar question.

grapefrukt