D
D
di_gangsta2016-03-10 17:13:30
Java
di_gangsta, 2016-03-10 17:13:30

How to find the point of intersection of 2 lines, knowing only 2 points of each line?

We are given 4 points (2 pairs):
(x1,y1) and (x2,y2)
(x3,y3) and (x4,y4)
It is known that they are NOT parallel and intersect at some point. Question - how to find this point?
If possible, then some formula or just code (in Java) Thanks in advance!

Answer the question

In order to leave comments, you need to log in

4 answer(s)
O
OnYourLips, 2016-03-10
@di_gangsta

Knowing two points, we can choose the coefficients a and b: y = ax + b through a system of two equations for each line.
Knowing these coefficients, you make a system of two equations for the intersection point.

A
Andrey Gostishchev, 2016-03-10
@MERCH_music

public static boolean isLinesIntercepts(Pair<Float, Float> start1, Pair<Float, Float> end1, Pair<Float, Float> start2, Pair<Float, Float> end2) {
        double vector1 = (end2.first - start2.first) * (start1.second - start2.second) - (end2.second - start2.second) * (start1.first - start2.first);
        double vector2 = (end2.first - start2.first) * (end1.second - start2.second) - (end2.second - start2.second) * (end1.first - start2.first);
        double vector3 = (end1.first - start1.first) * (start2.second - start1.second) - (end1.second - start1.second) * (start2.first - start1.first);
        double vector4 = (end1.first - start1.first) * (end2.second - start1.second) - (end1.second - start1.second) * (end2.first - start1.first);
        return ((vector1 * vector2 <= 0) && (vector3 * vector4 <= 0));
    }

could have googled

A
Anton Fedoryan, 2016-03-11
@AnnTHony

Equation of a line from two points
Point of intersection of two lines

A
Alexander Ruchkin, 2016-03-10
@VoidEx

Pseudocode:

vec_t = left.end - left.start;
vec_k = right.end - right.start;
r = right.start - left.start;
d = vec_t ⋅ vec_k;
if (d ≃ 0.0)
  return; // параллельны
d_t = r ⋅ vec_k;
d_k = r ⋅ vec_t;
t = d_t / d; // параметр на одном отрезке ∈ [0, 1]
k = d_k / d; // на втором отрезке ∈ [0, 1]
if (t ∈ [0, 1] ∧ k ∈ [0, 1])
  pt = left.start + t ⋅ vec_t = right.start + k ⋅ vec_k;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question