How do you find a point at a given perpendicular distance from a line?

前端 未结 4 1127
野性不改
野性不改 2020-11-29 19:27

I have a line that I draw in a window and I let the user drag it around. So, my line is defined by two points: (x1,y1) and (x2,y2). But now I would like to draw \"caps\" at

4条回答
  •  不知归路
    2020-11-29 19:57

    You need to compute a unit vector that's perpendicular to the line segment. Avoid computing the slope because that can lead to divide by zero errors.

    dx = x1-x2
    dy = y1-y2
    dist = sqrt(dx*dx + dy*dy)
    dx /= dist
    dy /= dist
    x3 = x1 + (N/2)*dy
    y3 = y1 - (N/2)*dx
    x4 = x1 - (N/2)*dy
    y4 = y1 + (N/2)*dx
    

提交回复
热议问题