0

What is the most efficient algorithm to find intersection point between two lines?

You are given four points A, B , C , D. Find the intersection point between AB and CD. Optimize the algorithm as much as you can.

There are two approach for this, one is using dot product and another is using slope intercept form for line. which one is better.

This might sound a repeated question but what I want to ask is which approach is better and most efficient with better complexity.

0

3 Answers 3

6

This doesn't require any algorithm, just the solution of two intersecting lines. That's a basic mathematics problem, not a computing one (it's just algebraic manipulation).

That said, here's a discussion you should find helpful.

Sign up to request clarification or add additional context in comments.

4 Comments

All mathematical operations, no matter how simple, are algorithms.
@BipedalShark: The point is, this is a computing forum, not a mathematics one. It makes no sense to talk about complexity of this 'algorithm', for example. See e.g. this discussion on meta: meta.stackexchange.com/questions/26339/…
The first subheading in the linked discussion shows the most efficient way that I'm aware of to get from four points to the intersection of those two lines. Most other mathematical discussions of the problem assume you're starting with lines in slope-intercept form (y = mx + b), but in real-world applications, I find you're much more likely to be starting from points instead.
It does make sense to talk of correctness. A naive translation of the algrebraic solution will not be accurate. Since the question involves floating point arithmetic a computing forum is an appropriate place to ask the question. It is answered in Ch. 33, Computational Geometry, of the 2nd edition of the textbook "Introduction to Algorithms" available from mitp-content-server.mit.edu/books/content/sectbyfn/books_pres_0/…. As they point out it is important to avoid the division in the algebraic solution.
2

I prefer Mr. Bourke's website for these types of questions. Here's his article on line intersectoin:

Intersection point of two lines

Given how trivial this is, it's pretty tough to optimize.

I guess the best you can do is make sure that everything is in the CPU cache, that way you can run those math ops at full speed. You may be tempted to precompute some of the differences (P2 - P1), but it's hard to say in this world whether the memory lookup for that will be any faster than just performing the subtraction itself. CPUs can do subtraction and multiplication in 1 op whereas memory lookups, if they miss the cache, can take a couple orders of magnitude longer.

Comments

0

It's not that trivial.

As far I remember the Pascal example ( http://actionsnippet.com/?p=956 ) does not work with collinear points.

I was not able to find a correctly implemented algorithm, so I had to write my own.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.