user1383359
user1383359

Reputation: 2753

Java: Fast 2D Hit Detection for Mouse Click

Context

I am using Java Graphics2D.

I am drawing a bunch of shapes.

I want to be able to do fast hit detection -- when the mouse clicks an object, I want to know what object is clicked.

More Details:

I would like to do this at the level of fill detection. I.e. if I have a circle, I don't want hit detection on the bounding box of the circle, I want hit detection on the inside of the Circle.

For strokes/lines/fonts, I'm okay with doing hit detection against the box (rather than the black part of the letters).

Question:

What APIs should I be using to set this up? [And out of courisity, what algoritm does Java use / what is it's big-OH running time?]

Thanks!

Upvotes: 0

Views: 1029

Answers (1)

meriton
meriton

Reputation: 70584

Ask each Shape with shape.contains(point)?

That's O(number of shapes), but you probably don't need anything faster. Assuming the hit test on a shape takes 1 microsecond, and you have 100,000 shapes, you can still process 10 clicks a second ...

Upvotes: 1

Related Questions