A simple algorithm for determining the intersection of two segments. Do lines intersect: intersection of segments on a plane

Perpendicular line

This task is probably one of the most popular and in demand in school textbooks. The tasks based on this theme are manifold. This is the definition of the point of intersection of two lines, this is the definition of the equation of a straight line passing through a point on the original line at some angle.

We will cover this topic using in our calculations the data obtained using

It was there that the transformation of the general equation of a straight line, into an equation with a slope and vice versa, and the determination of the remaining parameters of a straight line according to given conditions were considered.

What do we lack in order to solve the problems that this page is devoted to?

1. Formulas for calculating one of the angles between two intersecting lines.

If we have two straight lines which are given by the equations:

then one of the angles is calculated like this:

2. Equation of a straight line with a slope passing through a given point

From formula 1, we can see two border states

a) when then and therefore these two given lines are parallel (or coincide)

b) when , then , and therefore these lines are perpendicular, that is, they intersect at a right angle.

What can be the initial data for solving such problems, except for a given straight line?

A point on a line and the angle at which the second line intersects it

The second equation of the line

What tasks can a bot solve?

1. Two straight lines are given (explicitly or implicitly, for example, by two points). Calculate the point of intersection and the angles at which they intersect.

2. Given one straight line, a point on a straight line, and one angle. Determine the equation of a straight line that intersects a given one at a specified angle

Examples

Two straight lines are given by equations. Find the point of intersection of these lines and the angles at which they intersect

line_p A=11;B=-5;C=6,k=3/7;b=-5

We get the following result

Equation of the first line

y = 2.2 x + (1.2)

Equation of the second line

y = 0.4285714285714 x + (-5)

Angle of intersection of two lines (in degrees)

-42.357454705937

Point of intersection of two lines

x=-3.5

y=-6.5


Do not forget that the parameters of the two lines are separated by a comma, and the parameters of each line by a semicolon.

The line passes through two points (1:-4) and (5:2) . Find the equation of a straight line that passes through the point (-2:-8) and intersects the original line at an angle of 30 degrees.

One straight line is known to us, since two points through which it passes are known.

It remains to determine the equation of the second straight line. One point is known to us, and instead of the second, the angle at which the first line intersects the second is indicated.

Everything seems to be known, but the main thing here is not to be mistaken. We are talking about the angle (30 degrees) not between the x-axis and the line, but between the first and second lines.

For this we post like this. Let's determine the parameters of the first line, and find out at what angle it intersects the x-axis.

line xa=1;xb=5;ya=-4;yb=2

General equation Ax+By+C = 0

Coefficient A = -6

Factor B = 4

Coefficient C = 22

Coefficient a= 3.6666666666667

Coefficient b = -5.5

Coefficient k = 1.5

Angle of inclination to the axis (in degrees) f = 56.309932474019

Coefficient p = 3.0508510792386

Coefficient q = 2.5535900500422

Distance between points=7.211102550928

We see that the first line crosses the axis at an angle 56.309932474019 degrees.

The source data does not say exactly how the second line intersects the first. After all, it is possible to draw two lines that satisfy the conditions, the first rotated 30 degrees clockwise, and the second 30 degrees counterclockwise.

Let's count them

If the second line is rotated 30 degrees COUNTER-CLOCKWISE, then the second line will have a degree of intersection with the x-axis 30+56.309932474019 = 86 .309932474019 degrees

line_p xa=-2;ya=-8;f=86.309932474019

Straight line parameters according to the given parameters

General equation Ax+By+C = 0

Coefficient A = 23.011106998916

Factor B = -1.4840558255286

Coefficient C = 34.149767393603

Equation of a straight line in segments x/a+y/b = 1

Coefficient a= -1.4840558255286

Coefficient b = 23.011106998916

Equation of a straight line with angular coefficient y = kx + b

Coefficient k = 15.505553499458

Angle of inclination to the axis (in degrees) f = 86.309932474019

Normal equation of the line x*cos(q)+y*sin(q)-p = 0

