解平行四边形

解平行四边形之python脚本


有时候我们会遇到解平行四边形的问题,比如,已知平行四边形的长,斜宽和一个夹角,求对角线长度。虽然方法简单,但如果需要大量计算,并且方式固定,我们就可以写一个脚本来实现这种运算。
附上最终的代码:

# -*- coding: utf-8 -*-
import math
from optparse import OptionParser
import prettytable as pt
def main():
	parser = OptionParser()
	parser.add_option("-l","-L",dest="long",type="float",help="store the long of parallelogram")
	parser.add_option("-s","-S",dest="short",type="float",help="store the short of parallelogram")
	parser.add_option("-J",dest="angle2",type="float",help="store the big angle of parallelogram")
	parser.add_option("-g",dest="height",type="float",help="store the height of parallelogram")
	parser.add_option("-j",dest="angle1",type="float",help="store the small angle of parallelogram")
	(options, args) = parser.parse_args()
	long=options.long
	short=options.short
	jiao1=options.angle1
	gao=options.height
	jiao2=options.angle2
	if(gao!=None)&(jiao1!=None)&(long==None)&(short==None):
		calculate2_1(gao,jiao1)
	if(gao!=None)&(jiao2!=None)&(long==None)&(short==None):
		calculate2_2(gao,jiao2)
	if (long!=None)&(short!=None)&(jiao1!=None):
		calculate3_1(long,short,jiao1)
	if (long!=None)&(short!=None)&(jiao2!=None):
		calculate3_2(long,short,jiao2)
def calculate2_1(gao,jiao1):
	jiao11=math.radians(jiao1)
	BC=round((gao/math.sin(jiao11)),4)
	EB=round(BC*math.cos(jiao11),4)
	角A=角C=round(math.degrees(math.pi-jiao11),4)
	角D=jiao1
	tb=pt.PrettyTable(["高EC","角B",])
	tb.add_row([gao,jiao1])
	print("*当数据如下时:")
	print(tb)
	tb1=pt.PrettyTable(["BC","EB","角A","角C","角D"])
	tb1.add_row([BC,EB,角A,角C,角D])
	print("*结果如下:")
	print(tb1)
def calculate2_2(gao,jiao2):
	jiao1=round(math.pi-math.radians(jiao2),4)
	BC=round((gao/math.sin(jiao1)),4)
	EB=round(gao/math.tan(jiao1),4)
	角B=角D=round(math.degrees(jiao1),2)
	角A=jiao2
	tb=pt.PrettyTable(["高EC","角C",])
	tb.add_row([gao,jiao2])
	print("*当数据如下时:")
	print(tb)
	tb1=pt.PrettyTable(["BC","EB","角A","角B","角D"])
	tb1.add_row([BC,EB,角A,角B,角D])
	print("*结果如下:")
	print(tb1)
def calculate3_1(long,short,jiao1):
	s=math.radians(jiao1)
	s=round(s,6)
	gao=round(short*(math.sin(s)),4)
	tou1=round(short*(math.cos(s)),7)
	tou2=long-tou1
	BD=round(math.sqrt(math.pow(tou2,2)+math.pow(gao,2)),4)
	newchang=long+tou1
	AC=round(math.sqrt(math.pow(newchang,2)+math.pow(gao,2)),4)
	tb=pt.PrettyTable(["长","宽", "角B"])
	tb.add_row([long,short,jiao1])
	print("*当数据如下时:")
	print(tb)
	角A=角C=round(math.degrees(math.pi-s),2)
	tb2=pt.PrettyTable(["AC", "BD", "CE","角D","角A","角C"])
	tb2.add_row([AC,BD,gao,jiao1,角A,角C])
	print("*结果如下:")
	print(tb2)
	#print("* BD: {0:.4f}".format(BD))
	#print("* AC: {0:.4f}".format(AC))
def calculate3_2(long,short,jiao2):
	s=round(math.pi-math.radians(jiao2),4)
	r=round(math.degrees(s),2)
	gao=round(short*(math.sin(s)),4)
	tou1=round(short*(math.cos(s)),7)
	tou2=long-tou1
	BD=round(math.sqrt(math.pow(tou2,2)+math.pow(gao,2)),4)
	newchang=long+tou1
	AC=round(math.sqrt(math.pow(newchang,2)+math.pow(gao,2)),4)
	tb=pt.PrettyTable(["长","宽", "角C"])
	tb.add_row([long,short,jiao2])
	print("*当数据如下时:")
	print(tb)
	角B=角D=round(math.degrees(math.pi-s),2)
	tb2=pt.PrettyTable(["AC", "BD", "CE","角D","角A","角B"])
	tb2.add_row([AC,BD,gao,角D,r,角B])
	print("*结果如下:")
	print(tb2)

if __name__ == '__main__':
	print( '''
     A           E     B
      ================
     /|          |  /
    / | by mars  | /
   /  |          |/
   ===============  
 D    F          C  

          ---PINGXINGSIBIANXING1.0  by mars    
          ---OPTION: ouput the value of AC and BD
''')
	main()

这里面用到了几个库函数。

  1. math库,使用math.sin() , math.cos() 等三脚函数和math.pow() 求一个数的幂次方。这里主要用到勾股定理。
  2. optparse库,用来解析输入的变量。
  3. prettytable用来输入输出表格。

上面的代码主要实现四个功能,知道高和一角求其他变量。知道两边和一夹角求其他。如果有需要可以自己加入其他函数求更多的情况。
试验结果如下:
在这里插入图片描述

发布了19 篇原创文章 · 获赞 15 · 访问量 4214

猜你喜欢

转载自blog.csdn.net/weixin_43952190/article/details/101155846