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

assignment 10, #question.Program 10 Assignment (Banking Objects – Savings/...

#question.Program 10 Assignment (Banking Objects – Savings/Checking Accounts) Create a program named 10.py that performs the following: Create a class named ChkAcct, and a class n

Data wrangling, Ask quesICT702 TASK 2 1 2018 Semester 2 Data Wrangling Due ...

Ask quesICT702 TASK 2 1 2018 Semester 2 Data Wrangling Due in two parts - Friday of Week 9 and Week 12 Below Zero - ice cream store The local ice-cream store needs a new ordering s

Question, Data array A has data series from 1,000,000 to 1 with step size 1...

Data array A has data series from 1,000,000 to 1 with step size 1, which is in perfect decreasing order. Data array B has data series from 1 to 1,000,000, which is in random order.

Python programme comparing for and while loops, 1)    Write a python progra...

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

Expert, how can i become an expert & solution provider in the CS/IT field?

how can i become an expert & solution provider in the CS/IT field?

Variables, Variables We cannot  go very far without variables. A variabl...

Variables We cannot  go very far without variables. A variable is a value related to a name that we can bind  to have a particular value  and  then  later use in an expression.

Program in c to generate random input - python script, For this assignment,...

For this assignment, you are to: 1.    Modify the output format of your Python script from Assignment 1 to match the input format of your C program from Assignment 2. 2.    W

Variable, from urllib2 import urlopen # Open http://placekitten.com/ for...

from urllib2 import urlopen # Open http://placekitten.com/ for reading on line 4! response = kittens.read() body = response[559:1000] # Add your ''print'' statement here!

Conditionals-for and while, For and While   If we want  to do some o...

For and While   If we want  to do some operation or set of operations various  times, we can handle the process  in several  different types.  The most straightforward types

Create a program to produce a business speak phrase, The goal of this exerc...

The goal of this exercise is to write a \business phrase" generator, which each time it is called produces lines of business speak, such as: It's time that we became uber-effici

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