Perimeter of a polygon, Python Programming

Perimeter of a polygon

Now, let's consider the problem of computing the length  of the perimeter of a polygon. The input is a structure of vertices,  encoded as a list of lists of two  values (as like[[1, 2], [3.4, 7.6], [-4.4, 3]]). Here is the ?rst attempt:


def perim(vertices):

result = 0

for i in range(len(vertices)-1):

result = result + math.sqrt((vertices[i][0]-vertices[i+1][0])**2 + \ (vertices[i][1]-vertices[i+1][1])**2)

return result + math.sqrt((vertices[-1][0]-vertices[0][0])**2 + \ (vertices[-1][1]-vertices[0][1])**2)


Again, this works,  but it ain't correct. The basic cause is that someone reading the code doesn't immediately see what  all that  subtraction and  squaring is about.    We caould be fixed that  by de?ning another procedure:


def perim(vertices):

result = 0

for i in range(len(vertices)-1):

result = result + pointDist(vertices[i],vertices[i+1])

return result + pointDist(vertices[-1],vertices[0])

def pointDist(p1,p2):

return math.sqrt(sum([(p1[i] - p2[i])**2 for i in range(len(p1))]))


Now,  we've  de?ned a new  procedure pointDist, which  calculates the  Euclidean distance be­ tween  two positions.    And,  in fact,  we've  written it usually enough to work  on  points  of any dimension (not just two).  Just for fun, here's another example  to calculate the distance, which  some people would use  and others  would not.


def pointDist(p1,p2):

return math.sqrt(sum([(c1 - c2)**2 for (c1, c2) in zip(p1, p2)]))

For this to make sense, you have to understand zip. Here's a program of how it works:

> zip([1, 2, 3],[4, 5, 6])

[(1, 4), (2, 5), (3, 6)]


Posted Date: 8/9/2012 7:24:15 AM | Location : United States

Related Discussions:- Perimeter of a polygon, Assignment Help, Ask Question on Perimeter of a polygon, Get Answer, Expert's Help, Perimeter of a polygon Discussions

Write discussion on Perimeter of a polygon
Your posts are moderated
Related Questions
Print vs Return Here are two different method declarations: def f1(x): print x + 1 def f2(x): return x + 1 What happens when  we call them? >>> f1(3) 4 >>

Your task is to construct a translator from the subset of Python in Project 3 intocontinuation-passing style (CPS). Of course, the expected way of accomplishing this is to trans

Write an algorithm for the sum of the given series 1,-1/2,1/4,-1/8.....

Assume you have a truck which has to travel across a desert from the base camp at position 0 (left) to the target camp at position 4 (right). The intermediate positions 1,2, and 3

Bank transfer What  if we  have  two  values,  representing bank  accounts, and  need  to transfer an  amount of money  amt between them?  Consider that a bank account is show

You should use for whenever you can, because  it creates  the structure of your  loops clear. Sometimes, however, you require to do an operation various times, but you don't want t

When investing money, an important concept to know is compound interest. The equation FV = PV (1+rate)periods . This relates the following four quantities. The present value (

1 Low Level Exploits 1.1 Savegames Jimmy is becoming increasingly frustrated at the computer game hes playing. He has a save right before the levels boss but he needs either more

There are three seating categories at a stadium. For a softball game, Class A seats cost $15, Class B seats cost $12, and Class C seats cost $9. Write a program that asks how many

Rewrite the program (shown below, or in the Creating Functions section of the tutorial) so that it has separate functions for the perimeter and area of a square, a rectangl