Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!

Distance between a point and a cubic curve

Veronique Kus
Ranch Hand
Posts: 41
Hi,
I need to implement a function that makes a cubic curve "selected" when the user clicks on it. In order to do that, I need to detect whether the mouse click happened on the cubic curve (or within a very short distance from it). So I need a function which, using pseudo-code would look like this:

if(distance between mouseClick and cubicCurve < 10){
make cubic curve selected
}else{
do not select curve
}

Unlike Line2D, cubic curve does not have a function that would tell the distance between a certain point (e.g. mouse event) and the curve. I was wondering how I could implement this myself? I found this thread. It seems relevant but I don't fully understand what this code does and how it could help me. Can anyone please give me some advice and/or help me with this example code???

Veronique Kus
Ranch Hand
Posts: 41
OK so I decided to implement it using a simpler method, I create a small rectangle around the click point and test whether any of the cubic curve drawn instersect this rectangle.

Ben Wood
Ranch Hand
Posts: 342
Hi Veronique,

I think that sounds like a good solution. As a general comment, in case you do not know about it, if you are doing a lot of work with 2D geometry - testing for distances, intersections, point-on-line etc) - there is quite a lot of functionality in the Java Topology Suite that you might find useful.

http://tsusiatsoftware.net/jts/main.html

cheers,
Ben