RCap107
RCap107

Reputation: 287

How can I figure out whether a region belongs to a shape or not?

I am trying to write a program which draws circles and connects them using lines (I'm still learning the language) and I would like to perform a check on the mouse coordinates whenever I click on the screen. This check should be able to tell me whether the mouse is inside a particular shape or not.

I am aware that I can (of course) build some if instructions to define a rectangle and check if the mouse is inside the rectangle or not, but I can't figure out how to do this with any shape, so my question is:

Is it possible to write a function able to tell me if the mouse pointer (or any other point in the plane) is within a particular region regardless of its shape?

Another use would be, for example, checking whether a line I'm drawing crosses a circle which is already on the plane.

Upvotes: 0

Views: 35

Answers (1)

Kevin Workman
Kevin Workman

Reputation: 42174

Try googling "circle line collision" or "polygon point collision" for a ton of results.

Assuming you're using Java mode, you can also use the Polygon class, which has a contains(x, y) method: https://docs.oracle.com/javase/8/docs/api/java/awt/Polygon.html#contains-double-double-

Upvotes: 1

Related Questions