How to create a Triangle shaped drawing from my variables in Python -
i dove in world of programming , given basic exercise complete kind of stuck , not know next. problem : given 3 numbers determine if can form triangle , if yes calculate perimeter , area,draw triangle afterwards. have managed calculate perimeter , area of triangle(is such exists) have no idea how make computer draw triangle whatever values input.
here code:
import math = int(input("enter first number")) b = int(input("enter second number")) c = int(input("enter third number")) if a+b>c , a+c>b , b+c>a: print("the triangle's perimeter is:") print(int(a+b+c)) print("the area of triangle is:") print(int(math.sqrt((a+b+c)/2)*(((a+b+c)/2)-a)*(((a+b+c)/2)-b)*(((a+b+c)/2)-c))) else: print("the numbers not form triangle") input("press key continue")
would love if guys give me insight on how achieve task
here's solution, using tkinter:
from tkinter import * def draw(a, b, c): # determine corner points of triangle sides a, b, c = (0, 0) b = (c, 0) hc = (2 * (a**2*b**2 + b**2*c**2 + c**2*a**2) - (a**4 + b**4 + c**4))**0.5 / (2.*c) dx = (b**2 - hc**2)**0.5 if abs((c - dx)**2 + hc**2 - a**2) > 0.01: dx = -dx # dx has 2 solutions c = (dx, hc) # move away topleft, scale bit, convert int coords = [int((x + 1) * 75) x in a+b+c] # draw using tkinter root = tk() canvas = canvas(root, width=500, height=300) canvas.create_polygon(*coords) canvas.pack() root.mainloop() draw(2, 4, 5)
Comments
Post a Comment