#module
#defcfunc 多角形内外判定 array a, int 頂点数, double ex, double ey
	count = 0
	a_max = 頂点数 * 2
	
	for i,, a_max, 2

		始点 = a(i), a(i + 1)
		終点 = a((i + 2) \ a_max), a((i + 3) \ a_max)

		if 始点 != 終点{
			判定 = (ex - 始点) * (終点.1 - 始点.1) / (終点 - 始点) + 始点.1 - ey
		}else{
			判定 = (ex != 始点) || ((ey - 始点.1) * (ey - 終点.1) > 0)
		}
		
		if 判定 == 0{
			//点が線分上にある
			return 1
		}
		
		if 判定 < 0: if ex < 始点 ^ ex < 終点{
			//線分が点の上を通過した
			if 始点 < 終点{
				count++
			}else{
				count--
			}
		}
		
	next
	
	return count != 0
#global
#include "a2d.hsp"

alcreateImage

title "多角形内外判定"

*main
	stick key

	if key & 256{
		a(頂点数 * 2) = mousex, mousey
		頂点数++
	}
	if key & 512{
		頂点数 = limit(頂点数 - 1, 0)
	}

	判定結果 = 多角形内外判定(a, 頂点数, mousex, mousey)
	
	redraw 2
		alColor 240,240,240
		alFillRect 0,0,640,480
		alColor 0,0,0
		if 判定結果: alColor 200,100,50
		alFillPoly a, 頂点数
		alColor 50,130,30
		alDrawPoly a, 頂点数
		alCopyImagetoScreen
		pos 0, 0
		color
		mes "左クリック: 頂点を作成
		mes "右クリック: 最後に作成した頂点を削除
		mes ""+mousex+","+mousey + " : " + 判定結果
	redraw

	await 16
goto*main