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).
> 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.
> > 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?
> > > 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).
> > > 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.