Coefficient p = -1.4809790664999

Coefficient q = 3.0771888256405

Distance between points=23.058912962428

Distance from point to line li =

that is, our second line equation is y= 15.505553499458x+ 23.011106998916

Topic 3. Theory

Analytical geometry in space.

Equations of a plane and a straight line.

 General equation plane is a first-order algebraic equation with respect to the coordinates (x; y; z)

- normal , a vector perpendicular to the plane.


The conditions of parallelism and perpendicularity of the planes are determined by the conditions of collinarity and perpendicularity of the normals.

Some standard types of plane equations:

Equation of a plane perpendicular to a vector
passing through this point M 0 (X 0 , y 0 , z 0 )

A(x-x 0 )+B(y-y 0 )+C(z-z 0 )=0

Plane passing through three given points M 1 (X 1 , y 1 , z 1 ) , M 2 (x 2 , y 2 , z 2 ) , M 3 (x 3 , y 3 , z 3 )

Parallel to two given vectors
and
, (non-collinear ) passing through the point M 0 (X 0 , y 0 , z 0 )

Passing through two given points M 1 and M 2 , parallel to the vector , (non-collinear
)

Passing through a given point M 0 (x 0 , y 0 , z 0 ) , perpendicular to two given planes:

    A 1 x+B 1 y+C 1 z+D 1 =0 ;

    A 2 x+B 2 y+C 2 z+D 2 =0 .

The actual equations of the plane will be obtained by expanding the corresponding determinant in the first row.

 Formula to calculate distances from given point M 1 (x 1 , y 1 , z 1 ) before plane given by the equation Ah+By+ cz+ D=0 :

.

Obviously if d=0 , then the point M 1 belongs to the plane.

Straight line in space is defined as the line of intersection of two non-parallel planes (any passing through a straight line).

Types of equations of a straight line in space:

General equations of a straight line (intersection of two planes)

,

M 0 (x 0 , y 0 , z 0 ) is any point on a line.
-guide vector straight

Canonical equations

straight line or equation of a straight line passing through a given point with a given direction vector

Parametric equation

Equations of a straight line passing through two given points M 1 and M 2

The conditions of parallelism and perpendicularity of straight lines in space are defined as the conditions, respectively, of collinearity and perpendicularity of their direction vectors. Let the lines (1) and (2) be given in the canonical or parametric form, then

.

The condition for the intersection of two lines in space is the condition of complonarity of three vectors:

Transition from the general equations of the straight line to the equations in the canonical or parametric form is carried out as follows (the reverse transition is also possible).

Equations of a straight line in general form are given:
.

Let's find the coordinates of the direction vector:
as a vector product of the normals of the planes defining the line.

Let's find any point on the line. It also belongs to both planes that define the line, so its coordinates (x 0, y 0, z 0) can be found from the system of equations:

,

in which one of the coordinates must be set arbitrarily (because we find any point), but in such a way that the system has a unique solution. Vector coordinates and the found point is substituted into the canonical or parametric equations.

The conditions of parallelism and perpendicularity of the line and plane are formulated as the conditions of perpendicularity and parallelism of the normal and the direction vector.

,

Al+Bm+Cn=0.

,

.

Lesson from the series "Geometric Algorithms"

Hello dear reader!

We continue to get acquainted with geometric algorithms. In the last lesson, we found the equation of a straight line in the coordinates of two points. We have an equation of the form:

Today we will write a function that, using the equations of two straight lines, will find the coordinates of their intersection point (if any). To check the equality of real numbers, we will use the special function RealEq().

Points on the plane are described by a pair of real numbers. When using the real type, it is better to arrange the comparison operations with special functions.

The reason is known: there is no order relation on the Real type in the Pascal programming system, so it is better not to use records of the form a = b, where a and b are real numbers.
Today we will introduce the RealEq() function to implement the “=” (strictly equal) operation:

Function RealEq(Const a, b:Real):Boolean; (strictly equal) begin RealEq:=Abs(a-b)<=_Eps End; {RealEq}

