Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / April 2004

Tip: Looking for answers? Try searching our database.

selecting a line in a drawn graph??

Thread view: 
Digital Puer - 15 Apr 2004 22:46 GMT
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.
Eric Sosman - 15 Apr 2004 23:06 GMT
> I'm planning to write a graphing application that will draw lines
> between plotted points (in a cartesian plane). Once I draw the lines,
[quoted text clipped - 6 lines]
>
> 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.

Signature

Eric.Sosman@sun.com

VisionSet - 16 Apr 2004 01:02 GMT
> I'm planning to write a graphing application that will draw lines
> between plotted points (in a cartesian plane). Once I draw the lines,
[quoted text clipped - 4 lines]
> 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

--
Mike W
ak - 16 Apr 2004 00:53 GMT
> > 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
[quoted text clipped - 13 lines]
> 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
--

____________

http://reader.imagero.com the best java image reader.

> > I'm planning to write a graphing application that will draw lines
> > between plotted points (in a cartesian plane). Once I draw the lines,
[quoted text clipped - 3 lines]
> --
> Mike W
VisionSet - 16 Apr 2004 01:06 GMT
> 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.

--
Mike W
Josef Garvi - 16 Apr 2004 07:25 GMT
> 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.

Signature

Josef Garvi

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

new income - better environment - more food - less poverty



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.