当前位置:   article > 正文

python拟合直线的斜率_Python:直线,斜率k是已知的,一点P1是已知的,长度P1P2是已知的,如何得到P2?...

已知斜率 直线拟合

你可以用basic trig来解决这个问题。这是一般推导。

let p1 = (x1,y1) & p2 = (x2 = x1+d, y2 = y1+h),

let L be the distance between p1 & p2

* note for p1 & p2 such that x1 != x2 && y1 != y2, a triangle can be formed Ldh such that tan(theta) = h/d

h/d is the slope of the line (m) connecting points p1 & p2, so tan(theta) = m

=> theta = atan(m), from the law of sines (sin(a)/A = sin(b)/B)

=> sin(90)/L = sin(atan(theta))/y2

=> y2 = L*sin(atan(theta))

now get x from the point slope form of a line y= y1+m(x-x1) = (y-y1)/m +x1

so x2 = (y2-y1)/m + x1

下面是这个表达在Python:

from math import sin, atan

from random import randint

# This is the formula for (x2,y2) = p2

x = lambda y2, m, x1, y1: (y2 - y1)/float(m) + x1

y = lambda l, m, y1: l*sin(atan(m) ) + y1

# p2 constraints (x2 > x1, y2 > y1 or x2 > x1, y2 < y1)

p1 = [randint(1,1000),randint(1,1000)]

p2 = [randint(p1[0],1001),randint(0,p1[1])]

# calculate distance between p1 & p2 (L), also calculate slope (m)

slope = lambda x1,y1,x2,y2: (y2-y1)/float(x2-x1)

dist = lambda x1,y1,x2,y2: ((y2-y1)**2 + (x2-x1)**2 )**(0.5)

L = dist(p1[0],p1[1],p2[0],p2[1])

m = slope(p1[0],p1[1],p2[0],p2[1])

# now see if our functions for x & y yield p2

y2 = y(L,m,p1[1])

p_derived = [ x(y2,m,p1[0],p1[1]),y2 ]

print "p1: ",p1 , "p2 actual: ",p2, "p2 derived: ",p_derived

所以我在这里产生两个随机点P1和P2,并验证通过比较P2可以从坡上,距离和P1来计算我的派生结果到实际结果。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/779337
推荐阅读
相关标签
  

闽ICP备14008679号