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