首页 > 编程知识 正文

python中 自定义向量 计算向量乘积, 平行四边形面积,三角形面积

时间:2023-05-04 19:39:54 阅读:178508 作者:1849

# coding=utf-8 frommathimportsqrt,acos, 根据piclassvector(object ) :“”“docstring for vector”“”坐标轴列表的输入创建向量,然后, 此向量所在的空间维度' ' CANNOT_NORMALIZE_ZERO_VECTOR虚心的荷花=' cannotnormalizethezerovector ' no _ unique _ orthogonal _ vector coordinates ) : super(Vector, self (_ init _ (try : if not coordinates 3360 raisevalueerrorself.coordinates=tuple ) coordinates ) self.Didi nates excex dinatesmustbenonempty ' ) except typeerror 3360 raise typeerror (' thecoordinatesmustbeaniterable ' ) def __str__(self ) 向量长度defmagnitude(self ) : coordinates _ squared=[ x * *2forxinself.coordinates ] return sqrt ) sum ) cooordinates _ com w ) : new _ cordinates=[ ] new _ cordinates.append (self.coordinates [1] * w.coordinates [2]-w.coordinates w.coordinates [2]-w.coordinates [0] * self.coordinates w.coordinates [1]-w.coordinates [0] * self.coordinates 平行四边形面积defareaofpaaates ) )平行四边形w ) : vector _ product=self.cross product (w ) return vector_product.magnitude ) # v ) : return self.areaofparallelogram (v )/2 v1=vector ([ 8.462,7.893,-8.187] ) v2=vector([6.984,- 5.975 - 9.838,5.031 ) V4=vector([-4.268,-1.861,-8.866] ) printV3.areaofparallelogram(V4 ) V5=vector ([ 1.5 ] 5.791 ) 5.779 ] print V5.areaoftriangle (V6 ) vector:(-11.2045709999994,-97.609444,-105.68516199999999 ) # 142.

#三角形面积defareaoftriangle(self,v ) : return self.areaofparallelogram (v )/2defcross ) self,v ) : try: x_1, z _2=v.coordinates new _ cordinates=[ y _1* z _2- y _2* z _ 1,-(x_1*z_2 - x_2*z_1), x e:msg=str(e ) if msg==' need more than2valuestounpack ' : self _ embedded _ in _ R3=vector ) self.coordinator return self _ embedded _ in _ R3.cross (v _ embedded _ in _ R3 ) elif (msg==' toomanyvaluestounpack ' or msg=3360 ) else 3360 raisedefarea _ of _ parallelogram v ) : cross _ product=self.cross (v ) return cross_product.magnitude

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。