Detect mouse in an area?

BlitzMax Forums/BlitzMax Programming/Detect mouse in an area?

coffeedotbean(Posted 2007) [#1]
Hi, I need a little function where I can specify 4 points and if the mouse crosses into the area the 4 points set out I will return true.

Like :


MouseInArea(point1,point2,point3,point4)



heres a pick to explain a little more, I could attack am image to the mouse pointer and use image collide but that seems messy, must be a way to know if the mouse moves between the 4 points.




tonyg(Posted 2007) [#2]
I think you're looking for point in poly and there's a few in the Code Archives.
One
two
three
Might be some more as well.


coffeedotbean(Posted 2007) [#3]
wohoo.. great just what I was after.. thanks


SculptureOfSoul(Posted 2007) [#4]
Instead of using collideimage and attaching an image to the mouse, you can use CollideRect( MouseX(), MouseY(), 1, 1, CollisionLayers, WriteLayers) to see if the mouse is overlapping an image object. This creates a 1 pixel by 1 pixel rectangle at the mouse pointer and checks it for collisions.

Of course, that would only work if your "Play Game" sign above was a separate image and not part of the surrounding image. If it's part of the surrounding image, the methods Tony linked to are your best bet.


coffeedotbean(Posted 2007) [#5]
Hi,

Nice idea but still more work than is really required for this simple task.

I have tried skidracers pointinpoly but cant seem to get it to work, heres my code.. any 1 see where I am going wrong? For this test im just checking a 64x64 sqaure in the top left, plotting the points anti-clockwise (tried all other directions also) cant seem to get it to respond though =(

Graphics 800,600,0,60

SetBlend ALPHABLEND
Repeat ; cls

	If InsideQuad(mousex(),mousey(),0,0,0,64,64,64,64,0)=1 Then DrawText "wohoo",200,200 Else DrawText "doh!",200,200
	DrawText MouseX()+"/"+Mousey(),10,10
	
Flip ; Until KeyDown(KEY_ESCAPE) ; End

Function dot(x0,y0,x1,y1,x2,y2)
	Return (x1-x0)*(y2-y1)-(x2-x1)*(y1-y0)
End Function

Function InsideQuad(px,py,x0,y0,x1,y1,x2,y2,x3,y3)
	If dot(x0,y0,x1,y1,px,py)>0
		If dot(x1,y1,x2,y2,px,py)>0
			If dot(x2,y2,x3,y3,px,py)>0
				If dot(x3,y3,x0,y0,px,py)>0
					Return True
				EndIf
			EndIf
		EndIf
	EndIf
End Function



Grey Alien(Posted 2007) [#6]
SculptureOfSoul. Ha looks like I should have used that, I made a 1 pixel image as my mouse pointer and used CollideImage! haha. [edit] bah, it didn't work and I've no idea why...I had this

CollideImage(Pixel.Image, mx, my, 0, 1, 0)


and I changed it for this...

CollideRect(mx,my,1,1,1,0)

oh well.


SculptureOfSoul(Posted 2007) [#7]
edit: deleted because my mistake was trivial.


Grey Alien(Posted 2007) [#8]
Oops, I just put back my old code and now that doesn't work so I must have broken something. Please ignore previous post.


SculptureOfSoul(Posted 2007) [#9]
edit: deleted since content is no longer relevant. :P


coffeedotbean(Posted 2007) [#10]
found this version in archives.. works a treat =D

Graphics 800,600,0,60

SetBlend ALPHABLEND
Repeat ; cls

	If GenericPointInQuad(mousex(),mousey(),0,0,64,0,64,64,0,64)=1 Then DrawText "wohoo",200,200 Else DrawText "doh!",200,200
	DrawText MouseX()+"/"+Mousey(),10,10
	
Flip ; Until KeyDown(KEY_ESCAPE) ; End

Function GenericPointInQuad (x, y, x0, y0, x1, y1, x2, y2, x3, y3) ' clockwise
	If ((y - y0) * (x1 - x0)) - ((x - x0) * (y1 - y0)) <= 0 Then Return 0
	If ((y - y1) * (x2 - x1)) - ((x - x1) * (y2 - y1)) <= 0 Then Return 0
	If ((y - y2) * (x3 - x2)) - ((x - x2) * (y3 - y2)) <= 0 Then Return 0
	If ((y - y3) * (x0 - x3)) - ((x - x3) * (y0 - y3)) <= 0 Then Return 0
	Return True
End Function



Grey Alien(Posted 2007) [#11]
snaffled.