你如何在距离一条线的给定垂直距离上找到一个点? [英] How do you find a point at a given perpendicular distance from a line?

查看:167
本文介绍了你如何在距离一条线的给定垂直距离上找到一个点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在窗口中画了一条线,然后让用户拖动它。所以,我的线由两点定义:(x1,y1)和(x2,y2)。但是现在我想在我的线条的末端绘制帽子,也就是说,在我的每个端点上都有短垂线。因此,为了在终点(x1,y1)绘制我的帽子线,我需要找到两个点,它们组成一个垂直线,并且其每个点都离开点(x1,y1)N / 2个像素点。所以如何计算一个点(x3,y3) ),因为它需要与已知线的终点(x1,y1)相距一个垂直距离N / 2,即由(x1,y1)和(x2,y2)定义的直线?

解决方案

您需要计算垂直于线段的单位矢量。避免计算斜率,因为这可能导致除以零错误。

  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


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 the end of my line, that is, short perpendicular lines at each of my end points. The caps should be N pixels in length.

Thus, to draw my "cap" line at end point (x1,y1), I need to find two points that form a perpendicular line and where each of its points are N/2 pixels away from the point (x1,y1).

So how do you calculate a point (x3,y3) given it needs to be at a perpendicular distance N/2 away from the end point (x1,y1) of a known line, i.e. the line defined by (x1,y1) and (x2,y2)?

解决方案

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

这篇关于你如何在距离一条线的给定垂直距离上找到一个点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