• 追加された行はこの色です。
  • 削除された行はこの色です。
#author("2022-10-06T00:47:01+09:00","","")
#author("2022-10-06T17:45:11+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 の時はここに来ない。
 
			;      (        線分が点の上もしくは下を通る時のY座標         ) - 点のY座標
 	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
			
			if 判定 >= 0{
				if 判定 == 0{
					return 1; 点は線分上にある
				}
		}else{
			判定 = (ex != 始点) || ((始点.1 - ey) * (終点.1 - ey) > 0)
		}
		if 判定 < 0{
			if ex < 始点 ^ ex < 終点{
				if 始点 < 終点{
					count++
				}else{
					count--
				}
			}
			
		}else: if ex == 始点{
			if ey == 始点.1{
				return 1 ; 点は頂点上にある
			}
			if ex == 終点{
				if ey < 始点.1 ^ ey < 終点.1{
					return 1 ; 点は線分上にある
				}
			}
		}
		
		始点 = 終点, 終点(1)
		}else: if 判定 == 0: return 1 ; 点は線分上にある。
	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