• 追加された行はこの色です。
  • 削除された行はこの色です。
#author("2022-10-03T15:20:57+09:00","","")
#author("2022-10-03T18:52:10+09:00","","")
 #module
 #defcfunc 多角形内外判定 array a, int 頂点数, double ex, double ey
 
	if 頂点数 <= 0: return 0 
	if 頂点数 == 1: return a == ex && a.1 == ey
	
	count = 0
	
	始点 = a(頂点数 * 2 - 2), a(頂点数 * 2 - 1)
	
	for i,, 頂点数
	
		終点 = a(i * 2), a(i * 2 + 1)
		
		if ex < 始点 ^ ex < 終点{
			
			判定 = (ex - 始点) * (終点.1 - 始点.1) / (終点 - 始点) + 始点.1 - ey
			
			if 判定 >= 0{
				if 判定 == 0{
					return 1; 点は線分上にある
				}
				if 始点 < 終点{
					count++
				}else{
					count--
				}
			}
			
		}else: if ex == 始点{
			if ex == 終点{
				if (ey <= 始点.1 || ey <= 終点.1) && (ey >= 始点.1 || ey >= 終点.1){
					return 1 ; 点は線分上にある
				}
			}else: if ey == 始点.1{
				return 1 ; 点は頂点上にある
			}
		}
		
		始点 = 終点, 終点(1)
	next
 
	return count != 0
 
 #global

 #if 1
 #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
 
 #endif