Perimeter of a polygon, Python Programming

Assignment Help:

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)]

 


Related Discussions:- Perimeter of a polygon

Python style, Python  Style Software  engineering courses  often  pro...

Python  Style Software  engineering courses  often  provide very  rigid  guidelines on the style of programming, generally the appropriate value of indentation, or what  to us

File handling , A program to count how many files are on the file system wh...

A program to count how many files are on the file system which displays summary information regarding the total number of bytes used by all files and a breakdown of the number of b

Program to calculate area function, Rewrite the area.py program (shown bel...

Rewrite the area.py 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

Data wrangling, http://www.expertsmind.com/questions/data-wrangling-3011642...

http://www.expertsmind.com/questions/data-wrangling-301164244.aspx

Euler method, python program of motion of a particle in viscus medium

python program of motion of a particle in viscus medium

Example of Procedures as First-class objects, Procedures in Firrst-class ob...

Procedures in Firrst-class objects In Python, unlike  many  other  languages, methods are behave in much  the same way as num­ bers:  they  can be stored as values  of variabl

Work with functions, inputWithinRange (prompt, lowValue, highValue) This fu...

inputWithinRange (prompt, lowValue, highValue) This function will ask for a number using the prompt parameter. The function will only return a value that is within the inclusive ra

Lost, Import the sample code below into the Python IDLE and enhance it, run...

Import the sample code below into the Python IDLE and enhance it, run it and debug it. Add features to make this a more realistic database tool by providing for easy data entry an

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

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

#1 - Pseudo Code, Ask question #Minimum 100 worIn this project, create the ...

Ask question #Minimum 100 worIn this project, create the pseudo code from the following request. The program must have some sort of menu that allows users to make selections to

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd