Line intersection

Hi Is there a method in Java that can calculate the intersection of two lines. I know that there is a Line2D.lineIntersects but this method only returns true or false if the two lines intersect. I need the X, Y point where the 2 lines intersect.
[259 byte] By [nestorjba] at [2007-10-2 16:12:23]
# 1

Google: http://www.google.com/search?q=line+intersection+java

gives the following as the first hit:

http://persistent.info/archives/2004/03/08/java_lineline_intersections

I didn't verify that it is right--but it shows a reference for the formula (which you could have also found by Google).

MLRona at 2007-7-13 16:58:43 > top of Java-index,Java Essentials,Java Programming...
# 2

> Is there a method in Java that can calculate the

> intersection of two lines. I know that there is a

> Line2D.lineIntersects but this method only returns

> true or false if the two lines intersect. I need the

> X, Y point where the 2 lines intersect.

That point should be very easy to calculate, unless the lines are parallell or close to.

MLRona at 2007-7-13 16:58:43 > top of Java-index,Java Essentials,Java Programming...
# 3

> > Is there a method in Java that can calculate the

> > intersection of two lines. I know that there is a

> > Line2D.lineIntersects but this method only returns

> > true or false if the two lines intersect. I need

> the

> > X, Y point where the 2 lines intersect.

>

> That point should be very easy to calculate, unless

> the lines are parallell or close to.

Why would lines being close to parallel make the point of intersection more difficult to calculate?

tsitha at 2007-7-13 16:58:43 > top of Java-index,Java Essentials,Java Programming...
# 4
pretty straight forward algebra http://id.mind.net/~zona/mmts/intersections/intersectionOfTwoLines1/intersectionOfTwoLines1.htmlshouldn't be much trouble to put that into a function..
BlairTheMagiciana at 2007-7-13 16:58:43 > top of Java-index,Java Essentials,Java Programming...
# 5

> > > Is there a method in Java that can calculate the

> > > intersection of two lines. I know that there is

> a

> > > Line2D.lineIntersects but this method only

> returns

> > > true or false if the two lines intersect. I need

> > the

> > > X, Y point where the 2 lines intersect.

> >

> > That point should be very easy to calculate,

> unless

> > the lines are parallell or close to.

>

> Why would lines being close to parallel make the

> point of intersection more difficult to calculate?

A valid point, it makes zero difference (ignoring rounding errors that may classify non-parallel lines as parallel).

remonvva at 2007-7-13 16:58:43 > top of Java-index,Java Essentials,Java Programming...
# 6

> > > That point should be very easy to calculate,

> > > unless

> > > the lines are parallell or close to.

> >

> > Why would lines being close to parallel make the

> > point of intersection more difficult to calculate?

> A valid point, it makes zero difference (ignoring rounding errors that

> may classify non-parallel lines as parallel).

Rounding errors (if, for example ints were involved) leading to

non-parallel being treated as parallel (or vice versa) would be a

problem.

With or without rounding error, the equations can yield "signed infinities"

that have to be dealt with.

Also there could be a question of precision: if the original line

segments were the result of measurement then how close they

are to parallel could make the difference between whether the

intersection is a reliably calculated value, or a more of a guess.

The OP shouldn't be put off! It's a reasonable question, and MLRon's

link provides a simple solution. But it also seems quite reasonable

to warn that - depending on the context of the original problem -

being parallel or close to parallel could involve problems.

pbrockway2a at 2007-7-13 16:58:43 > top of Java-index,Java Essentials,Java Programming...