LoginSignup
0
0

More than 5 years have passed since last update.

sympyで「点と線分との最短距離(perpendicular)」をquaternionでやろうとした。//solveの引数にmatrixが使えた。

Last updated at Posted at 2018-07-24

(参考)点と線分との最短距離
https://qiita.com/kkdd/items/b3c5e06798e59fe2768e
(参考)sympyで線分上の最も近い点
https://qiita.com/mrrclb48z/items/dacf38b39cca8ed50b6e
(参考)アフィン変換/平行移動だって変換行列で
https://qiita.com/yuba/items/7fb6a49adfda8fa466d8
(参考)座標変換 回転行列 まとめ
https://qiita.com/MENDY/items/eeb0a4b067e596fd98dd

fullscript.py
(2)/2
from sympy import *
var("A B C D E")
var("x y")
var("ct st dx dy ds")
var("x1 y1")
var("x2 y2")
A =Matrix(([ct,-st,dx], [st,ct,dy], [0,0,1]))
B =Matrix(([0,0], [ds,0], [1,1]))
C =Matrix(([x1,x2], [y1,y2], [1,1]))
D =Matrix(([x], [y], [1]))
dict=solve(A*B-C, [ct,st,dx,dy])
A=A.replace(ct,dict[ct]).replace(st,dict[st]).replace(dx,dict[dx]).replace(dy,dict[dy]).replace(ct**2 + st**2,1)
print("A=",A)
E=A.inv()*D.replace(-x1+x2,x2-x1).replace(-y1+y2,y2-y1)
print("H=",E[1])
E[1]=E[1].replace(ds,sqrt( (x2-x1)**2+(y2-y1)**2))
E[1]=E[1].subs({x1:0,y1:0, x2:1, y2:1, x:0, y:1})
print("H=",E[1])
print("H=",float(E[1]))
print('-----------------------------------------------------------------------------------------')
A=A.subs(ds,sqrt( (x2-x1)**2+(y2-y1)**2))
A=A.subs({x1:0,y1:0, x2:1, y2:1, x:0, y:1})
print("A=",A)
E[0]=E[0].subs(ds,sqrt( (x2-x1)**2+(y2-y1)**2))
E[0]=E[0].subs({x1:0,y1:0, x2:1, y2:1, x:0, y:1})
E[1]=E[1].subs({x1:0,y1:0, x2:1, y2:1, x:0, y:1})
E[2]=1
A=A.subs(ds,sqrt( (x2-x1)**2+(y2-y1)**2))
F=A*E
print("E=",E)
print('HX=',float(E[0]),' HY=',float(E[1]))
# A= Matrix([[(y1 - y2)/ds, -(-x1 + x2)/ds, x2], [(-x1 + x2)/ds, (y1 - y2)/ds, y2], [0, 0, 1]])
# H= (x2*(-x1 + x2)/ds - y2*(y1 - y2)/ds)/((-x1 + x2)**2/ds**2 + (y1 - y2)**2/ds**2) - x*(-x1 + x2)/(ds*((-x1 + x2)**2/ds**2 + (y1 - y2)**2/ds**2)) + y*(y1 - y2)/(ds*((-x1 + x2)**2/ds**2 + (y1 - y2)**2/ds**2))
# H= sqrt(2)/2
# H= 0.7071067811865476
# -----------------------------------------------------------------------------------------
# A= Matrix([[-sqrt(2)/2, -sqrt(2)/2, 1], [sqrt(2)/2, -sqrt(2)/2, 1], [0, 0, 1]])
# E= Matrix([[sqrt(2)/2], [sqrt(2)/2], [1]])
# HX= 0.7071067811865476  HY= 0.7071067811865476
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0