Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Point Inside a Polygon //JavaScript Function


Checks whether a point is inside a polygon.
Adapted from: [http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html]

[UPDATED CODE AND HELP CAN BE FOUND HERE: Point Inside a Polygon]



//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/classes/is-point-in-poly [v1.0]

function isPointInPoly(poly, pt){
	for(var c = false, i = -1, l = poly.length, j = l - 1; ++i < l; j = i)
		((poly[i].y <= pt.y && pt.y < poly[j].y) || (poly[j].y <= pt.y && pt.y < poly[i].y))
		&& (pt.x < (poly[j].x - poly[i].x) * (pt.y - poly[i].y) / (poly[j].y - poly[i].y) + poly[i].x)
		&& (c = !c);
	return c;
}


Example

<script type="text/javascript">
//<![CDATA[

points = [
	{x: 0, y: 0},
	{x: 0, y: 50},
	{x: 50, y: 10},
	{x: -50, y: -10},
	{x: 0, y: -50},
	{x: 0, y: 0}
];

alert(isPointInPoly(points, {x: 10, y: 10}) ? "In" : "Out");

//]]>
</script>

Drawing an arrow cursor

Calling polygon may be faster than a blit.
x,y = 20,20
arrow = [(0,0), (0,10), (2,8), (4,12), (6,11), (4,7), (7,7)]
canvas.polygon([(x+dx,y+dy) for dx,dy in arrow], 0, 0xffffff)

Here's an example app. It let you move cursor around.
When you press the select key, a red circle is drawn.
The longer you press, the bigger the circle.

Start with the usual stuff
import e32
from appuifw import *
from key_codes import *

class Keyboard(object):
    def __init__(self):
        self.state = {}  # is this key pressing ?
        self.buffer= {}  # is it waiting to be processed ?
    def handle_event(self, event): # for event_callback
        code = event['scancode']
        if event['type'] == EEventKeyDown:
            self.buffer[code]= 1   # put into queue
            self.state[code] = 1
        elif event['type'] == EEventKeyUp:
            self.state[code] = 0
    def pressing(self, code):      # just check
        return self.state.get(code,0)
    def pressed(self, code):       # check and process the event
        if self.buffer.get(code,0):
            self.buffer[code] = 0  # take out of queue
            return 1
        return 0

key = Keyboard()
app.body = canvas = Canvas(event_callback=key.handle_event)

def quit():
    global running
    running = 0

app.exit_key_handler = quit
running = 1

Then the main part of this specific app.
x,y = 20,20
arrow = [(0,0), (0,10), (2,8), (4,12), (6,11), (4,7), (7,7)]

while running:
    if key.pressing(EScancodeUpArrow):
        y -= 1
    if key.pressing(EScancodeDownArrow):
        y += 1
    if key.pressing(EScancodeLeftArrow):
        x -= 1
    if key.pressing(EScancodeRightArrow):
        x += 1
    if key.pressed(EScancodeSelect):
        r = 1
        while key.pressing(EScancodeSelect):
            r += 1       # bigger red circle
            canvas.ellipse([(x-r,y-r),(x+r,y+r)], fill=0xff0000)
            canvas.polygon([(x+dx,y+dy) for dx,dy in arrow], 0, 0xffffff)
            e32.ao_sleep(0.03)

    canvas.clear()
    canvas.polygon([(x+dx,y+dy) for dx,dy in arrow], 0, 0xffffff)
    e32.ao_sleep(0.01)

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS