空間図形・実装1
直線と平面の方程式をPythonで実装
この記事を書くのは3回め。
PCの調子が悪いのか、原因不明だけど公開すると記事が消えた(涙)。気を取り直して。
直線と平面の交点を求めたい
直線: x = x0 + tm
x0:直線上の点
m:方向ベクトル
t:係数 (長さかな?)
→ (x-x0) / a = (y-y0) / b = (z-z0) / c
平面: ax + by +cz = h
(n,x)=h
n:法線ベクトル
x:平面上の任意の点
import numpy as np #直線Class class vecLine(): m = np.zeros(3, dtype=np.float) # 方向ベクトル r0 = np.zeros(3, dtype=np.float) # 直線上の点(起点) t = 1.0 # 直線長さ # 直線設定(起点、ベクトル、長さ) def setLineVecL(self, set_r0, set_m, set_t): self.m = np.array(set_m) self.r0 = np.array(set_r0) self.t = set_t # 直線設定(2点) def setLinePos(self, set_p1, set_p2): self.m = np.array(np.array(set_p2) - np.array(set_p1)) self.r0 = np.array(set_p1) self.t = np.linalg.norm((np.array(set_p2) - np.array(set_p1)), ord=2) # 直線要素の取り出し def getLine(self): return(self.m, self.r0, self.t) #平面Class class vecPlane(): n = np.zeros(3, dtype=np.float) # 平面の法線ベクトル h = 1 # 符号付き距離 # 平面の設定(法線ベクトル、符号付きの距離) def setPlane(self, set_n, set_h): self.n = set_n self.h = set_h # 平面の設定(3点) def setPlanePos3(self, set_pos1, set_pos2, set_pos3): # https://risalc.info/src/plane-signed-distance.html self.n = np.cross(np.array(set_pos2) - np.array(set_pos1), np.array(set_pos3) - np.array(set_pos1)) self.h = np.dot(self.n, np.array(set_pos1)) # 平面の設定(法線ベクトル、一点) def setPlanePos1H(self, set_pos1, set_n): self.n = np.array(set_n) x0 = np.array(set_pos1) self.h = np.dot(self.n, x0) # 平面の要素取り出し def getPlane(self): return (self.n, self.h)
一旦ここまで。
交点の求め方。
# 複合処理 class vecLinePlane(): vLine = vecLine() vPlane = vecPlane() angle = 0.0 # なす角 intersection = np.zeros(3, dtype=np.float) # 交点 # 平面と直線の交点の算出 def getIntersection(self, cLine, cPlane): intersection = cLine.m * (cPlane.h - np.dot(cPlane.n, cLine.r0)) / np.dot(cPlane.n, cLine.m) + cLine.r0 return intersection
(下記のサイトを参考にしました)
http://tau.doshisha.ac.jp/lectures/2008.intro-seminar/html.dir/node29.html
このコードはクラス定義しただけ。実行部分がない。
肩こりがひどく、今日はここまで。。。