找到两条线的交点 [英] Finding the intersection of two lines
本文介绍了找到两条线的交点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两行:
y = -1/3x + 4
y = 3x + 85
交叉点位于 [24.3,12.1]
.
我准备了一组坐标:
points = [[1, 3], [4, 8], [25, 10], ... ]
#y = -1/3x + b
m_regr = -1/3
b_regr = 4
m_perp = 3 #(1 / m_regr * -1)
distances = []
points.each do |pair|
x1 = pair.first
y2 = pair.last
x2 = ((b_perp - b_regr / (m_regr - m_perp))
y2 = ((m_regr * b_perp) / (m_perp * b_regr))/(m_regr - m_perp)
distance = Math.hypot((y2 - y1), (x2 - x1))
distances << distance
end
是否有宝石或更好的方法?
Is there a gem or some better method for this?
注意:上面的方法无效.请参阅我的答案以找到有效的解决方案.
NOTE: THE ABOVE METHOD DOES NOT WORK. See my answer for a solution that works.
推荐答案
After much suffering and many different tries, I found a simple algebraic method here that not only works but is dramatically simplified.
distance = ((y - mx - b).abs / Math.sqrt(m**2 + 1))
其中x和y是已知点的坐标.
where x and y are the coordinates for the known point.
这篇关于找到两条线的交点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文