Intersection between two lines in coordinates

后端 未结 6 1773
臣服心动
臣服心动 2020-12-31 19:42

I can detect the intersection point of two lines, but if my line don\'t has the length of my screen, it detects the point, where it shouldn\'t be.

Here a preview:

6条回答
  •  没有蜡笔的小新
    2020-12-31 20:16

    Here is another solution in Swift 4.2. This is functionally identical to MartinR's solution but uses simd vectors and matrices to clean it up.

    /// Protocol adoped by any type that models a line segment.
    protocol LineSegment
    {
        /// Point defining an end of a line segment.
        var p1: simd_double2 { get }
        /// Point defining an end of a line segment.
        var p2: simd_double2 { get }
    }
    
    extension LineSegment
    {
        /// Calcualte the intersection between this line segment and another line
        /// segment.
        ///
        /// Algorithm from here:
        /// http://www.cs.swan.ac.uk/~cssimon/line_intersection.html
        ///
        /// - Parameter other: The other line segment.
        /// - Returns: The intersection point, or `nil` if the two line segments are
        ///            parallel or the intersection point would be off the end of
        ///            one of the line segments.
        func intersection(lineSegment other: LineSegment) -> simd_double2?
        {
            let p3 = other.p1 // Name the points so they are consistent with the explanation below
            let p4 = other.p2
            let matrix = simd_double2x2(p4 - p3, p1 - p2)
            guard matrix.determinant != 0 else { return nil } // Determinent == 0 => parallel lines
            let multipliers = matrix.inverse * (p1 - p3)
            // If either of the multipliers is outside the range 0 ... 1, then the
            // intersection would be off the end of one of the line segments.
            guard (0.0 ... 1.0).contains(multipliers.x) && (0.0 ... 1.0).contains(multipliers.y)
                else { return nil }
            return p1 + multipliers.y * (p2 - p1)
        }
    }
    

    The algorithm works because, if you have line segment a defined by two points p1 and p2 and line segment b defined by p3 and p4 the points on a and b are respectively defined by

    • p1 + ta(p2 - p1)
    • p3 + tb(p4 - p3)

    so the point of intersection would be where

    p1 + ta(p2 - p1) = p3 + tb(p4 - p3)

    This can be rearranged as

    p1 - p3 = tb(p4 - p3) + ta(p1 - p2)

    and with a bit of jiggery pokery you can get to the following equivalent

    p1 - p3 = A.t

    where t is the vector (tb, ta) and A is the matrix whose columns are p4 - p3 and p1 - p2

    The equation can be rearranged as

    A-1(p1 - p3) = t

    Everything on the left hand side is already known or can be calculated to get us t. Either of the components of t can be plugged into the respective original equation to get the intersection point (NB floating point rounding errors will mean that the two answers probably aren't exactly the same but are very close).

    Note that, if the lines are parallel, the determinant of A will be zero. Also, if either component is outside the range 0 ... 1, then one or both line segments needs to be extended to get to the point of intersection.

提交回复
热议问题