draw to memory

Hi all,

i need to draw a polygon (non-convex) into memory, so that i can query which pixels are inside the polygon and which not. I came across matplotlib which has some Polygon class in it so i presume i could use this (although so far, i didn't understand the terminology behind the library and how to string things together... patches? artists? O_O)

What is the easiest way to draw the filled polygon, defined by a sequence of points, into some 1bit raster, so that i can answer queries like "Is the pixel at [10, 12] inside the polygon?"

Cheers!

hi, did you find this?
http://matplotlib.sourceforge.net/faq/howto_faq.html?highlight=codex%20nxutils#test-whether-a-point-is-inside-a-polygon

···

On Wed, Mar 18, 2009 at 2:48 PM, crwe crwe <crwe@...2525...> wrote:

Hi all,

i need to draw a polygon (non-convex) into memory, so that i can query which pixels are inside the polygon and which not. I came across matplotlib which has some Polygon class in it so i presume i could use this (although so far, i didn't understand the terminology behind the library and how to string things together... patches? artists? O_O)

What is the easiest way to draw the filled polygon, defined by a sequence of points, into some 1bit raster, so that i can answer queries like "Is the pixel at [10, 12] inside the polygon?"

Cheers!

------------------------------------------------------------------------------
Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are
powering Web 2.0 with engaging, cross-platform capabilities. Quickly and
easily build your RIAs with Flex Builder, the Eclipse(TM)based development
software that enables intelligent coding and step-through debugging.
Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

hi, did you find this?
http://matplotlib.sourceforge.net/faq/howto_faq.html?highlight=codex%20nxutils#test-whether-a-point-is-inside-a-polygon

I used the search function but no, i didn't find that link :slight_smile: Shame on me and thank you Brent.

Looking at the implementation, it's really simple and i will do without matplotlib altogether and roll my own. In case anyone else needs this, the code is:

def insidePoly(x, y, xs, ys):
    """Decide whether point (x, y) lies inside the 2d polygon defined by its vertices zip(xs, ys).
    """
    result = False
    for now in xrange(len(xs)):
        if (ys[now] < y and ys[now - 1] >= y) or (ys[now - 1] < y and ys[now] >= y):
            if xs[now] + 1.0 * (y - ys[now]) * (xs[now - 1] - xs[now]) / (ys[now - 1] - ys[now]) < x:
                result = not result
    return result

crwe crwe wrote:

Looking at the implementation, it's really simple and i will do
without matplotlib altogether and roll my own.

It sounds like you have a solution, but if you need to test a LOT of points, it can be efficient to rasterize, and then get a lightning fast check.

It that case, though, I'd be inclined to use something like PIL's ImageDraw, rather than MPL -- all of MPL's scaling, anti-aliasing, etc. will just make it harder.

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@...259...