Task. Equations of two straight lines are given: and . Find their point of intersection.

Decision. The obvious solution is to solve the system of equations of lines: Let's rewrite this system a little differently:
(1)

We introduce the notation: , , . Here D is the determinant of the system, and are the determinants obtained by replacing the column of coefficients for the corresponding unknown with a column of free terms. If , then system (1) is definite, that is, it has a unique solution. This solution can be found by the following formulas: , , which are called Cramer's formulas. Let me remind you how the second order determinant is calculated. The determinant distinguishes between two diagonals: the main and secondary. The main diagonal consists of elements taken in the direction from the upper left corner of the determinant to the lower right corner. Side diagonal - from the upper right to the lower left. The second order determinant is equal to the product of the elements of the main diagonal minus the product of the elements of the secondary diagonal.

The code uses the RealEq() function to check for equality. Calculations over real numbers are made with accuracy up to _Eps=1e-7.

Program geom2; Const _Eps: Real=1e-7;(calculation accuracy) var a1,b1,c1,a2,b2,c2,x,y,d,dx,dy:Real; Function RealEq(Const a, b:Real):Boolean; (strictly equal) begin RealEq:=Abs(a-b)<=_Eps End; {RealEq} Function LineToPoint(a1,b1,c1,a2,b2,c2: real; var x,y:real):Boolean; {Определение координат точки пересечения двух линий. Значение функции равно true, если точка пересечения есть, и false, если прямые параллельны. } var d:real; begin d:=a1*b2-b1*a2; if Not(RealEq(d,0)) then begin LineToPoint:=True; dx:=-c1*b2+b1*c2; dy:=-a1*c2+c1*a2; x:=dx/d; y:=dy/d; end else LineToPoint:=False End;{LineToPoint} begin {main} writeln("Введите коэффициенты уравнений: a1,b1,c1,a2,b2,c2 "); readln(a1,b1,c1,a2,b2,c2); if LineToPoint(a1,b1,c1,a2,b2,c2,x,y) then writeln(x:5:1,y:5:1) else writeln("Прямые параллельны."); end.

We have compiled a program with which you can, knowing the equations of the lines, find the coordinates of their intersection point.


When solving some geometric problems using the coordinate method, it is necessary to find the coordinates of the point of intersection of lines. Most often, one has to look for the coordinates of the point of intersection of two lines on the plane, but sometimes it becomes necessary to determine the coordinates of the point of intersection of two lines in space. In this article, we will deal with finding the coordinates of the point at which two lines intersect.

Page navigation.

The point of intersection of two lines is a definition.

Let's first define the point of intersection of two lines.

Thus, in order to find the coordinates of the point of intersection of two lines defined on the plane by general equations, it is necessary to solve a system composed of equations of given lines.

Let's consider an example solution.

Example.

Find the point of intersection of two lines defined in a rectangular coordinate system in the plane by the equations x-9y+14=0 and 5x-2y-16=0 .

Decision.

We are given two general equations of lines, we will compose a system from them: . The solutions of the resulting system of equations are easily found if its first equation is solved with respect to the variable x and this expression is substituted into the second equation:

The found solution of the system of equations gives us the desired coordinates of the point of intersection of two lines.

Answer:

M 0 (4, 2) x-9y+14=0 and 5x-2y-16=0 .

So, finding the coordinates of the point of intersection of two lines, defined by general equations on the plane, is reduced to solving a system of two linear equations with two unknown variables. But what if the straight lines on the plane are given not by general equations, but by equations of a different type (see the types of the equation of a straight line on the plane)? In these cases, you can first bring the equations of lines to a general form, and only after that find the coordinates of the intersection point.

Example.

and .

Decision.

Before finding the coordinates of the point of intersection of the given lines, we bring their equations to a general form. Transition from parametric equations to a straight line to the general equation of this straight line is as follows:

Now we will carry out the necessary actions with the canonical equation of the line:

