Python:直线,斜率k已知,一点P1已知,长度P1P2已知,如何得到P2?
对于直线,斜率k已知,一个点(x1,y1)已知,如何使用python获得另一个点(x2,y2)? 我知道计算的方法,但不知道如何使用python编写代码Python:直线,斜率k已知,一点P1已知,长度P1P2已知,如何得到P2?,python,Python,对于直线,斜率k已知,一个点(x1,y1)已知,如何使用python获得另一个点(x2,y2)? 我知道计算的方法,但不知道如何使用python编写代码 (y2-y1)/(x2-x1)=k sqrt((x2-x1)^2+(y2-y1)^2)=长度 如果两个未知方程,两个变量的幂为1,linalg应该可以求解,但现在2rd函数的幂为2,如何处理 我试图简化如下,但似乎我无法应用linalg解决方案 kx2-y2-kx1+y1=0 (y2-y1)^2+(x2-x1)^2=长度^2 补充: 感谢你的回
为什么要使用复杂的解决方案呢?我将在假设
P1=(0,0)
的情况下处理它
假设P1
位于原点,并且知道slope=k
,您知道P2=(r,k*r)
对于常数r
。r
可以通过计算距离P1P2
来计算。既然你说你已经知道了距离,那么我们可以简单地做:
r^2 + (k*r)^2 = d^2
要查找r
。一旦找到r
,就可以在P1=(0,0)
时获得P2
的坐标。因此,要找到实际坐标,只需执行P2=P1+P2
。检查以下内容:
import math
# Obviously assume k and d are known constants, and P1 is a known point
k = # given k
d = # given d
p1 = # (given x coord, given y coord)
r_sq = d**2 / (1 + k**2)
r = math.sqrt(r_sq)
p2 = (p1[0] + r, p1[1] + k*r)
print(p2)
如果您必须使用
numpy
,您应该向我们展示您的代码以及困扰您的具体原因。是什么阻止您使用该模块,无论是错误还是意外行为?您可以使用基本触发器解决此问题。这里是一般的推导
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
从数学导入sin,atan
从随机导入randint
#这是(x2,y2)=p2的公式
x=λy2,m,x1,y1:(y2-y1)/浮点数(m)+x1
y=λl,m,y1:l*sin(atan(m))+y1
#p2约束(x2>x1,y2>y1或x2>x1,y2
因此,这里我生成了两个随机点p1和p2,并通过将我的推导结果与实际结果进行比较,验证p2是否可以从斜率、距离和p1计算出来。您的问题能否更具体一些?计算的哪一部分很难编码?你对Python已经了解多少了?对于一条直线,在距离你的点(x1,y1)给定距离D的地方有两个点。一个在(x1+dx,y1+dy)处,另一个在(x1-dx,y1-dy)处。您应该查找有关将坡度转换为角度的反正切函数(称为
atan
)的信息。从这里开始,正弦和余弦将给出dx和dy。你的问题实际上不是关于Python,而是关于三角学。一旦你有了一个trig公式来解决你的问题,它就可以很容易地编码到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