3

I have a list of points as such:

a = [(2,4),(34,53),(34,2),(84,64)]

These points are listed in clockwise order, and they form a Polygon. I need to find the perimeter of the polygon, which would be the sum of the distance between adjacent points. I would have to use the distance formula to find the distance, so it would have to be the distance between (2,4) and (34,53), and then the distance between (34,53) and (34,2) and so on.

How would I write this for loop that uses the distance formula for using the points of the 1st element and the 2nd element, and then when moves on to the 2nd and 3rd element and so on, and finally when its at the last element, it uses the distance formula on the last element and the first element.. sort of like a word-wrap?

4 Answers 4

3

A more simple for loop is just:

>>> for i in range(len(a)):
        print a[i-1], a[i]


(84, 64) (2, 4)
(2, 4) (34, 53)
(34, 53) (34, 2)
(34, 2) (84, 64)
Sign up to request clarification or add additional context in comments.

Comments

0

Defining a polygon perimeter function

import math
def poly_peri():

a = [(2,4),(34,53),(34,2),(84,64)]
# Intializing perimeter to be 0
perimeter = 0

# MAIN LOOP
#[len(a) - 1] because we dont want to continue after the last number
for i in range(len(a) - 1):
    distance = ((a[i + 1][0] - a[i][0])**2 + (a[i+1][1] - a[i][1])**2)

    perimeter += math.sqrt(distance)

return perimeter

You can also use the list of points of your polygon as an argument for the function poly_peri(a). Meaning, when you call the function poly_peri(), you would call it as poly_peri([(2,4),(34,53),(34,2),(84,64)]). This would output the same result for whatever you put as an argument.The code would look like

import math
def poly_peri(a):

points = a
# Intializing perimeter to be 0
perimeter = 0

# MAIN LOOP
#[len(points) - 1] because we dont want to continue after the last number
a.append(a[0])
for i in range(len(points) - 1):
    distance = ((points[i + 1][0] - points[i][0])**2 + (points[i+1][1] - points[i][1])**2)

    perimeter += math.sqrt(distance)

return perimeter

3 Comments

Your logic is not finding distance between (84,64) and (2,4)
you need to add a.append(a[0]) before you start loop
@Ravichandra Thank you for letting me know!
0

Use modulus division to calculate your indices. For example, if you have a list of points of length n, and j is your index, then you can calculate a wrapping index with j = (j+1)%n.

Comments

0

Accounting for "negative" distances

import math
perimeter = 0
a = [(2,4),(34,53),(34,2),(84,64)]

for i in range(len(a)):
    k = i+1 if (i<len(a)-1) else 0
    x,y = a[i]
    x2,y2 = a[k]
    edge = math.fabs(math.sqrt(math.pow(y2-y, 2) + math.pow(x2-x,2)))
    print("""Edge {}: {}""".format(i,edge))
    perimeter += edge


# when run
Edge 0: 58.52349955359813
Edge 1: 51.0
Edge 2: 79.64923100695951
Edge 3: 101.6070863670443
>>> print("""Perimeter: {}""".format(perimeter))
Perimeter: 290.7798169276019

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.