Determining if a point is within a circle

Hey guys,

I have need to write an applet that does the following:

1. Draws a circle

2. When a user clicks anywhere inside the circle and drags the mouse to

a different location the circle should be redrawn in that location when mouse is released.

My question is:

How do you determine weather a mouse click occurred within a circle? I think the question is rather mathematical, basically how to check if a point

A(x, y) is inside a circle.

Thanks for your help

[516 byte] By [_grinch_a] at [2007-11-26 18:51:04]
# 1
Use Shape.contains() method from Ellipse2D.Float or .Double object.
hiwaa at 2007-7-9 6:25:09 > top of Java-index,Java Essentials,Java Programming...
# 2

Hiwa already gave you the Java solution, but, in case you're wondering, the point is inside the circle if the distance between the point and the center of the circle is less than the radius of the circle.

So, assuming you have the following information.

Center of circle: xc, yc

Radius of circle: r

Point: xp, yp

if (xc - xp)^2 + (yc -yp)^2 < r^2 then the point is in the circle.

(Note that if you try to do this in Java, ^ is NOT a power operator.)

jverda at 2007-7-9 6:25:09 > top of Java-index,Java Essentials,Java Programming...
# 3

> Hey guys,

> I have need to write an applet that does the

> following:

> 1. Draws a circle

> 2. When a user clicks anywhere inside the circle and

> drags the mouse to

> a different location the circle should be redrawn in

> that location when mouse is released.

>

> My question is:

> How do you determine weather a mouse click occurred

> within a circle? I think the question is rather

> mathematical, basically how to check if a point

> A(x, y) is inside a circle.

>

> Thanks for your help

You click on a point (xi, yi); then, if the distance between the origin of the circle and (xi, yi) is less than the radius of the circle you are golden.

Carlemagnea at 2007-7-9 6:25:09 > top of Java-index,Java Essentials,Java Programming...
# 4
Guys, thanks a lot for your quick replies. I tried your solution Jverd as that's exactly what i was looking for - and it worked. Cheers!
_grinch_a at 2007-7-9 6:25:09 > top of Java-index,Java Essentials,Java Programming...