Python 3 - graphicalTriangle - 字符串格式/函数

时间:2012-10-11 16:33:55

标签: python python-3.x

想知道是否有人可以在最后一个字符串(区域,周边)中指出我的错误 - 任何帮助都将非常感激。 )

message = Text(Point(5, 0.5),"The perimeter is: {0:0.2f}".format(perimeter))
message.draw(win)
message2 = Text(Point(5, 1),"The area is: {0:0.2f}".format(a))
message2.draw(win)

####完整代码#####

# Program: triangle.py
import math
from graphics import *

def square(x):
    return x * x

def distance(p1, p2):
    dist = math.sqrt(square(p2.getX() - p1.getX()) + square(p2.getY() - p1.getY()))
    return dist

def perimeter():
    # Calculate the perimeter of the triangle
    perimeter = distance(p1,p2) + distance(p2,p3) + distance(p3,p1)
    return perimeter

def area():
    # Calculate the area of the triangle
    base = (distance(p3, p1) * (1 / 2))
    height = ((base) ** 2) - (distance(p1, p2) ** 2)
    h = math.sqrt(square(height))
    a = ((1/2) * (base) * h)
    return a

def main():
    win = GraphWin("Draw a Triangle")
    win.setCoords(0.0, 0.0, 10.0, 10.0)
    message = Text(Point(5, 0.5), "Click on three points")
    message.draw(win)

    # Get and draw three vertices of triangle
    p1 = win.getMouse()
    p1.draw(win)
    p2 = win.getMouse()
    p2.draw(win)
    p3 = win.getMouse()
    p3.draw(win)

    # Use Polygon object to draw the triangle
    triangle = Polygon(p1,p2,p3)
    triangle.setFill("black")
    triangle.setOutline("blue")
    triangle.draw(win)


    message = Text(Point(5, 0.5),"The perimeter is: {0:0.2f}".format(perimeter))
    message.draw(win)
    message2 = Text(Point(5, 1),"The area is: {0:0.2f}".format(a))
    message2.draw(win)

    # Wait for another click to exit
    win.getMouse()
    win.close()

main()

3 个答案:

答案 0 :(得分:0)

有一些问题。

当前的问题是您正在尝试格式化“周长”功能和未定义变量“a”。 “f”格式说明符需要浮点数,所以它可能会抱怨。

另一个大问题是你没有调用perimeter()和area()函数。

由于函数未被​​调用,可能尚未显示的下一个问题是,周边和区域函数正在使用主函数本地的变量。

您需要修改周边()和区域()以将p1,p2和p3作为参数,类似于distance()将p1和p2作为参数的方式。然后,您需要更新代码和对format()的调用,以便实际调用外围和区域函数。它可能看起来像这样:

p = perimeter(p1,p2,p3)
a = area(p1,p2,p3)
message = Text(Point(5, 0.5),"The perimeter is: {0:0.2f}".format(p))
message.draw(win)
message2 = Text(Point(5, 1),"The area is: {0:0.2f}".format(a))

答案 1 :(得分:0)

如果我正确理解了问题,那么您无法获得面积计算的正确结果。我看到了两个问题。

首先,您没有呼叫perimeterarea功能。你需要调用它们,并可能将点集合作为参数传递。 (在我写这篇文章时,Jeremiah也非常清楚地回答了这一部分。)

第二个问题是我相信你对三角区域的计算是错误的。您正在尝试应用公式area = base * height / 2,但您没有开头的基数或高度,而且您没有正确计算它们。

虽然可以修复基础和高度计算,但我建议使用不同的(但在数学上等效的)公式,以便更好地匹配您拥有的数据。这是一种方法,使用我找到的公式here

def area(p0, p1, p2):                      # This code assumes points are tuples
    return abs((p0[0]*(p1[1]-p2[1]) +      # but the formula can work with any
                p1[0]*(p2[1]-p0[1]) +      # sort of point, just replace the
                p2[0]*(p0[1]-p1[1])) / 2)  # indexing with accessor calls.

另一种选择,因为你也在计算周长,就是使用Heron's formula。为了有效地执行此操作,您需要在同一函数中找到周边和区域,因此您只能计算边的长度一次:

def perimeter_and_area(p0, p1, p2):
    a = distance(p0, p1)
    b = distance(p1, p2)
    c = distance(p2, p0)

    perimeter = a+b+c

    s = perimeter / 2   # semiperimeter
    area = math.sqrt(s * (s-a) * (s-b) * (s-c))

    return perimeter, area

答案 2 :(得分:0)

感谢你帮助Jeremiah:

作业代码

import math

from graphics import *

def square(x):
    return x * x

def distance(p1, p2):
    # calculate distance between two points
    dist = math.sqrt(square(p2.getX() - p1.getX()) + square(p2.getY() - p1.getY()))
    return dist

def perimeter(tri):
    # Calculate the perimeter of the triangle
    points = tri.getPoints()
    p1 = points[0]
    p2 = points[1]
    p3 = points[2]
    perim = distance(p1,p2) + distance(p2,p3) + distance(p3,p1)
    return perim 

 def area(tri):
    # Calculate the area of the triangle
    points = tri.getPoints()
    p1 = points[0]
    p2 = points[1]
    p3 = points[2]
    base = (distance(p3, p1) * (1 / 2))
    height = ((base) ** 2) - (distance(p1, p2) ** 2)
    h = math.sqrt(square(height))
    a = (1/2) * ((base) * (h))
    return a


def main():
    # Setup graphWin
    win = GraphWin("Draw a Triangle")
    win.setCoords(0.0, 0.0, 10.0, 10.0)
    message = Text(Point(5, 0.5), "Click on three points")
    message.draw(win)

    # Get and draw three vertices of triangle
    p1 = win.getMouse()
    p1.draw(win)
    p2 = win.getMouse()
    p2.draw(win)
    p3 = win.getMouse()
    p3.draw(win)

    # Use Polygon object to draw the triangle
    triangle = Polygon(p1,p2,p3)
    triangle.setFill("black")
    triangle.setOutline("blue")
    triangle.draw(win)
    p = perimeter(triangle)
    a = area(triangle)

    # write text to graphWin
    message2 = Text(Point(5, 1),"The perimeter is: {0:0.2f}".format(p))
    message2.draw(win)
    message3 = Text(Point(5, 2),"The area is: {0:0.2f}".format(a))
    message3.draw(win)
    message.setText("Click again to close")

    # Wait for another click to exit
    win.getMouse()
    win.close()

main()