Scratch detailed linear → algebra - find the coordinates of the intersection of two line segments

        Someone may ask: What is the use of finding the coordinates of the intersection point? And why use line substitution? Doesn't the equation of a straight line work? ? ?

        For this problem, I can only say that the calculation of the straight line equation is too many times, and the direction of the line must be considered at every turn. The slope of a 90° straight line does not exist, so it is better to use vectors (that is, linear algebra) to calculate. As for what is useful, maybe some algorithms need it, such as calculating whether a line intersects with a rectangle to make a judgment.

        This tutorial involves less content about line generation. In addition to ideas, you will also learn:

        How to check whether two line segments intersect? properties of similar triangles. direction from one point to another. Knowledge of trigonometric functions.

        In addition, the positive and negative characteristics of vector cross product, the calculation of vector cross product, the calculation of unit vector, the vector representation method from one point to another point, the geometric meaning and calculation of the quantity product (dot product) of vectors.


Examples of applications of linear algebra:

  1. Intersection Points (this tutorial)
  2. Divide a concave polygon into triangles (for rendering)
  3. The shortest point from a straight line (cross product)
  4. Judgment problems in various directions.

Part 1: How to judge whether the line segments intersect?

        This part needs to understand the meaning of vector cross product first, let me talk about it in a simple way

        First, look at the picture below.

        We have two vectors v and vector w, then the area of ​​the parallelogram formed by v (the word vector is omitted, the same is true later) and w is the result of v×w (× is the cross product), but the cross product has Positivity ,

        If v is to the left of w , then v×w<0 (that is, negative),

If v is to the right         of w , then v×w>0 (that is, positive),

        When v coincides with w or is opposite, it is easy to think that the result is 0 (not stretched into a parallelogram),

        To sum up, clockwise is negative and counterclockwise is positive.

        By the way, for a vector [x1, y1] and a vector [x2, y2], their cross product is the determinant of these two vectors (this sentence can not be understood), that is, x1*y2-x2*y1.


        Based on the above conclusions, we can begin to study how two lines intersect.

Intersect diagram

        As shown in the figure, the line segment AB intersects the line segment CD, and the endpoints of AB are connected to CD to form vectors a, b, c, d.

        We found that the cross product of a by b has the same sign as the cross product of d by c.

        Let's look at disjoint.

disjoint diagram

        The sign of the vector a cross multiplied by b is the same as that of d cross multiplied by c

        Next look at special.

