ポリゴンのアウトラインの長さを返すポリゴンクラスのlength()メソッドを提供する必要があります。これは、各ポイントから次のポイントまでの距離を合計します最後の点から最初の点までの距離。例:3点ポリゴンポリ=ポリ(p1、p2、p3)、poly.length()は、p1からp2までの距離にp2からp3までの距離とp3からp1までの距離を戻す必要があります。 oopでlength()メソッドを設定する方法は?まだ各ポイントの合計長さを取得する方法を設定する方法
def dist(self, other):
dis_x = (other.x - self.x)*(other.x - self.x)
dis_y = (other.y - self.y)*(other.y - self.y)
dis_new = math.sqrt(dis_x + dis_y)
return dis_new
だけにどのように立ち往生:私はすでにDIST()メソッドは、指定された点までの2D距離をthatreturns定義さ
class Polygon:
def __init__(self, points=[]): # init with list of points
print("creating an instance of class", self.__class__.__name__)
self.point_list = points[:] # list to store a sequence of points
def draw(self):
turtle.penup()
for p in self.point_list:
p.draw()
turtle.pendown()
# go back to first point to close the polygon
self.point_list[0].draw()
def num_points(self):
return len(point_list)
ありがとう: はここに私のコードです各点からのアウトラインの合計長さを取得します。
ピタゴラスの定理を使って距離を求め、それらを足しますか?あなたのコードを見たり、ポリーゴンのポイントをどのように保管していなくても、これに答える方法はありません。 – kindall
こんにちは、私は元のコードを更新しました – Sophie