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
1)    Write a python programmecomparing for and while loops like the following. Math Times table For Loop: 2x1=2 2x2=4 ..... 2x12=24 While Loop: 3x1=3 3x

I have python game project which is due by next Tuesday. Do you think that I can get it on time if I order today?

Write a program that will take price data for stocks and print it graphically to the screen. Your program should begin by asking the user for the file name. It should then create a

Imagine a "particle" located on the centre square of a two-dimensional grid of dimensions 11 by 75. The particle can only move one square at a time, either up, down, left, or right

Non-local references So far, whenever we needed to compute a variable, there  was  a binding for that  variable in the 'local' environment (the environment in which  we were e

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 list

Tower of Hanoi game that you can let a player to move discs between the towers using a mouse. Moreover, you are required to do the followings: •Graphically represent any state in t

Every programming language has to have  some method for indicating grouping of operations. Here is how you execute an if-then-else structure in Java:   if (s == 1){ s = s

what are the issues assciated with top-down analysis

Finally! After years of adding unimportant sports, the IOC has finally added Shoe Tying to the Olympics. And you’ve been selected to write the software to handle the judging of t