0

想知道是否有人可以在最后一个字符串(面积,周长)中指出我的错误 - 任何帮助将不胜感激。)

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()
4

3 回答 3

0

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

首先,你没有调用你的perimeterandarea函数。您需要调用它们,并可能将点集合作为参数传递。(当我写这篇文章时,耶利米也很清楚地回答了这部分。)

第二个问题是我认为您对三角形面积的计算是错误的。您正在尝试应用公式area = base * height / 2,但一开始没有底数或高度,并且您没有正确计算它们。

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

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
于 2012-10-11T21:12:28.810 回答
0

There are a few problems.

The immediate problem is that you are trying to format the function "perimeter" and undefined variable "a". The "f" format specifier requires a float, so it's probably complaining about that.

The other big problem is that you are not calling the perimeter() and area() functions.

The next problem, which may not have shown up yet, because the functions aren't being called, is that the perimeter and area functions are using variables that are local to the main function.

You need to modify perimeter() and area() to take p1, p2, and p3 as parameters, similar to how distance() takes p1 and p2 as parameters. Then you need to update the code and the calls to format() so that the perimeter and area functions are actually called. It might look like this:

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))
于 2012-10-11T20:30:14.733 回答
0

感谢您的帮助耶利米:

作业代码

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()
于 2012-10-11T22:20:47.083 回答