Click within circle

BlitzMax Forums/BlitzMax Programming/Click within circle

rs22(Posted 2011) [#1]
Hi,

I've searched the forums for examples of how to do this, but I couldn't get anything to work. Probably something I'm doing wrong, as opposed to a problem with the method.

Could someone please tell me how I can detect if the mouse is clicked within the circle?



Thanks!

Last edited 2011

Last edited 2011


Jesse(Posted 2011) [#2]
I didn't quite understand what you where trying to do so I did it my own way. I hope you understand it.
I think this should give you an idea how to do it.
SuperStrict
Graphics 640, 480


Type tcircle
	Field x:Float
	Field y:Float
	Field radius:Float
End Type

Local circle:tcircle = New Tcircle
circle.x = 300
circle.y = 300
circle.radius = 192/2

Local foundcircle:Int = False
Repeat

	Cls
	
	If foundcircle = True	SetColor(0,255,0) Else	SetColor(0, 0, 255)

	DrawOval(circle.x-circle.radius, circle.y-circle.radius, circle.radius*2, circle.radius*2)
	SetColor(255, 255, 255)
	DrawRect(circle.x-16, circle.y-16, 32, 32)

	Flip
	
	Local mx:Float = MouseX() - circle.x  '**********  
	Local my:Float = MouseY() - circle.y  '*********** 
	
	If Sqr(mx^2+my^2) < circle.radius '**************
		foundcircle = True
	Else
		foundcircle = False
	EndIf


Until KeyHit(KEY_ESCAPE) Or AppTerminate()



rs22(Posted 2011) [#3]
Thank you!


Arabia(Posted 2011) [#4]
I needed to do the same thing recently, but with an oval. A cicle is an oval, just with the same width and height, and I found this code by Warpy which works perfectly. I'm not going to try and dissect the maths behind Warpy & Jesse's code - but I'm guessing they are similar.

Just thought you might like to see another way of doing it which also works if you are using ovals.

http://www.blitzmax.com/Community/posts.php?topic=90460

p.s. I didn't run Jesse's code to see if it works with ovals, but I'm guessing not.


ziggy(Posted 2011) [#5]
If you remove the sqr is a bit faster. Changing If Sqr(mx^2+my^2) < circle.radius to If (mx^2+my^2) < circle.radius^2 does the job.


Warpy(Posted 2011) [#6]
and circle.radius*circle.radius is even faster than circle.radius^2! Jesse's method is the same idea as mine, but it doesn't do ovels, Arabia.