
Centre of Irregular Shape
I have a question for all you geometry / math wizards out there as this problem
has certainly gone past my rather flimsy knowledge of the stuff.
Using Imaging Lingo, I am sort of replicating the function of a polygonal lasso
tool which you would find in many drawing apps such as Photoshop. For those of
you who don't know how it works, the user clicks at various points on the screen
to define an irregular shape for a selection.
Now so far ive done this fine using Lingo  ive written some code to allow
points (x and y) to be generated from the mousedown event, points to be dragged
and moved and you can close the shape no problem.. However I now need to find
the CENTRE of this shape, as I want to floodfill() it rather than make a
selection (simple drawing app you see) In reality, I guess I don't need to find
out what the center of the shape is, just a point which is inside the shape so
the floodfill() method fills the 'right' side of the shape, if you get me.
And this is where Ive been getting lost.
I have all the X and Y coordinates of the points stored in two lists so I have
easy access to them. A partial solution would be to find the bounding box of
this defined shape and fill from the center of that. This would work for
regular shapes such as simple boxes, but for stranger polygons I cant rely on
this.
I'm thinking it may involve turning the shape into separate triangles and
finding the centre of each, but I really don't know and the maths of it has gone
beyond me already. Does anybody know if this is possible or can point me in the
direction of some formula (or even lingo) to use? Otherwise I may just have to
get the user to do the floodfill themselves by forcing them to click inside
their shape with a fill can. Not as much fun of course, and I would ideally like
to crack this one.
Thanks
Chris

Re: Centre of Irregular Shape
http://www.cs.rit.edu/~icss571/filli..._is_poly.html\
This is the best general tutorial I know of. It should give you some ideas to solve your dilema. Good reading!

Re: Centre of Irregular Shape
Here's another link:
http://softsurfer.com/Archive/algori...rithm_0103.htm
a bit more involved, but you may be able to figure out something from it... I
don't know myself how to implement it.
You might also be able to utilize the mapStageToMember() command to get your
floodfill working... but again, I'm not too familiar with this command. I've
seen it mentioned before and recall an example that suggests you could use it
somehow to find if a point is within your polygon.
Oh, and if you need to get a point to test the best thing is to take and three
of your points from your list and derive a point from that: (x1 + x2 + x3)/3 =
xvalue of point, (y1 + y2 + y3)/3 = yvalue of point. You still must test
because the point could lie outside the polygon area.

Re: Centre of Irregular Shape
Finding out whether or not a point is inside or outside a polygon is a classic
math question. You basically draw a line from the point to somewhere beyond the
polygon edges and then find all the points of intersection (yes, there is math
in that part). If it intersects an odd number of points it is inside the
polygon, an even number means outside.
Great visualization of that at (although they use a full line), along with a
code snippet in fortran, or C or something, but can be converted.
http://www.alienryderflex.com/polygon/
This method definitely wouldn't be very fast for what you want to do, but is
interesting ;)
Another good visualizing link is at (with C code):
http://astronomy.swin.edu.au/~pbourk...ry/insidepoly/

Re: Centre of Irregular Shape
"ExploreLearning" <webforumsuser@macromedia.com> wrote in message
news:cfu3f0$cfn$1@forums.macromedia.com...
> Finding out whether or not a point is inside or outside a polygon is a
classic
> math question. You basically draw a line from the point to somewhere
beyond the
> polygon edges and then find all the points of intersection (yes, there is
math
> in that part). If it intersects an odd number of points it is inside the
> polygon, an even number means outside.
Surely if one is trying to find a point (any point) that lies inside the
shape, this can be done in reverse  start outside the shape, and working in
a line across the shape test whether you're on an edge pixel or not. Stop on
the next pixel after you detect an edge and do the floodfill there. Suppose
you could get caught out if the shape has any 1 pixel wide or less
regions...
just 2 pennies
h

Re: Centre of Irregular Shape
> Surely if one is trying to find a point (any point) that lies inside
the
> shape, this can be done in reverse  start outside the shape, and
working in
> a line across the shape test whether you're on an edge pixel or not.
Stop on
> the next pixel after you detect an edge and do the floodfill there.
Suppose
> you could get caught out if the shape has any 1 pixel wide or less
> regions...
How do you reliably determine where outside the shape is? And that way
wouldn't work if the first edge it encountered was in the direction of
the detection.
My suggestion: give the user a paint bucket tool and let them choose
where to start the floodfill <g>.
Andrew