special disjoint

        This time, a cross multiplied by b and d cross multiplied by c have different symbols. For this kind of mistake, just exchange d and b, as shown in the figure below:

        Corrected (note that the endpoints should be in the same place when cross multiplying vectors.

        This time, the cross of a and b and the cross of d and c have the same sign again!

        To sum up, if a(x1, y1), b(x2, y2), c(x3, y3), d(x4, y4), (both are vectors), then a lot of the above can be expressed mathematically as the following picture:

        In the picture above, × stands for cross multiplication, · stands for multiplication, and multiplication is greater than or equal to the same sign.

        Or change "or" to "and", and then change ≥ to <, then the line segments will intersect, otherwise they will not intersect.


Next, start implementing it with code!

        Here, I simply made a drawing system:

Then, create a homemade block

        Then, we can write such code to realize whether the two line segments intersect: first calculate the vector of a, b, c, d, and then according to the cross product formula (don’t forget, it is the determinant: x1*y2-x2 *y1 ), to judge .

        How to find the vector ?

        There is a formula for this problem. For point A(x1,y1) and point B(x2,y2) in the rectangular coordinate system of the plane , the vector AB is [(x2-x1),(y2-y1)]

        We go straight to the code.

Calculate vector

        The first is to calculate the vector, then save it in the cache, and initialize the return value. Next, we need to perform cross multiplication and judge whether it is <0, as shown in the figure below:

cross product and judge

        In this way, a banana intersection judgment is completed, but if it is judged whether the line segment is on the line, how to do it?

        We know that if the angle between one vector and another vector is 0° or 180°, then their cross product is 0. When on the line, the two vectors point to the other two endpoints respectively, forming 180 degrees;

        If this point is on the extension line of another line segment, then the angle between these two vectors is 0°;

        What if the point is on the endpoint?

        Think about it, then the modulus (length) of a vector is 0, it seems that the cross multiplication is still 0.

        Then the cross multiplication of these three possibilities will be 0, we can add a judgment equal to 0, and return the corresponding value. ,As shown below:


Part 2: Calculating intersection points

        Part 1 is finally finished, and the next step is the calculation of the intersection coordinates.

1. Clear your mind.

Line segments AB and CD intersect at point E

        As shown in the figure, point H is the point where C is projected to AB, and G is the same. (Projection is to make a vertical line to it), and the projection lengths are d1 and d2 respectively.

        What we are asking for is the coordinates of point E. For this, let me talk about it first: what is a similar triangle? (You can understand it in the sixth grade, please skip it for students in the third grade and above)

two triangles

        As shown in the figure, AB is parallel to CF and DE is parallel. Because of the parallel lines, we know that there are most of the same angles in the figure, which have been marked in the figure.

        Let's observe that the three corners (or two) of these two triangles are the same (really), but they are not congruent. Two triangles with three equal corners like this, we call them similar (don't know the exact Inaccurate, I'm only in the first year of junior high school ()

For two similar triangles, the ratios of         their corresponding side lengths are equal . for example:

        AB/BC=DE/DC, AB/DE=AC/CE。

        I don't know if you understand it, but that's all.

Look at the picture just now:

Find the coordinates of the intersection point E

        First, we look at the triangle DGE and the triangle CHE. Because the two opposite angles are equal (∠CEH and ∠GED), and because they are projections, one angle is 90 degrees, so the two triangles are similar .

        resemblance? That is the ratio is equal! Isn't that the ratio of the intersection point (x, y) to the two points is the same as the ratio of d1 and d2? (d1/d2=EH/GE)

        Let's see, what percentage of GH does EH account for? It is EH/GH. Now, since the ratio is the same, we can write EH/GH in the form of d1/(d1+d2) {that is, EH/GH=EH/(EH+GE)=d1/(d1+d2) }.

        Similarly, GE/GH can also be written in the form of d2/(d1+d2) {that is, GE/GH=GE/(EH+GE)=d2/(d1+d2)}.

        Hey, the weight (proportion) is known, the coordinates are easy to handle! (x, y) is the H coordinate * proportion + G coordinate * proportion, that is

Be careful not to reverse the weight


2. Calculate the projection.

        You must ask me: How do d1 and d2 count? ? ? Also, what are your projected coordinates? ? ?

        This is about the geometric meaning of the vector dot product (·) .

Vector dot product diagram

        The geometric meaning of the vector dot product is the distance between the point and the end point after one vector is projected to another vector , and then multiplied by the length (modulo length) of the other vector. In the figure, vector AB·vector AD obtains the length of AE, ie AC*AD.

        Sitting vector dot product formula 1 (coordinate system): a·b=(x1, y1)·(x2, y2)=x1*x2+y1*y2. Among them, a and b are two vectors, (x1, y1) and (x2, y2) respectively

        Vector dot product formula 2 (geometric): a b=|a|*|b|*cosθ, θ is the angle between a and b

        Vector cross product formula (geometry): a b=|a|*|b|*sinθ, θ is the angle between a and b

        Hey, we found a projection! What if the modulus length (length) of b in the above formula is 1 ?

        Due to the geometric meaning of the dot product, when the modulus of b is 1, a b is the projection of a to b!

        Then we can calculate the projection .

"projection"

        Given a point outside the line (x0, y0) and two points on the line (x1, y1), (x2, y2), calculate the projected point (x, y).

        How to project vector AB to vector AC ?

        First, find the unit vector of AC (the modulus length is 1, purple), then the vector AB·purple vector, you get the length of AD, and finally, find the direction of AC, and move the length of AD in the direction of AC, which is point D The coordinates of friends. (yeah)

        As for how to find the AC direction, you can use the inverse trigonometric function—arctan to find it. I won’t say much here, just upload the code directly.

        Next, how to find the unit vector?

        For a vector [x,y], its unit vector is [x/√(x^2+y^2), y/√(x^2+y^2)], which is actually the vector divided by the modulus length. As shown below:

unit vector formula

        Then there's just the points left.

dot product

        Next we can calculate the projection.

Half of the projection (because the cache may be used, the penultimate 1 and 2 items are used)

        Ah, we find the length and direction of AD, but how do we know its horizontal and vertical coordinates?

Find A'B and BD

        This is part of the picture just now. What we want is the x length and y length of AB (difference in horizontal and vertical coordinates), that is, A'B and BD

        Since ∠AA'B=90°, triangle AA'B is a right triangle

        For right triangles, we have a theorem: There is a fixed ratio of the three sides of the triangle for each angle.

        That is to say, in the triangle AA'B, the values ​​of A'B/AB and AA' (the length is BD)/AB are fixed.

        Here, we call AA' the side adjacent to angle a, A'B the side opposite to angle a, and AB the hypotenuse of angle a. (can not remember)

        We call the ratio of the opposite side to the hypotenuse the sine, which is recorded as sin, that is, sin a=right/oblique;

        The ratio of the adjacent side to the hypotenuse is called the cosine, which is recorded as cos, that is, cos a=adjacent/oblique;

        There are also functions such as tangent (tan) and inverse trigonometry. If you are interested, you can search by yourself. We only use sin and cos this time.

        We know the value of AB and angle a, then A'B and A'A are easy to find,

        sin a=A'B/AB, AB*sin a= A'B/AB*AB= A'B (Find out!)

        cos a=A'A/AB,AB*cos a=A'A/AB*AB=A'A

        Then the difference between the horizontal and vertical coordinates is calculated, and the code is as follows:

projection

        Don't forget to also calculate the projection distance (Pythagorean Theorem)

        The final code is shown in the figure

        change drawing code

image

        We found that the projection is a vertical line, indicating that the program is normal. (You should develop the habit of testing when doing programs!) Wonderful!


3. Calculate the intersection coordinates

just launched formula

        This is the coordinate formula of the intersection point that we just launched. Here, we can calculate d1, d2 and x6, y6, x5, y5, then x, y will come out!

        First, we need to project a line segment onto another line segment

Part of the coordinates of the line segment intersection

        Then, calculate the weight of each point (that is, d1 d2):

A part of the coordinates of the line segment intersection*2

        Finally, the weight is multiplied by the point coordinates and added, which is the intersection point coordinates.

Line segment intersection coordinates (full)


Part 3: Ending

        At this point, the coordinates of the intersection point of the two line segments have been completed. The following is the entire code:

all codes

        In the draw and move areas, I added transparency variations to prevent confusion.

        Let's test it out.

Show results


Resource download link

        If you don't want to write code, you can download this supporting resource: Scratch Tutorial Works: Line Segment Intersection Coordinates

        Thanks for the support

Guess you like

Origin blog.csdn.net/leyang0910/article/details/132006576