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

エレミヤの援助に感謝します:

割り当てのための作業コード

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 に答える
0

If I understand the question correctly, you're not getting the correct results for the area calculation. There are two issues I see.

First, you're not calling your perimeter and area functions. You need to call them, and probably pass the collection of points in as parameters. (While I was writing this, Jeremiah answered this part too, quite clearly.)

The second issue is that I believe your calculation for the area of the triangle is wrong. You're trying to apply the formula area = base * height / 2 but you don't have the base or height at the start and you're not calculating them correctly.

While it is possible to fix your base and height computations, instead I suggest is using a different (but mathematically equivalent) formula which better matches the data you have. Here's one way to do that, using the formula I found 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.

Another option, since you're also calculating the perimeter, is to use Heron's formula. To do this efficiently you'll want to find both the perimeter and area in the same function, so you can compute the lengths of the sides only once:

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

いくつかの問題があります。

当面の問題は、関数 "perimeter" と未定義の変数 "a" をフォーマットしようとしていることです。「f」フォーマット指定子には浮動小数点が必要なので、おそらくそれについて不平を言っています。

もう 1 つの大きな問題は、perimeter() および area() 関数を呼び出していないことです。

次の問題は、関数が呼び出されていないため、まだ表示されていない可能性がありますが、境界関数と面積関数がメイン関数に対してローカルな変数を使用していることです。

distance() が p1 と p2 をパラメーターとして受け取るのと同様に、perimeter() と area() を変更して、p1、p2、および p3 をパラメーターとして受け取る必要があります。次に、周囲関数と面積関数が実際に呼び出されるように、コードと 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))
于 2012-10-11T20:30:14.733 に答える