Thus, the desired coordinates of the point of intersection of the lines are the solution of the system of equations of the form . We use to solve it:

Answer:

M 0 (-5, 1)

There is another way to find the coordinates of the point of intersection of two lines in the plane. It is convenient to use it when one of the lines is given by parametric equations of the form , and the other - the equation of a straight line of a different form. In this case, in another equation, instead of the variables x and y, you can substitute the expressions and , from which it will be possible to obtain the value that corresponds to the point of intersection of the given lines. In this case, the point of intersection of the lines has coordinates .

Let's find the coordinates of the point of intersection of the lines from the previous example in this way.

Example.

Determine the coordinates of the point of intersection of the lines and .

Decision.

Substitute in the equation of the direct expression:

Solving the resulting equation, we get . This value corresponds to the common point of the lines and . We calculate the coordinates of the intersection point by substituting the straight line into the parametric equations:
.

Answer:

M 0 (-5, 1) .

To complete the picture, one more point should be discussed.

Before finding the coordinates of the point of intersection of two lines in the plane, it is useful to make sure that the given lines really intersect. If it turns out that the original lines coincide or are parallel, then there can be no question of finding the coordinates of the intersection point of such lines.

You can, of course, do without such a check, and immediately compose a system of equations of the form and solve it. If the system of equations has a unique solution, then it gives the coordinates of the point at which the original lines intersect. If the system of equations has no solutions, then we can conclude that the original lines are parallel (since there is no such pair of real numbers x and y that would simultaneously satisfy both equations of given lines). From the presence of an infinite set of solutions to the system of equations, it follows that the original lines have infinitely many points in common, that is, they coincide.

Let's look at examples that fit these situations.

Example.

Find out if the lines and intersect, and if they intersect, then find the coordinates of the intersection point.

Decision.

The given equations of lines correspond to the equations and . Let's solve the system composed of these equations .

Obviously, the equations of the system are linearly expressed through each other (the second equation of the system is obtained from the first by multiplying both its parts by 4), therefore, the system of equations has an infinite number of solutions. Thus, the equations and define the same line, and we cannot talk about finding the coordinates of the point of intersection of these lines.

Answer:

The equations and determine the same straight line in the rectangular coordinate system Oxy, so we cannot talk about finding the coordinates of the intersection point.

Example.

Find the coordinates of the point of intersection of the lines and , if possible.

Decision.

The condition of the problem admits that the lines may not intersect. Let's compose a system of these equations. Applicable for its solution, since it allows you to establish the compatibility or inconsistency of the system of equations, and if it is compatible, find a solution:

The last equation of the system after the direct course of the Gauss method turned into an incorrect equality, therefore, the system of equations has no solutions. From this we can conclude that the original lines are parallel, and we cannot talk about finding the coordinates of the point of intersection of these lines.

The second solution.

Let's find out if the given lines intersect.

- normal line vector , and the vector is a normal vector of the line . Let's check the execution and : equality is true, since , therefore, the normal vectors of the given lines are collinear. Then, these lines are parallel or coincide. Thus, we cannot find the coordinates of the point of intersection of the original lines.

Answer:

It is impossible to find the coordinates of the point of intersection of the given lines, since these lines are parallel.

Example.

Find the coordinates of the point of intersection of the lines 2x-1=0 and if they intersect.

Decision.

We compose a system of equations that are general equations of given lines: . The determinant of the main matrix of this system of equations is different from zero , so the system of equations has a unique solution, which indicates the intersection of the given lines.

To find the coordinates of the point of intersection of the lines, we need to solve the system:

The resulting solution gives us the coordinates of the point of intersection of the lines, that is, 2x-1=0 and .

Answer:

Finding the coordinates of the point of intersection of two lines in space.

The coordinates of the point of intersection of two lines in three-dimensional space are found similarly.

Let's consider examples.

Example.

Find the coordinates of the point of intersection of two lines given in space by the equations and .

Decision.

We compose a system of equations from the equations of given lines: . The solution of this system will give us the desired coordinates of the point of intersection of lines in space. Let us find the solution of the written system of equations.

