Find a line intersecting a known line at right angle, given a point

后端 未结 6 660
隐瞒了意图╮
隐瞒了意图╮ 2020-12-29 15:53

This is basic graphics geometry and/or trig, and I feel dumb for asking it, but I can\'t remember how this goes. So:

  1. I have a line defined by two points (x1, y1
6条回答
  •  北荒
    北荒 (楼主)
    2020-12-29 16:07

    A useful rule of thumb in this kind of computational geometry is that you should work with vectors as long as you can, switching to Cartesian coordinates only as a last resort. So let's solve this using vector algebra. Suppose your line goes from p to p + r, and the other point is q.

    Now, any point on the line, including the point you are trying to find (call it s), can be expressed as s = p + λ r for a scalar parameter λ.

    Now the vector from q to s must be perpendicular to r. Therefore

    (q − (p + λ r)) · r = 0

    Where · is the dot product operator. Expand the product:

    (qp) · r = λ (r · r)

    And divide:

    λ = (qp) · r / r · r

    When you come to implement it, you need to check whether r · r = 0, to avoid division by zero.

提交回复
热议问题