Point inside a circle

This is just something I wanted to share with you guys, in case you need it in the future :slight_smile:

I was figuring out if a point was located inside a circle. I want to use this to see if the user clicked in a rounded customized button on a canvas. Google was my friend. I found a formula to compare the distance of the point to the center of the circle with the circle’s radius.

Here is a Function that returns TRUE if the point is inside the circle. I added an optional mode value, as explained in code. Feel free to modify

Function CirclePoint(CircleX as integer, CircleY as integer, CircleRadius as integer, PointX as integer, PointY as integer, mode as integer = 0) As Boolean
    
  '=== Calculate the distance of the point to the center of the circle ===
    dim x2 as double  = ( (PointX - CircleX) ^ 2 )
    dim y2 as double = ( ( PointY - CircleY) ^ 2 )
    dim d as Double = sqrt( x2 + y2 )
  
  
  '=== Compare the distance of that point with the radius of the circle ===
    dim insideCircle as boolean =  (d < CircleRadius)
    dim onCircle as boolean = (d = CircleRadius)
    dim outsideCircle as boolean = (d > CircleRadius)
    
  
  '=== Return the result, according to the selected mode ===
    ' mode = 0: returns true, if point is ON INSIDE the circle     (Default mode)
    ' mode = 1: returns true, if point is INSIDE the circle
    ' mode = 2: returns true, if point is ON the cicle
    ' mode = 3: returns true, if point is OUTSIDE the circle
  
  
  select case mode
    
  case 1
    Return insideCircle
    
  case 2
    Return onCircle
    
  case 3 
    Return outsideCircle
    
  case else
    Return (insideCircle or onCircle)  ' DEFAULT (mode = 0)
    
  end select

End Function

Thanks for sharing Edwin.

Thank you!

You’re welcome :slight_smile:

I “borrowed” so many lines of code I use in my projects. This way I try to contribute in other people’s projects.

Edwin, does it ever return onCircle?

I would expect d never to be (exactly) equal to the radius. Well, yes, when you click on the pixel inmediately outside the circle line for pixels at the same x o y position as the center of the circle.

Julen

[quote=201676:@Julen Ibarretxe Uriguen]Edwin, does it ever return onCircle?
I would expect d never to be (exactly) equal to the radius.
Julen[/quote]

I think it is. What if I have to exactly the same circles, I take a random point on the first circle. That point will always be on the other circle.

But it doesn’t really matter. This mode might never be used. I just needed a way to easily find out if a user clicked on a round shape. I use it on an iOS app, where I have just one big canvas, with elements drawn on it. One element can be a button. When the user touches the canvas, and the location of this touch-event is within the radius of this circle I can raise an event. Simple… :slight_smile:

For your purpose it’s fine.

However, someone else might try to use it for something else, and get wrong results. The math to determine whether the point is in or out is using doubles and you are comparing the result to an integer (amount of pixels), so I think it will never be equal, even when they should.

Julen

I see what you mean, and you are right.
In that case the radius should be a double value. Although the precision of a double value is questionable.

No, I think the distance, d, should be rounded to an integer.

Julen

I’d make all the position and size parameters doubles and then see if the difference is < some very tiny delta (maybe user definable)
That way this works for devices defined in integral units (integers) and for code where floating point values are used

You can speed up the code by skipping the square root bit, since for nonnegative x, y, x < y if and only if sqrt(x) < sqrt(y). This also allows you to work in integer math when appropriate.