Yavaar Nosimohomed
Yavaar Nosimohomed

Reputation: 167

Previous and next elements in a loop

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?

Upvotes: 3

Views: 117

Answers (4)

Imtiaz Raqib
Imtiaz Raqib

Reputation: 375

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

Upvotes: 0

Matiu Carr
Matiu Carr

Reputation: 298

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

Upvotes: 0

jamylak
jamylak

Reputation: 133764

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)

Upvotes: 3

Stewart Smith
Stewart Smith

Reputation: 1416

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.

Upvotes: 0

Related Questions