四面体の体積「2023神戸大学前期理系第4問」をsympyとFreeCADでやってみた。

FreeCADのマクロで作図しました。
ワイヤーフレームです。ソリッドを勉強中。

オリジナル

https://mathmathmass.exblog.jp/33021014/

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

https://mathexamtest.web.fc2.com/2023/202310601/2023106010100mj.html#top-0103

# sympyで(オリジナルのやり方)
T氏様の方法を勉強中。

# sympyで(sympy的?安易なやり方)
四面体(三角錐)の全部の辺の長さを計算。
全部の点の座標を計算後、(1)(2)(3)
点A,点B,点CはXY平面上。
点Cは、メソッドで、2円の交点。
点Oは、solveで、3球面の交点。答えがでるとは思いませんでした。

```python
from sympy import *
var('x y z s t')
def mySphereFormula(myP,myR):
    return ((x-myP.x)**2+(y-myP.y)**2+(z-myP.z)**2-myR**2).expand()
def mySimentaiTaiseki(PTO,PTA,PTB,PTC):
    return Matrix([[PTA.x-PTO.x, PTA.y-PTO.y, PTA.z-PTO.z],
                   [PTB.x-PTO.x, PTB.y-PTO.y, PTB.z-PTO.z],
                   [PTC.x-PTO.x, PTC.y-PTO.y, PTC.z-PTO.z]]
                  ).det()/6
OA=sqrt(13)
OB=5
OC=5
OAdotOB=  1
OAdotOC=  1
OBdotOC=-11
AB=(OA**2+OB**2-2*OA*OB*        (OAdotOB/(OA*OB)))**Rational(1,2)
BC=(OB**2+OC**2-2*OB*OC*Rational(OBdotOC,(OB*OC)))**Rational(1,2)
CA=(OC**2+OA**2-2*OC*OA*        (OAdotOC/(OA*OC)))**Rational(1,2)
print("#(1)",CA)
A=Point( 0,0,0)
B=Point(AB,0,0)
ans=Circle(Point(0,0),CA).intersection(Circle(Point(AB,0),BC))
C=Point(ans[1].x,ans[1].y,0)
O=Point(solve([mySphereFormula(A,OA),
               mySphereFormula(B,OB),
               mySphereFormula(C,OC)],[x,y,z])[1]
        )
H=Plane(A,B,C).projection(O)
myEq=Eq((H-O),(A-O)+s*(B-A)+t*(C-A))
print("#(2)",solve([Eq(myEq.lhs.x,myEq.rhs.x),
                    Eq(myEq.lhs.y,myEq.rhs.y),
                    Eq(myEq.lhs.z,myEq.rhs.z)
                  ],[s,t]))
print("#(3)",abs(mySimentaiTaiseki(O,A,B,C)))
#(1) 6
#(2) {s: 1/3, t: 1/3}
#(3) 6*sqrt(5)
```
# FreeCADのマクロで作図

```python
import FreeCAD
import Part
import DraftTools
import Draft
import Mesh
############################################################################
# 計算
from sympy import *
var('x y z s t')
def mySphereFormula(myP,myR):
    return ((x-myP.x)**2+(y-myP.y)**2+(z-myP.z)**2-myR**2).expand()
def mySimentaiTaiseki(PTO,PTA,PTB,PTC):
    return Matrix([[PTA.x-PTO.x, PTA.y-PTO.y, PTA.z-PTO.z],
                   [PTB.x-PTO.x, PTB.y-PTO.y, PTB.z-PTO.z],
                   [PTC.x-PTO.x, PTC.y-PTO.y, PTC.z-PTO.z]]
                  ).det()/6
OA=sqrt(13)
OB=5
OC=5
OAdotOB=  1
OAdotOC=  1
OBdotOC=-11
AB=(OA**2+OB**2-2*OA*OB*        (OAdotOB/(OA*OB)))**Rational(1,2)
BC=(OB**2+OC**2-2*OB*OC*Rational(OBdotOC,(OB*OC)))**Rational(1,2)
CA=(OC**2+OA**2-2*OC*OA*        (OAdotOC/(OA*OC)))**Rational(1,2)
print("#(1)",CA)
A=Point( 0,0,0)
B=Point(AB,0,0)
ans=Circle(Point(0,0),CA).intersection(Circle(Point(AB,0),BC))
C=Point(ans[1].x,ans[1].y,0)
O=Point(solve([mySphereFormula(A,OA),
               mySphereFormula(B,OB),
               mySphereFormula(C,OC)],[x,y,z])[1]
        )
H=Plane(A,B,C).projection(O)
myEq=Eq((H-O),(A-O)+s*(B-A)+t*(C-A))
print("#(2)",solve([Eq(myEq.lhs.x,myEq.rhs.x),
                    Eq(myEq.lhs.y,myEq.rhs.y),
                    Eq(myEq.lhs.z,myEq.rhs.z)
                  ],[s,t]))
print("#(3)",abs(mySimentaiTaiseki(O,A,B,C)))
######################################################################
# 3D作図
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.5 mm'
    return
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]) )
    return
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)
    return
def myLine_S(*args):
    for i in range(1,len(args)):
        myLine(args[i-1],args[i])
    return
def myLine_C(*args):
    for i in range(1,len(args)):
        myLine(args[i-1],args[i])
    myLine(args[i],args[0])
    return
#
myLine_C  (A,B,C)
myLine    (O,A)
myLine    (O,B)
myLine    (O,C)
myLine    (O,H)
myTxtXYZ_S(O,"O",A,"A",B,"B",C,"C",H,"H")
#
doc = App.activeDocument()
App.ActiveDocument.addObject("App::Origin", "Origin")
App.ActiveDocument.getObject('Origin').Visibility = True
App.ActiveDocument.recompute()
Gui.activeDocument().activeView().viewAxonometric()
Gui.SendMsgToActiveView("ViewFit")
```

![1png.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/109698/7c25422b-be51-a061-1523-f53ea9174951.png)

originをマウスで非表示

![2png.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/109698/ea35dcb7-7dc9-fdd9-c1b3-bdb355beb52f.png)

# 参考

https://qiita.com/mrrclb48z/items/48fec792ce4dda5b5a65