「平面上での「拡大・縮小・回転・平行移動」およびその組み合わせは、.....」を参考にSymPy Liveでやってみた。

同じ答えがでました。正しいですか？

オリジナル

https://qiita.com/hsjoihs/items/77ed087fc5f909239bc1

`transform="matrix(0.64 2.52 -2.52 0.64 36.36 -12.52)"`
#SymPy Liveで(Pycharmでも)

以下サイトに、ソースコードを貼り付けて、Evaluateです。

https://live.sympy.org/

```python
from sympy import *
def myComputeSvgTransform(beforea,beforeb, aftera, afterb):
    d_x = beforeb.x - beforea.x
    d_y = beforeb.y - beforea.y
    d_afterx = afterb.x - aftera.x
    d_aftery = afterb.y - aftera.y
    K = d_x * d_x + d_y * d_y
    u_r = (d_x * d_afterx + d_y * d_aftery) / K
    u_i = (d_x * d_aftery - d_y * d_afterx) / K
    w_x= aftera.x - u_r * beforea.x + u_i * beforea.y
    w_y= aftera.y - u_r * beforea.y - u_i * beforea.x
    return Matrix([u_r,u_i,-u_i,u_r,w_x,w_y]).n()\
            .applyfunc(lambda x: Symbol('{:.2f}'.format(x)))
beforea,beforeb=Point2D( 2, 7),Point2D( 5,3)
aftera ,afterb =Point2D(20,-3),Point2D(32,2)
print("#",               myComputeSvgTransform(beforea,beforeb, aftera, afterb))
print("#",       flatten(myComputeSvgTransform(beforea,beforeb, aftera, afterb)))
print("#",Matrix(flatten(myComputeSvgTransform(beforea,beforeb, aftera, afterb))))
# Matrix([[0.64], [2.52], [-2.52], [0.64], [36.36], [-12.52]])
# [0.64, 2.52, -2.52, 0.64, 36.36, -12.52]
# Matrix([[0.64], [2.52], [-2.52], [0.64], [36.36], [-12.52]])
```

matrixとsolveで

```python
from sympy import *
var('u_r  u_i  w_x  w_y ')
def myMatrix_ComputeSvgTransform(myBef,myAft):
    myA = Matrix([
        [u_r,-u_i, w_x],
        [u_i, u_r, w_y],
        [  0,   0,   1]
    ])
    ans=solve(myA * myBef - myAft, [u_i,u_r,w_x,w_y])
    return Matrix([ans[u_r],ans[u_i],-ans[u_i],ans[u_r],ans[w_x],ans[w_y]]).n()\
            .applyfunc(lambda x: Symbol('{:.2f}'.format(x)))
def myPoint2DtoMatrix(myPoint2D):
    return Matrix([myPoint2D.x,myPoint2D.y,1])
bef_a,bef_b=Point2D( 2, 7),Point2D( 5,3)
aft_a,aft_b=Point2D(20,-3),Point2D(32,2)
myBef=Matrix([flatten(myPoint2DtoMatrix(bef_a)),flatten(myPoint2DtoMatrix(bef_b))])
myAft=Matrix([flatten(myPoint2DtoMatrix(aft_a)),flatten(myPoint2DtoMatrix(aft_b))])
print("#",myMatrix_ComputeSvgTransform(myBef.T,myAft.T))
# Matrix([[0.64], [2.52], [-2.52], [0.64], [36.36], [-12.52]])
```

後から値を代入してみた。

```python
from sympy import *
var('bx1 by1 bx2 by2')
var('ax1 ay1 ax2 ay2')
var('u_r  u_i  w_x  w_y')
def myMatrix_ComputeSvgTransform(myBef,myAft):
    myA = Matrix([
        [u_r,-u_i, w_x],
        [u_i, u_r, w_y],
        [  0,   0,   1]
    ])
    ans=solve(myA * myBef - myAft, [u_i,u_r,w_x,w_y])
    return Matrix([ans[u_r],ans[u_i],-ans[u_i],ans[u_r],ans[w_x],ans[w_y]])
def myPoint2DtoMatrix(myPoint2D):
    return Matrix([myPoint2D.x,myPoint2D.y,1])
bef_a,bef_b=Point2D(bx1,by1),Point2D(bx2,by2)
aft_a,aft_b=Point2D(ax1,ay1),Point2D(ax2,ay2)
myBef=Matrix([flatten(myPoint2DtoMatrix(bef_a)),flatten(myPoint2DtoMatrix(bef_b))])
myAft=Matrix([flatten(myPoint2DtoMatrix(aft_a)),flatten(myPoint2DtoMatrix(aft_b))])
mySub={bx1:2,by1:7, bx2:5, by2:3, ax1:20, ay1:-3, ax2:32, ay2:2}
print("#",myMatrix_ComputeSvgTransform(myBef.T,myAft.T)
         .subs(mySub).n().applyfunc(lambda x: Symbol('{:.2f}'.format(x))))
# Matrix([[0.64], [2.52], [-2.52], [0.64], [36.36], [-12.52]])
```