Home > Actionscript 3, Actionscript 3, Flash > Point in Polygon with actionscript 3

Point in Polygon with actionscript 3

Testing whether a point is inside a polygon is a basic operation in computer graphics. Here a example of how to do that in a very simple way.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
private function insidePolygon(pointList:Array, p:Point):Boolean
{
	var counter:int = 0;
	var i:int;
	var xinters:Number;
	var p1:PointTest;
	var p2:PointTest;
	var n:int = pointList.length;
 
	p1 = pointList[0];
	for (i = 1; i <= n; i++)
	{
		p2 = pointList[i % n];
		if (p.y > Math.min(p1.y, p2.y))
		{
			if (p.y <= Math.max(p1.y, p2.y))
			{
				if (p.x <= Math.max(p1.x, p2.x))
				{
					if (p1.y != p2.y) {
						xinters = (p.y - p1.y) * (p2.x - p1.x) / (p2.y - p1.y) + p1.x;
						if (p1.x == p2.x || p.x <= xinters)
							counter++;
					}
				}
			}
		}
		p1 = p2;
	}
	if (counter % 2 == 0)
	{
		return(false);
	}
	else
	{
		return(true);
	}
}

DEMO
SOURCE
Reference

Categories: Actionscript 3, Actionscript 3, Flash Tags:
  1. October 28th, 2009 at 17:20 | #1

    Ahh! Excelente! Gracias por compartirlo. Estoy pensando en hacer una app que involucre recorte de figuras, algo me dice que esto me servirá para orientarme :)

  2. October 29th, 2009 at 05:04 | #2

    :) , ya te agregue a tweeter. Saludos

  1. No trackbacks yet.

Spam Protection by WP-SpamFree

Get Adobe Flash playerPlugin by wpburn.com wordpress themes