Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a Triangle shaped drawing from my variables in Python

Tags:

python

I just recently dove in to the world of programming and was given a very basic exercise to complete but I am kind of stuck and do not know what to do next. The problem was : Given 3 numbers determine if they can form a triangle and if yes calculate the Perimeter and Area,draw the Triangle afterwards. I have managed to calculate the perimeter and area of the triangle(is such exists) but have no idea how to make the computer draw a triangle from whatever values were input.

Here is the code:

import math
a = int(input("Enter your first number"))
b = int(input("Enter your second number"))
c = int(input("Enter your third number"))
if a+b>c and a+c>b and b+c>a:
    print("The Triangle's Perimeter is:")
    print(int(a+b+c))
    print("The Area of the 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 do not form a triangle")
input("Press any key to continue")

Would love if you guys could give me an insight on how to achieve this task

like image 993
Adrian Syn Rusnac Avatar asked Oct 07 '13 12:10

Adrian Syn Rusnac


People also ask

How do you draw a triangle in tkinter?

You can use polygon -function to draw a triangle. Just specify a list of corner-points of your triangle as argument. Check out the tkinter-documentation for more info.


2 Answers

Here's another solution, using Tkinter:

from Tkinter import *

def draw(a, b, c):
    # determine corner points of triangle with sides a, b, c
    A = (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 two solutions
    C = (dx, hc)

    # move away from topleft, scale up a bit, convert to int
    coords = [int((x + 1) * 75) for 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)

enter image description here

like image 197
tobias_k Avatar answered Sep 29 '22 00:09

tobias_k


from turtle import color, begin_fill, forward, left, end_fill, done
from math import acos, degrees

def triangle_exists(a, b, c):
    """Return True iff there exists a triangle with sides a, b, c."""
    return a + b > c and b + c > a and c + a > b

def triangle_angle(a, b, c):
    """Return the angle (in degrees) opposite the side of length a in the
    triangle with sides a, b, c."""
    # See http://en.wikipedia.org/wiki/Law_of_cosines
    return degrees(acos((b ** 2 + c ** 2 - a ** 2) / (2.0 * b * c)))

def draw_triangle(a, b, c):
    """Draw a triangle with sides of lengths a, b, and c."""
    assert(triangle_exists(a, b, c))
    color('black', 'yellow')
    begin_fill()
    forward(c)
    left(180 - triangle_angle(b, c, a))
    forward(a)
    left(180 - triangle_angle(c, a, b))
    forward(b)
    end_fill()
    done()

>>> draw_triangle(400, 350, 200)

enter image description here

like image 44
Gareth Rees Avatar answered Sep 28 '22 23:09

Gareth Rees