空間ベクトル「京都府立大学2022年前期環境・情報科学科第３問」sympyとFreeCADでやってみた。

3D作図したら、謎が増えました。アドバイスををいただけると幸いです。

原点O,A(1,0,0),B(0,2,0),C(0,0,3)
オリジナル

https://mathmathmass.exblog.jp/32122140/

上と同じです。大学入試数学問題集成＞【３】

https://mathexamtest.web.fc2.com/2022/202211546/2022115460100mj.html


# 公式サイト
問題,解答

https://www.kpu.ac.jp/media/04zen_math_kanjo.pdf#page=4

https://www.kpu.ac.jp/media/04zen_math_kanjo-kaito.pdf#page=4

https://www.kpu.ac.jp/admissions/exam/past/general/

# sympyで

```python
from sympy import *
from sympy.vector import ImplicitRegion
var('x y z')
var('u v w')
r=Symbol('r', positive=True)
R=Symbol('R', positive=True)
A=Point3D(1,0,0)
B=Point3D(0,2,0)
C=Point3D(0,0,3)
O=Point3D(0,0,0)
a=Plane(A,B,C)
print("#(1)  ",a.equation())
C12=Point3D(r,r,r)
ans=solve(a.distance(C12)-r,r)
r1=ans[0]
r2=ans[1]
C1=C12.subs({r:r1})
C2=C12.subs({r:r2})
sphereQ1=ImplicitRegion((x,y,z),(x-C1.x)**2+(y-C1.y)**2+(z-C1.z)**2 -r1**2 )
sphereQ2=ImplicitRegion((x,y,z),(x-C2.x)**2+(y-C2.y)**2+(z-C2.z)**2 -r2**2 )
print("#(2)Q1",sphereQ1)
print("#(2)Q2",sphereQ2)
print("#(3)  ",a.intersection(Line3D(Point3D(1/3, 1/3, 1/3),Point3D(3/2, 3/2, 3/2)))[0])
Z=Point3D(u,v,w)
ans=solve([Eq(A.distance(Z),R),Eq(B.distance(Z),R),Eq(C.distance(Z),R),Eq(O.distance(Z),R)],
          [u,v,w,R])[0]
print("#(4)  ",ImplicitRegion((x, y, z), (x-ans[0])**2 + (y-ans[1])**2 + (z-ans[2])**2 -ans[3]**2))
#(1)   6*x + 3*y + 2*z - 6
#(2)Q1 ImplicitRegion((x, y, z), (x - 1/3)**2 + (y - 1/3)**2 + (z - 1/3)**2 - 1/9)
#(2)Q2 ImplicitRegion((x, y, z), (x - 3/2)**2 + (y - 3/2)**2 + (z - 3/2)**2 - 9/4)
#(3)   Point3D(6/11, 6/11, 6/11)
#(4)   ImplicitRegion((x, y, z), (x - 1/2)**2 + (y - 1)**2 + (z - 3/2)**2 - 7/2)
```

(4)のsolveについて、uvwR距離2乗でもできました。
```
solve..**1...[u,v,w,R] ===>solve...**2...[u,v,w,R]
```


# FreeCADのマクロで
(1)～(3)
![png1.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/109698/fd67406c-9e78-2960-f87c-9d60cb104ff0.png)
(4)
![png4.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/109698/4487cf93-cefa-90a9-7500-e3af08e0a5be.png)


