前段时间我在math.stackexchange上问了一个问题并得到了答案。我很难从该答案中获得算法,因为我的背景是设计中的,希望你们中的一些人可以帮助我。
带有视觉草图的原始问题和可能的答案在这里: https://math.stackexchange.com/questions/667432/triangle-with-two-constraints-each-corner-on-a-given-line
问题是:给定3个三维线(a,b和c)在公共点S和b上的给定点B重合,我正在寻找a上的点A和点C上的点c其中AB和BC的长度相同,角度ABC为90度。
我必须用命令式语言实现这个算法,C ++,Java,命令式伪代码或类似代码中的任何代码都可以。
此外,对此问题的不同方法也同样受欢迎。另外:如果完整的解决方案确实太耗时,感谢任何提示!
答案 0 :(得分:2)
两个关键公式是
(我已经回答了数学堆栈交换网站中公式的推导)
代入第二个中的第一个最后给出了一个4阶方程式,用封闭形式求解非常烦人。因此,我在Python中使用了一个简单的数值求解器:
# function to solve (we look for t such that f(t)=0)
def f(t):
s = (t*cB - B2) / (t*ac - aB)
return s*s - 2*s*aB - t*t + 2*t*cB
# given f and an interval to search generates all solutions in the range
def solutions(f, x0, x1, n=100, eps=1E-10):
X = [x0 + i*(x1 - x0)/(n - 1) for i in xrange(n)]
Y = map(f, X)
for i in xrange(n-1):
if (Y[i]<0 and Y[i+1]>=0 or Y[i+1]<0 and Y[i]>=0):
xa, xb = X[i], X[i+1]
ya, yb = Y[i], Y[i+1]
if (xb - xa) < eps:
# Linear interpolation
# 0 = ya + (x - xa)*(yb - ya)/(xb - xa)
yield xa - ya * (xb - xa) / (yb - ya)
else:
for x in solutions(f, xa, xb, n, eps):
yield x
搜索算法对区间中的函数进行采样,当发现两个相邻的样本越过f = 0行时,在这两个样本之间递归地重复搜索(除非区间大小低于指定的限制,否则近似于在这种情况下,一条线并计算交叉点。)
我已经测试了算法产生随机问题并用
解决它们from random import random as rnd
for test in xrange(1000):
a = normalize((rnd()-0.5, rnd()-0.5, rnd()-0.5))
b = normalize((rnd()-0.5, rnd()-0.5, rnd()-0.5))
c = normalize((rnd()-0.5, rnd()-0.5, rnd()-0.5))
L = rnd() * 100
B = tuple(x*L for x in b)
aB = dot(a, B)
cB = dot(c, B)
B2 = dot(B, B)
ac = dot(a, c)
sols = list(solutions(f, -1000., 1000.))
并且存在解决方案为0,1,2,3或4的情况。例如问题
a = (-0.5900900304960981, 0.4717596600172049, 0.6551614908475357)
b = (-0.9831451620384042, -0.10306322574446096, 0.15100848274062748)
c = (-0.6250439408232388, 0.49902426033920616, -0.6002456660677057)
B = (-33.62793897729328, -3.5252208930692497, 5.165162011403056)
有四种不同的解决方案:
s = 57.3895941365 , t = -16.6969433689
A = (-33.865027354189415, 27.07409541837935, 37.59945205363035)
C = (10.436323283003153, -8.332179814593692, 10.022267893763457)
|A - B| = 44.5910029061
|C - B| = 44.5910029061
(A - B)·(C - B) = 1.70530256582e-13
s = 43.619078237 , t = 32.9673082734
A = (-25.739183207076163, 20.5777215193455, 28.577540327140607)
C = (-20.606016281518986, 16.45148662649085, -19.78848391300571)
|A - B| = 34.5155582156
|C - B| = 34.5155582156
(A - B)·(C - B) = 1.13686837722e-13
s = -47.5886624358 , t = 83.8222109697
A = (28.08159526800866, -22.450411211385674, -31.17825902887765)
C = (-52.39256507303229, 41.82931682916268, -50.313918854788845)
|A - B| = 74.0747844969
|C - B| = 74.0747844969
(A - B)·(C - B) = 4.54747350886e-13
s = 142.883074325 , t = 136.634726869
A = (-84.31387768560096, 67.4064705656035, 93.61148799140805)
C = (-85.40270813540043, 68.1840435123674, -82.01440263735996)
|A - B| = 124.189861967
|C - B| = 124.189861967
(A - B)·(C - B) = -9.09494701773e-13
答案 1 :(得分:0)
为λ,mu未知数(在矩阵形式上方)写出两个二次方程 用纸,笔和头,或任何数学软件,如Maple,Mathematica,Matlab,Derive等解决这个系统。你将4th order equation。它有封闭形式的解决方案 - 应用法拉利或Kardano方法并获得真正的根,找到mu,lambda,然后点坐标。