垂直于给定点的直线
如何从给定点在线段上绘制垂线?我的线段定义为(x1,y1),(x2,y2),如果我从点(x3,y3)绘制垂直线并且它与点(x4,y4)上的线相交。我想找出这个(x4,y4)。
How can I draw a perpendicular on a line segment from a given point? My line segment is defined as (x1, y1), (x2, y2), If I draw a perpendicular from a point (x3,y3) and it meets to line on point (x4,y4). I want to find out this (x4,y4).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
我同意 peter.murray.rust 的观点,向量使解决方案更清晰:
I agree with peter.murray.rust, vectors make the solution clearer:
您知道该点和斜率,因此新直线的方程为:
由于直线是垂直的,因此斜率是负倒数。现在您有两个方程并且可以求解它们的交集。
You know both the point and the slope, so the equation for the new line is:
Since the line is perpendicular, the slope is the negative reciprocal. You now have two equations and can solve for their intersection.
您经常会发现使用向量使解决方案更清晰...
这是我自己的库中的一个例程:
}
您将必须实现 Real2(一个点)和 Vector2 和 dotProduct() 但这些应该很简单:
然后代码看起来类似于:
库 (org.xmlcml.euclid) 位于:
http://sourceforge.net/projects/cml/
并且有单元测试将执行此方法并向您展示如何使用它。
You will often find that using vectors makes the solution clearer...
Here is a routine from my own library:
}
You will have to implement Real2 (a point) and Vector2 and dotProduct() but these should be simple:
The code then looks something like:
The library (org.xmlcml.euclid) is at:
http://sourceforge.net/projects/cml/
and there are unit tests which will exercise this method and show you how to use it.
计算连接点 (x1,y1) 和 (x2,y2) 的直线的斜率:
m=(y2-y1)/(x2-x1)
连接 (x1,y1) 的直线方程和 (x2,y2) 使用直线方程的点斜率形式,将是
y-y2 = m(x-x2)
连接 (x3,y3) 和 (x4,y4) 的直线的斜率将为 -(1/m)
再次,使用点斜率形式的直线方程连接 (x3,y3) 和 (x4,y4) 的直线方程将为 y-y3 = -(1/m)(x-x3)
求解这两个直线方程就像求解两个变量的线性方程一样,得到的 x 和 y 的值将是 (x4,y4)
我希望这个有帮助。
干杯
Compute the slope of the line joining points (x1,y1) and (x2,y2) as
m=(y2-y1)/(x2-x1)
Equation of the line joining (x1,y1) and (x2,y2) using point-slope form of line equation, would be
y-y2 = m(x-x2)
Slope of the line joining (x3,y3) and (x4,y4) would be
-(1/m)
Again, equation of the line joining (x3,y3) and (x4,y4) using point-slope form of line equation, would be
y-y3 = -(1/m)(x-x3)
Solve these two line equations as you solve a linear equation in two variables and the values of x and y you get would be your (x4,y4)
I hope this helps.
cheers
下面问题的Matlab函数代码
Matlab function code for the following problem
Mathematica 在 2014 年版本 10 中引入了函数
RegionNearest[]
。该函数可用于返回此问题的答案:Mathematica introduced the function
RegionNearest[]
in version 10, 2014. This function could be used to return an answer to this question:这主要是阿恩克里希恩答案的重复。我只是想用完整的 Mathematica 代码片段来完成他的部分:
This is mostly a duplicate of Arnkrishn's answer. I just wanted to complete his section with a complete Mathematica code snippet:
这是已接受答案的 C# 实现。它还使用 ArcGis 返回 MapPoint,因为这就是我们在该项目中使用的。
感谢雷,这对我来说非常有效。
c#arcgis
This is a C# implementation of the accepted answer. It's also using ArcGis to return a MapPoint as that's what we're using for this project.
Thanks to Ray as this worked perfectly for me.
c#arcgis
只是为了完整起见,这里是使用齐次坐标的解决方案。
齐次点是:
p1 = (x1,y1,1), p2 = (x2,y2,1), p3 = (x3,y3,1)
通过两点的直线是它们的叉积
l_12 := p1 x p2 = (y1-y2, x2-x1, x1*y2 - x2*y1)
点到线的(有符号)距离是它们的点积。
d := l_12 * p3 = x3*(y1-y2) + y3*(x2-x1) + x1*y2 - x2*y1
从 p4 到 p3 的向量是 d 乘以 l_12 的法向量除以法向量的平方长度。
n2 := (y1-y2)^2 + (x2-x1)^2
p4 := p3 + d/n2*(y1-y2, x2-x1, 0)
注意:如果将 l_12 除以法线的长度向量
l_12 := l_12 / sqrt((y1-y2)^2 + (x2-x1)^2)
距离 d 将是欧几里德距离。
Just for the sake of completeness, here is a solution using homogeneous coordinates.
The homogeneous points are:
p1 = (x1,y1,1), p2 = (x2,y2,1), p3 = (x3,y3,1)
a line through two points is their cross-product
l_12 := p1 x p2 = (y1-y2, x2-x1, x1*y2 - x2*y1)
The (signed) distance of a point to a line is their dot product.
d := l_12 * p3 = x3*(y1-y2) + y3*(x2-x1) + x1*y2 - x2*y1
The vector from p4 to p3 is d times the normal vector of l_12 divided by the squared length of the normal vector.
n2 := (y1-y2)^2 + (x2-x1)^2
p4 := p3 + d/n2*(y1-y2, x2-x1, 0)
Note: if you divide l_12 by the length of the normal vector
l_12 := l_12 / sqrt((y1-y2)^2 + (x2-x1)^2)
the distance d will be the euclidean distance.
首先计算点确定的线性函数
(x1,y2),(x2,y2)
。我们得到:
这一步很容易通过两点之间的线性函数公式来计算。
然后,计算经过 (x3,y3) 的线性函数 y。
函数斜率是 -m,其中 m 是 y1 的斜率。
然后通过点(x3,y3)的坐标计算const b2。
我们得到 y2 = -mx+b2,其中 m 和 b2 是常数。
最后要做的就是找到 y1, y2 的交集。
您可以通过求解方程来找到 x:
-mx+b2 = mx+b1
,然后将 x 代入其中一个方程来找到 y。First, calculate the linear function determined by the points
(x1,y2),(x2,y2)
.We get:
This step is easy to calculate by the formula of linear function between two points.
Then, calculate the linear function y that goes through (x3,y3).
The function slope is -m, where m is the slope of y1.
Then calculate the const b2 by the coordinates of the point (x3,y3).
We get y2 = -mx+b2 where m and b2 are constants.
The last thing to do is to find the intersection of y1, y2.
You can find x by solving the equation:
-mx+b2 = mx+b1
, then place x in one of the equations to find y.这工作完美:
This work perfectly :
这是一个矢量化 Matlab 函数,用于查找
m
点到n
线段上的成对投影。这里,xp
和yp
是m x 1
向量,保存m
个不同点的坐标,并且x1< /code>、
y1
、x2
和y2
是n x 1
向量,保存起始点和结束点的坐标n
条不同的线段。它返回
m x n
矩阵、x
和y
,其中x(i, j)
和y(i, j)
是第i
点到第j
线上的投影坐标。实际工作在前几行中完成,函数的其余部分运行一个自测试演示,以防不带参数调用它。它相对较快,我在不到 0.05 秒的时间内找到了 2k 个点到 2k 个线段的投影。
This is a vectorized Matlab function for finding pairwise projections of
m
points onton
line segments. Herexp
andyp
arem by 1
vectors holding coordinates ofm
different points, andx1
,y1
,x2
andy2
aren by 1
vectors holding coordinates of start and end points ofn
different line segments.It returns
m by n
matrices,x
andy
, wherex(i, j)
andy(i, j)
are coordinates of projection ofi
-th point ontoj
-th line.The actual work is done in first few lines and the rest of the function runs a self-test demo, just in case where it is called with no parameters. It's relatively fast, I managed to find projections of 2k points onto 2k line segments in less than 0.05s.
我为你解出了方程:
其中 ^2 均方
I solved the equations for you:
Where ^2 means squared
来自关于垂直性的维基百科文章:
通过 (x1, y1) 和 (x2, y2 的直线的斜率 m >) 是 m = (y2 – y1)/(x2 – x1)
From the Wikipedia article on Perpendicularity:
The slope of the line, m, through (x1, y1) and (x2, y2) is m = (y2 – y1)/(x2 – x1)