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来计算我的派生结果到实际结果。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值