<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: polygon code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 24 Jul 2008 02:03:01 GMT</pubDate>
    <description>DZone Snippets: polygon code</description>
    <item>
      <title>Point Inside a Polygon //JavaScript Function</title>
      <link>http://snippets.dzone.com/posts/show/5295</link>
      <description>&lt;a href="http://jsfromhell.com/math/is-point-in-poly"&gt;&lt;br /&gt;Checks whether a point is inside a polygon.&lt;br /&gt;Adapted from: [http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html]&lt;br /&gt;&lt;br /&gt;[UPDATED CODE AND HELP CAN BE FOUND HERE: Point Inside a Polygon]&lt;br /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;//+ Jonas Raoni Soares Silva&lt;br /&gt;//@ http://jsfromhell.com/classes/is-point-in-poly [v1.0]&lt;br /&gt;&lt;br /&gt;function isPointInPoly(poly, pt){&lt;br /&gt;	for(var c = false, i = -1, l = poly.length, j = l - 1; ++i &lt; l; j = i)&lt;br /&gt;		((poly[i].y &lt;= pt.y &amp;&amp; pt.y &lt; poly[j].y) || (poly[j].y &lt;= pt.y &amp;&amp; pt.y &lt; poly[i].y))&lt;br /&gt;		&amp;&amp; (pt.x &lt; (poly[j].x - poly[i].x) * (pt.y - poly[i].y) / (poly[j].y - poly[i].y) + poly[i].x)&lt;br /&gt;		&amp;&amp; (c = !c);&lt;br /&gt;	return c;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Example&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;script type="text/javascript"&gt;&lt;br /&gt;//&lt;![CDATA[&lt;br /&gt;&lt;br /&gt;points = [&lt;br /&gt;	{x: 0, y: 0},&lt;br /&gt;	{x: 0, y: 50},&lt;br /&gt;	{x: 50, y: 10},&lt;br /&gt;	{x: -50, y: -10},&lt;br /&gt;	{x: 0, y: -50},&lt;br /&gt;	{x: 0, y: 0}&lt;br /&gt;];&lt;br /&gt;&lt;br /&gt;alert(isPointInPoly(points, {x: 10, y: 10}) ? "In" : "Out");&lt;br /&gt;&lt;br /&gt;//]]&gt;&lt;br /&gt;&lt;/script&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Sun, 30 Mar 2008 16:57:56 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5295</guid>
      <author>jonasraoni (Jonas Raoni Soares Silva)</author>
    </item>
    <item>
      <title>Drawing an arrow cursor</title>
      <link>http://snippets.dzone.com/posts/show/1452</link>
      <description>Calling polygon may be faster than a blit.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;x,y = 20,20&lt;br /&gt;arrow = [(0,0), (0,10), (2,8), (4,12), (6,11), (4,7), (7,7)]&lt;br /&gt;canvas.polygon([(x+dx,y+dy) for dx,dy in arrow], 0, 0xffffff)&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Here's an example app. It let you move cursor around.&lt;br /&gt;When you press the select key, a red circle is drawn.&lt;br /&gt;The longer you press, the bigger the circle.&lt;br /&gt;&lt;br /&gt;Start with the usual stuff&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import e32&lt;br /&gt;from appuifw import *&lt;br /&gt;from key_codes import *&lt;br /&gt;&lt;br /&gt;class Keyboard(object):&lt;br /&gt;    def __init__(self):&lt;br /&gt;        self.state = {}  # is this key pressing ?&lt;br /&gt;        self.buffer= {}  # is it waiting to be processed ?&lt;br /&gt;    def handle_event(self, event): # for event_callback&lt;br /&gt;        code = event['scancode']&lt;br /&gt;        if event['type'] == EEventKeyDown:&lt;br /&gt;            self.buffer[code]= 1   # put into queue&lt;br /&gt;            self.state[code] = 1&lt;br /&gt;        elif event['type'] == EEventKeyUp:&lt;br /&gt;            self.state[code] = 0&lt;br /&gt;    def pressing(self, code):      # just check&lt;br /&gt;        return self.state.get(code,0)&lt;br /&gt;    def pressed(self, code):       # check and process the event&lt;br /&gt;        if self.buffer.get(code,0):&lt;br /&gt;            self.buffer[code] = 0  # take out of queue&lt;br /&gt;            return 1&lt;br /&gt;        return 0&lt;br /&gt;&lt;br /&gt;key = Keyboard()&lt;br /&gt;app.body = canvas = Canvas(event_callback=key.handle_event)&lt;br /&gt;&lt;br /&gt;def quit():&lt;br /&gt;    global running&lt;br /&gt;    running = 0&lt;br /&gt;&lt;br /&gt;app.exit_key_handler = quit&lt;br /&gt;running = 1&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Then the main part of this specific app.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;x,y = 20,20&lt;br /&gt;arrow = [(0,0), (0,10), (2,8), (4,12), (6,11), (4,7), (7,7)]&lt;br /&gt;&lt;br /&gt;while running:&lt;br /&gt;    if key.pressing(EScancodeUpArrow):&lt;br /&gt;        y -= 1&lt;br /&gt;    if key.pressing(EScancodeDownArrow):&lt;br /&gt;        y += 1&lt;br /&gt;    if key.pressing(EScancodeLeftArrow):&lt;br /&gt;        x -= 1&lt;br /&gt;    if key.pressing(EScancodeRightArrow):&lt;br /&gt;        x += 1&lt;br /&gt;    if key.pressed(EScancodeSelect):&lt;br /&gt;        r = 1&lt;br /&gt;        while key.pressing(EScancodeSelect):&lt;br /&gt;            r += 1       # bigger red circle&lt;br /&gt;            canvas.ellipse([(x-r,y-r),(x+r,y+r)], fill=0xff0000)&lt;br /&gt;            canvas.polygon([(x+dx,y+dy) for dx,dy in arrow], 0, 0xffffff)&lt;br /&gt;            e32.ao_sleep(0.03)&lt;br /&gt;&lt;br /&gt;    canvas.clear()&lt;br /&gt;    canvas.polygon([(x+dx,y+dy) for dx,dy in arrow], 0, 0xffffff)&lt;br /&gt;    e32.ao_sleep(0.01)&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Fri, 10 Feb 2006 09:06:11 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1452</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
  </channel>
</rss>
