Finding the intersection of straight lines by vector cross multiplication

Time:2022-2-6

Solution method

Two tips:

  • useVector to represent points, you can useVector additionTo showTranslation of points
  • usePoint and vectorTo represent a straight line, which can be processedInfinite slopeThe situation.

Finding the intersection of straight lines by vector cross multiplication

As shown in the figure, two straight lines intersect at the point\(I\)。 Let’s use some\(p_1\)Sum vector\(\vec v_1\)To represent straight line 1, with points\(p_2\)Sum vector\(\vec v_2\)To represent line 2.

If you think of a point as a vector, you have\(\mathbf I=\mathbf p_1+t\mathbf v_1\)(using vector addition to express the translation of points), now our task is to find the scalar\(t\)。 So called useVector cross productTo find the intersection of straight lines is to use\(\overrightarrow{Ip_2}\times\vec v_2=\vec 0\)(two vectors are parallel) find this formula\(t\)

Before starting the derivation, review several operation rules of vector cross multiplication:

  • Left distribution law of addition:\(\mathbf{a} \times (\mathbf{b} + \mathbf{c}) = \mathbf{a} \times \mathbf{b} + \mathbf{a} \times \mathbf{c}\)
  • Right distribution law of addition:\((\mathbf{a} + \mathbf{b}) \times \mathbf{c} = \mathbf{a} \times \mathbf{c} + \mathbf{b} \times \mathbf{c}\)
  • Scalar multiplication:\((\lambda\mathbf{a})\times\mathbf{b} = \lambda(\mathbf{a}\times\mathbf{b}) = \mathbf{a}\times(\lambda\mathbf{b})\)

Let’s solve the scalar\(t\), the derivation process is as follows:

\[\begin{array}{rcl}
(\mathbf{p}_2 – \mathbf{I}) \times \mathbf{v}_2 &=& \mathbf{0}\\
(\mathbf{p}_2 – (\mathbf{p}_1 + t\mathbf{v}_1)) \times \mathbf{v}_2 &=& \mathbf{0}\\
(\mathbf{p}_2 – \mathbf{p}_1) \times \mathbf{v}_2 – t\mathbf{v}_1 \times \mathbf{v}_2 &=& \mathbf{0}\\
(\mathbf{p}_2 – \mathbf{p}_1) \times \mathbf{v}_2 &=& t(\mathbf{v}_1 \times \mathbf{v}_2)\\
t &=& \frac{|(\mathbf{p}_2 – \mathbf{p}_1) \times \mathbf{v}_2|}{|\mathbf{v}_1 \times \mathbf{v}_2|}
\end{array}
\]

Well, now we have the scalar\(t\), next use\(\mathbf I=\mathbf p_1+t\mathbf v_1\)This formula calculates the intersection\(I\)The coordinates of.

Incidentally, this idea of “unified representation of points and vectors” has a very important application in computer graphics, that is, homogeneous coordinates.

code

First, we need to write a vector class.

Note that in the codecrossThe function evaluates the module of the vector product, so it returns a scalar.

//Two dimensional vector
struct Vec
{
    double x, y;
    Vec() = default;
    Vec(double x, double y) : x(x), y(y) {}
    double len2() const { return x * x + y * y; }
    double len() const { return sqrt(len2()); }
    Vec operator+(const Vec &b) const { return Vec(x + b.x, y + b.y); }
    Vec operator-(const Vec &b) const { return Vec(x - b.x, y - b.y); }
    Vec operator*(double t) const { return Vec(x * t, y * t); }
    Vec operator/(double t) const { return Vec(x / t, y / t); }
    Vec operator-() const { return Vec(-x, -y); }
};

//Module of vector product
double cross(const Vec &a, const Vec &b) { return a.x * b.y - b.x * a.y; }

Code for solving the intersection of two lines. The code is quite concise and can deal with all situations, including infinite slope of the straight line.

//Solve the intersection of two lines
//P1 and V1 are used to represent straight line 1, P2 and V2 are used to represent straight line 2
Vec intersection(const Vec &p1, const Vec &v1, const Vec &p2, const Vec &v2)
{
    double t = cross((p2 - p1), v2) / cross(v1, v2);
    return p1 + v1 * t;
}