selecting a line in a drawn graph??

D

Digital Puer

I'm planning to write a graphing application that will draw lines
between plotted points (in a cartesian plane). Once I draw the lines,
how would I be able to click on a drawn line to select it? I would
then allow the user to change the color of the line.

The only way I can think of is to find the point the user clicked and
then see if it fits on the line between any two pairs of points, but
this is highly inefficient and potentially inaccurate.

I looked at JFreeChart, but they evidently don't offer this functionality.

Thanks for any help.
 
E

Eric Sosman

Digital said:
I'm planning to write a graphing application that will draw lines
between plotted points (in a cartesian plane). Once I draw the lines,
how would I be able to click on a drawn line to select it? I would
then allow the user to change the color of the line.

The only way I can think of is to find the point the user clicked and
then see if it fits on the line between any two pairs of points, but
this is highly inefficient and potentially inaccurate.

I looked at JFreeChart, but they evidently don't offer this functionality.

(This question might be more appropriate for one of
the graphics groups -- comp.graphics.algorithms seems a
good candidate.)

One approach is to consider the clicked point as the
center of a small square of maybe three to five pixels on
a side. Then go through your list of lines and determine
which of them intersect the square. If the intersection
count is one (or zero) you're all set, otherwise you can
try to disambiguate them by seeing which one passes closest
to the clicked point, or throw up your hands and ask the
user to click on a less ambiguous point where the lines
aren't quite so close together.

Or you could calculate the distance (squared distance,
most likely) from the clicked point to the closest point
on each line segment, which will be one of the endpoints
or the "perpendicular point" in between them. Then choose
the line with the closest approach, provided it's smaller
than some reasonable threshold.

Yet another method is to let the line itself tell you.
Draw each line in a different color, then retrieve the
color of the pixel at the clicked point and see which line
it belongs to.

Or ... Aww, the heck with it. Go to c.g.a. and
read their FAQ.
 
A

ak

The only way I can think of is to find the point the user clicked and
Can't think of any other way of doing it.

If:

mx+c-e < y < mx+c+e

select the line y=mx+c

m - gradient
c - intercept
e - error of clicking near line (choose a value)
x - coord
y - coord

do you know how to detect click on bezier curve (with known 2 control and 2
end points)
and/or quad curve (with known 2 endpoints and 1 control point)?

thanks
 
V

VisionSet

Digital Puer said:
I'm planning to write a graphing application that will draw lines
between plotted points (in a cartesian plane). Once I draw the lines,
how would I be able to click on a drawn line to select it? I would
then allow the user to change the color of the line.

The only way I can think of is to find the point the user clicked and
then see if it fits on the line between any two pairs of points, but
this is highly inefficient and potentially inaccurate.

Can't think of any other way of doing it.

If:

mx+c-e < y < mx+c+e

select the line y=mx+c

m - gradient
c - intercept
e - error of clicking near line (choose a value)
x - coord
y - coord
 
V

VisionSet

ak said:
do you know how to detect click on bezier curve (with known 2 control and 2
end points)
and/or quad curve (with known 2 endpoints and 1 control point)?

Not mathematically, without a deal of thought and scribblings. But you and
the OP could examine pixels.
 
J

Josef Garvi

Digital said:
The only way I can think of is to find the point the user clicked and
then see if it fits on the line between any two pairs of points, but
this is highly inefficient and potentially inaccurate.

Perhaps inefficient, but not inaccurate.
You should check if the distance from the point clicked to the line is less
than a certain threshold value (if the lines are thin, the user should be
allowed to click just outside the line and still get the line).

Anyway, I believe it's the best way to do it. Don't think efficiency should
be a problem unless you have really a great number of lines.

Just remember to keep some z-order on your lines, for example storing them
in a list. You will then loop through this list from top to bottom,
returning the first line that matches the mouse coordinate. That way, if
two lines intersect, the user will get the topmost line. If the lines have
different colors or styles (dashes, dots) then they should be drawn in the
reverse z-order, and everything becomes predictable.

--
Josef Garvi

"Reversing desertification through drought tolerant trees"
http://www.eden-foundation.org/

new income - better environment - more food - less poverty
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top