上のsympy計算に、FreeCAD作図を追加しました。
変な線がでています。
上の図は、マクロそのままではありません。手を加えています。
```python
import FreeCAD
import Part
import Draft
import Mesh
#########################################################################################################
# 計算
from sympy import *
from sympy.vector import ImplicitRegion
var('x y z')
var('u v w')
r=Symbol('r', positive=True)
R=Symbol('R', positive=True)
A=Point3D(1,0,0)
B=Point3D(0,2,0)
C=Point3D(0,0,3)
O=Point3D(0,0,0)
a=Plane(A,B,C)
print("#(1)  ",a.equation())
C12=Point3D(r,r,r)
ans=solve(a.distance(C12)-r,r)
r1=ans[0]
r2=ans[1]
C1=C12.subs({r:r1})
C2=C12.subs({r:r2})
sphereQ1=ImplicitRegion((x,y,z),(x-C1.x)**2+(y-C1.y)**2+(z-C1.z)**2 -r1**2 )
sphereQ2=ImplicitRegion((x,y,z),(x-C2.x)**2+(y-C2.y)**2+(z-C2.z)**2 -r2**2 )
print("#(2)Q1",sphereQ1)
print("#(2)Q2",sphereQ2)
C1aC2=a.intersection(Line3D(Point3D(1/3, 1/3, 1/3),Point3D(3/2, 3/2, 3/2)))[0]
print("#(3)  ",C1aC2)
Z=Point3D(u,v,w)
ans=solve([Eq(A.distance(Z),R),Eq(B.distance(Z),R),Eq(C.distance(Z),R),Eq(O.distance(Z),R)],
          [u,v,w,R])[0]
print("#(4)  ",ImplicitRegion((x, y, z), (x-ans[0])**2 + (y-ans[1])**2 + (z-ans[2])**2 -ans[3]**2))
#(1)   6*x + 3*y + 2*z - 6
#(2)Q1 ImplicitRegion((x, y, z), (x - 1/3)**2 + (y - 1/3)**2 + (z - 1/3)**2 - 1/9)
#(2)Q2 ImplicitRegion((x, y, z), (x - 3/2)**2 + (y - 3/2)**2 + (z - 3/2)**2 - 9/4)
#(3)   Point3D(6/11, 6/11, 6/11)
#(4)   ImplicitRegion((x, y, z), (x - 1/2)**2 + (y - 1)**2 + (z - 3/2)**2 - 7/2)
# #########################################################################################################
### 3D作図
def my_Sphere_3D(xyz,r):
    sphere = doc.addObject("Part::Sphere", "mySphere2")
    sphere.Radius =   float(r)
    sphere.Angle1 = -90
    sphere.Angle2 =  90
    sphere.Angle3 = 360
    sphere.Placement = App.Placement(App.Vector(float(xyz.x),float(xyz.y),float(xyz.z)), App.Rotation(0, 90, 0))
    #FreeCADGui.getDocument('Unnamed').getObject('mySphere2').Transparency = 90
    FreeCADGui.getDocument('Unnamed').getObject('mySphere2').Transparency = 40
def my_Sphere(my_R):
    sphere = doc.addObject("Part::Sphere", "mySphere2")
    sphere.Radius =   1
    sphere.Angle1 = -90
    sphere.Angle2 =  90
    sphere.Angle3 = 360
    sphere.Placement = App.Placement(App.Vector(0, 0, 0), App.Rotation(0, 90, 0))
    FreeCADGui.getDocument('Unnamed').getObject('mySphere2').Transparency = 90
def myXYZ2Txt(A):
    return '(' + str(A.x) + ',' + str(A.y) + ',' + str(A.z) + ')'
def myTxtXYZ(A,myWedgei):
    P5x=float(A.x)
    P5y=float(A.y)
    P5z=float(A.z)
    p5 = FreeCAD.Vector(P5x, P5y, P5z)
    myText = Draft.makeText(myWedgei, p5)
    myText.Label = myWedgei
    FreeCADGui.ActiveDocument.ActiveObject.FontSize = '0.12 mm'
def myLine(A,B):
    Ax,Ay,Az=float(A.x),float(A.y),float(A.z)
    Bx,By,Bz=float(B.x),float(B.y),float(B.z)
    pl = FreeCAD.Placement()
    pl.Rotation.Q = (0.4247081540122249, 0.17592004639554645, 0.33985110062924484, 0.8204732460821097)
    pl.Base = FreeCAD.Vector(-3.9166066876399563, -2.1670824762243774, 1.7495260956243028)
    points = [FreeCAD.Vector(Ax,Ay,Az), FreeCAD.Vector(Bx,By,Bz)]
    line = Draft.make_wire(points, placement=pl, closed=False, face=True, support=None)
    Draft.autogroup(line)
def myLine_S(*args):
    for i in range(1,len(args)):
        myLine(args[i-1],args[i])
    return 0
def myTxtXYZ_S(*xy_tx):
    for i in range(1,int(len(xy_tx)/2)+1):
        myTxtXYZ(xy_tx[2*i-2],xy_tx[2*i-1]+myXYZ2Txt(xy_tx[2*i-2]) ) 
myTxtXYZ_S(O,"O",A,"A",B,"B",C,"C",C1,"C1",C2,"C2",C1aC2,"C1aC2")
myLine_S(A,B,C,A)
myLine_S(C1,C1aC2,C2)
myLine  (O,A)
myLine  (O,B)
myLine  (O,C)
doc = App.activeDocument()
my_Sphere_3D(C1,r1)
my_Sphere_3D(C2,r2)
my_Sphere_3D(Point3D(1/2,1,3/2),sqrt(7/2))
App.ActiveDocument.addObject("App::Origin", "Origin")
App.ActiveDocument.getObject('Origin').Visibility = True
App.ActiveDocument.recompute()
Gui.activeDocument().activeView().viewAxonometric()
Gui.SendMsgToActiveView("ViewFit")
```