解平行四邊形

解平行四邊形之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用來輸入輸出表格。

上面的代碼主要實現四個功能,知道高和一角求其他變量。知道兩邊和一夾角求其他。如果有需要可以自己加入其他函數求更多的情況。
試驗結果如下:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章