The main matrix of the system has the form , and the extended .

Let's define A and the rank of the matrix T . We use

Point of intersection of lines

Let us be given two straight lines given by their coefficients and . It is required to find their intersection point, or find out that the lines are parallel.

Decision

If two lines are not parallel, then they intersect. To find the intersection point, it is enough to compose a system of two equations of lines and solve it:

Using Cramer's formula, we immediately find a solution to the system, which will be the desired intersection point:



If the denominator is zero, i.e.

then the system of solutions has no (direct are parallel and do not coincide) or has infinitely many (direct match). If it is necessary to distinguish between these two cases, it is necessary to check that the coefficients of the lines are proportional with the same coefficient of proportionality as the coefficients and , for which it is enough to calculate two determinants, if they are both equal to zero, then the lines coincide:

Implementation

struct pt (double x, y;); struct line (double a, b, c;); constdouble EPS=1e-9; double det (double a, double b, double c, double d)(return a * d - b * c;) bool intersect (line m, line n, pt & res)(double zn = det (m.a, m.b, n.a , n.b);if(abs(zn)< EPS)returnfalse; res.x=- det (m.c, m.b, n.c, n.b)/ zn; res.y=- det (m.a, m.c, n.a, n.c)/ zn;returntrue;} bool parallel (line m, line n){returnabs(det (m.a, m.b, n.a, n.b))< EPS;} bool equivalent (line m, line n){returnabs(det (m.a, m.b, n.a, n.b))< EPS &&abs(det (m.a, m.c, n.a, n.c))< EPS &&abs(det (m.b, m.c, n.b, n.c))< EPS;}

Lesson from the series " Geometric Algorithms»

Hello dear reader.

Tip 1: How to find the coordinates of the point of intersection of two lines

Let's write three more new functions.

The LinesCross() function will determine if intersect whether two segment. In it, the relative position of the segments is determined using vector products. To calculate vector products, let's write a function - VektorMulti().

The RealLess() function will be used to implement the comparison operation “<” (строго меньше) для вещественных чисел.

Task1. Two segments are given by their coordinates. Write a program that determines Do these segments intersect? without finding the intersection point.

Decision
. The second is given by dots.



Consider a segment and points and .

The point lies to the left of the line, for which the vector product > 0, since the vectors are positively oriented.

The point is located to the right of the line, for it the vector product< 0, так как векторы отрицательно ориентированы.

In order for the points and , to lie on opposite sides of the line , it is sufficient that the condition< 0 (векторные произведения имели противоположные знаки).

Similar reasoning can be carried out for the segment and points and .

So if , then the segments intersect.

To check this condition, the LinesCross() function is used, and to calculate vector products, the VektorMulti() function is used.

ax, ay are the coordinates of the first vector,

bx, by are the coordinates of the second vector.

Program geometry4; (Do 2 segments intersect?) Const _Eps: Real=1e-4; (calculation precision) var x1,y1,x2,y2,x3,y3,x4,y4: real; var v1,v2,v3,v4: real;function RealLess(Const a, b: Real): Boolean; (Strictly less than) begin RealLess:= b-a> _Eps end; (RealLess)function VektorMulti(ax,ay,bx,by:real): real; (ax,ay - a coordinates bx,by - b coordinates) begin vektormulti:= ax*by-bx*ay; end;Function LinesCross(x1,y1,x2,y2,x3,y3,x4,y4:real): boolean; (Do the segments intersect?) begin v1:=vektormulti(x4-x3,y4-y3,x1-x3,y1-y3); v2:=vectormulti(x4-x3,y4-y3,x2-x3,y2-y3); v3:=vectormulti(x2-x1,y2-y1,x3-x1,y3-y1); v4:=vectormulti(x2-x1,y2-y1,x4-x1,y4-y1); if RealLess(v1*v2.0) and RealLess(v3*v4.0) (v1v2<0 и v3v4<0, отрезки пересекаются} then LinesCross:= true else LinesCross:= false end; {LinesCross}begin {main} writeln(‘Введите координаты отрезков: x1,y1,x2,y2,x3,y3,x4,y4’); readln(x1,y1,x2,y2,x3,y3,x4,y4); if LinesCross(x1,y1,x2,y2,x3,y3,x4,y4) then writeln (‘Да’) else writeln (‘Нет’) end.

Program execution results:

Enter the coordinates of the segments: -1 1 2 2.52 2 1 -1 3
Yes.

We have written a program that determines whether the segments given by their coordinates intersect.

In the next lesson, we will write an algorithm that can be used to determine whether a point lies inside a triangle.

Dear reader.

You have already read several lessons from the Geometric Algorithms series. Is everything available written? I will be very grateful if you leave a review about these lessons. Perhaps something else needs to be improved.

Sincerely, Vera Gospodarets.

Let two segments be given. The first one is given by dots P 1 (x 1 ;y 1) and P 2 (x 2 ;y 2). The second is given by dots P 3 (x 3 ;y 3) and P 4 (x 4 ;y 4).

The relative position of the segments can be checked using vector products:

Consider the segment P 3 P 4 and points P1 and P2.

Dot P1 lies to the left of the line P 3 P 4, for it the vector product v1 > 0, since the vectors are positively oriented.
Dot P2 located to the right of the line, for it the vector product v2< 0 , since the vectors are negatively oriented.

To point P1 and P2 lie on opposite sides of a straight line P 3 P 4, it is sufficient that the condition v 1 v 2< 0 (vector products had opposite signs).

Similar reasoning can be carried out for the segment P 1 P 2 and points P3 and P4.

So if v 1 v 2< 0 and v 3 v 4< 0 , then the segments intersect.

The cross product of two vectors is calculated by the formula:

where:
ax, ay are the coordinates of the first vector,
bx, by are the coordinates of the second vector.

The equation of a straight line passing through two different points given by their coordinates.

Let two non-coinciding points be given on a straight line: P1 with coordinates ( x1;y1) and P2 with coordinates (x 2 ; y 2).

Line intersection

Accordingly, the vector with the origin at the point P1 and end at a point P2 has coordinates (x 2 -x 1, y 2 -y 1). If a P(x, y) is an arbitrary point on the line, then the coordinates of the vector P 1 P equal (x - x 1, y - y 1).

With the help of the cross product, the condition of collinarity of vectors P 1 P and P 1 P 2 can be written like this:
|P 1 P,P 1 P 2 |=0, i.e. (x-x 1)(y 2 -y 1)-(y-y 1)(x 2 -x 1)=0
or
(y 2 -y 1)x + (x 1 -x 2)y + x 1 (y 1 -y 2) + y 1 (x 2 -x 1) = 0

The last equation is rewritten as follows:
ax + by + c = 0, (1)
where
a \u003d (y 2 -y 1),
b \u003d (x 1 -x 2),
c \u003d x 1 (y 1 -y 2) + y 1 (x 2 -x 1)

So, the straight line can be given by an equation of the form (1).

How to find the point of intersection of lines?
The obvious solution is to solve the system of equations of lines:

ax 1 +by 1 =-c 1
ax 2 +by 2 =-c 2
(2)

Enter designations:

Here D is the determinant of the system, and D x ,D y are the determinants obtained by replacing the column of coefficients for the corresponding unknown with a column of free terms. If a D ≠ 0, then system (2) is definite, that is, it has a unique solution. This solution can be found using the following formulas: x 1 \u003d D x / D, y 1 \u003d D y / D, which are called Cramer's formulas. A little reminder of how the second order determinant is calculated. The determinant distinguishes between two diagonals: the main and secondary. The main diagonal consists of elements taken in the direction from the upper left corner of the determinant to the lower right corner. Side diagonal - from the upper right to the lower left. The second order determinant is equal to the product of the elements of the main diagonal minus the product of the elements of the secondary diagonal.