Connect two Line Segments

前端 未结 7 2167
你的背包
你的背包 2021-01-02 12:07

Given two 2D line segments, A and B, how do I calculate the length of the shortest 2D line segment, C, which connects A and B?

7条回答
  •  执笔经年
    2021-01-02 12:29

    Consider your two line segments A and B to be represented by two points each:

    line A represented by A1(x,y), A2(x,y)

    Line B represented by B1(x,y) B2(x,y)

    First check if the two lines intersect using this algorithm.

    If they do intersect, then the distance between the two lines is zero, and the line segment joining them is the intersection point.

    If they do not intersect, Use this method: http://paulbourke.net/geometry/pointlineplane/ to calculate the shortest distance between:

    1. point A1 and line B
    2. Point A2 and line B
    3. Point B1 and line A
    4. Point B2 and line A

    The shortest of those four line segments is your answer.

提交回复
热议问题