5

我最近才涉足编程世界,并完成了一个非常基本的练习,但我有点卡住了,不知道下一步该做什么。问题是:给定 3 个数字,确定它们是否可以形成三角形,如果可以,计算周长和面积,然后绘制三角形。我已经设法计算出三角形的周长和面积(是否存在),但不知道如何让计算机从输入的任何值中绘制一个三角形。

这是代码:

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")

如果你们能告诉我如何完成这项任务,我会很高兴

4

3 回答 3

7

这是另一个使用 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)

在此处输入图像描述

于 2013-10-07T13:44:12.030 回答
4
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)

在此处输入图像描述

于 2013-10-07T13:31:08.477 回答
-1

If you are plotting by using the (x,y) coordinates of the vertices, you have at least three continuous degrees of freedom: 2 for choosing the location of vertex A, and one more for the direction from A to B. Then, there is a binary choice of clockwise or counter-clockwise order for the labeling of the vertices.

If you don't care which of these to use, then you can put A at (0,0), B at (0, c), and then solve for the intersection of two circles: radius a centered at B, and radius b centered as A.

x² + y² = b² 
(x - c)² + y² = a² .... subtract these to eliminate y² 
(x - c)² - x² = a² - b²
-2cx + c² = a² - b²
2cx = c² + b² - a²
x = (c² + b² - a²)/(2c)
y = ± √[ b² - x² ] .... choose - for clockwise, + for counter-clockwise

Now you have three points A=(0,0), B=(0,c) and C=(x,y) with the desired opposite side lengths a,b,c respectively. If you need the angles, use the Law of Cosines from trig:

c² = a² + b² - 2ab(cos C)
2ab(cos C) = a² + b² - c²
C = cos⁻¹[ (a² + b² - c²)/(2ab) ]

The same pattern applies to solving for A and B. Those are the interior angles. For turtles, the turning angle is exterior angle, so start at point A, move (c) in any direction, turn (180-B), then move (a), then turn (180-B), then move (b).

于 2013-10-07T13:49:32.283 回答