我试图用python中的Matoplotlib绘制一个三角形,其边长是从输入中给定的。下面您将看到我的代码,我如何使用变量在Matplotlib中绘制一个三角形?在
import numpy as np
import turtle
from turtle import *
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import matplotlib.patches as patches
def triangle_angle(t, v, w):
"""Diese Funktion ergibt den Eck zwischen v und w"""
cosT=np.divide((v**2+w**2-t**2),2*v*w)
alfaT=np.arccos(cosT)
alfaT=np.degrees(alfaT)
return alfaT
def triangle(a,b,c):
l=[]
if (a+b>c) and (a+c>b) and (b+c>a) : #
l.append(True)
l.append(np.sum([a,b,c]))
p = float(np.divide(l[1], 2)) #Berechnet die haelfte des Umfangs
l.append(np.sqrt(np.prod([p, (p - a), (p - b), (p - c)]))) #Berechnet die Flaeche mittles Heron Formula
else:
l.append(False) #Gibt dem ersten Element den Wert False
l.append("Diese Zahlen koennen kein Dreieck konstruieren, Das Umfang koennte nicht berechnet sein")
l.append("Die Flaeche koennte nicht berechnet sein") # Die Flaeche und Umfang koennte nicht berechnet werden
return(l)
print("Dieses Programm prueft ob es mit 3 gegebene Laengen ein Dreickes gibt, berechnet \
die Flaeche und das Umfang des Dreiecks und es zeichnet")
x=float(raw_input("geben Sie die erste Zahl ein "))
y= float(raw_input("geben Sie die erste Zahl ein "))
z= float(raw_input("geben Sie die erste Zahl ein "))
if (x<=0) or (y<=0) or (z<=0):
print("Eine oder mehrere Zahlen sind nicht positiv, bitte geben Sie die !Positive! Zahlen noch einmal. ")
x=float(raw_input("geben Sie die erste Zahl ein "))
y= float(raw_input("geben Sie die erste Zahl ein "))
z= float(raw_input("geben Sie die erste Zahl ein "))
if triangle(x,y,z)[0] == True:
print("So ein Dreiecks gibt es. Das Umfang ist "+str(triangle(x,y,z)[1])+" und die Flaeche ist "+str(triangle(x,y,z)[2]))
else:
print("So ein Dreiecks gibt es nicht")
else:
if triangle(x,y,z)[0] == True:
print("So ein Dreiecks gibt es. Das Umfang ist "+str(triangle(x,y,z)[1])+" und die Flaeche ist "+str(triangle(x,y,z)[2]))
else:
print("So ein Dreiecks gibt es nicht")
fig1 = plt.figure()
ax1 = fig1.add_subplot(111, aspect='equal')
ax1.add_patch(patches.RegularPolygon((0.5, 0.5),3,0.2,))
plt.show()
如有任何帮助,将不胜感激,谢谢。在
等边三角形可以用
RegularPolygon
在matplotlib中绘制。但是,如果三角形的边可能不同,这不是一个选项。在因此,需要将三角形绘制为一般
Polygon
。因此需要边缘点的坐标。在为了简单起见,我们可以选择沿x轴定位最长的边(}。剩下的就是计算三角形顶点的坐标。在
c
),这决定了三角形的前两点为(0,0)
和{相关问题 更多 >
编程相关推荐