'Perimeter function in python

I am currently coding a program that calculates the perimeter of a geometric figure with the following coordenates: (0.0, 0.0) (1.0, 5.0) (4.0, 8.0) (9.0, 4.0) (7.0, 1.0) The perimeter should be 26.4214. However, my program prints a lower number.

I know for a fact that the error is in the function perimetre but I don't know how to solve it. If anyone knows how to do it, their help would be appreciated.

Thanks a lot

def distancia_entre_dos_punts(x1,y1,x2,y2):
    difX=x1-x2
    difY=y1-y2
    distancia=math.sqrt(difX*difX+difY*difY)
    return distancia

def perimetre(pol, num_vertexs):
    perim= 0
    for i in range(0, num_vertexs-1):
        perim=perim+distancia_entre_dos_punts(pol[i,0],pol[i,1],pol[i+1,0],pol[i+1,1]) <----- Error?
        perim= perim+distancia_entre_dos_punts(pol[num_vertexs,0],pol[num_vertexs,1],pol[0,0],pol[0,1])<----- Error?
        return perim


Solution 1:[1]

I think there is something wrong in the indentation.

def perimetre(pol, num_vertexs):
    perim= 0
    for i in range(0, num_vertexs-1):
        perim=perim+distancia_entre_dos_punts(pol[i,0],pol[i,1],pol[i+1,0],pol[i+1,1]) <----- Error?
    perim= perim+distancia_entre_dos_punts(pol[num_vertexs,0],pol[num_vertexs,1],pol[0,0],pol[0,1])<----- Error?
    return perim

here the solution :

import numpy as np
import math


pol = np.array([[0.0, 0.0],[1.0, 5.0],[4.0, 8.0],[9.0, 4.0],[7.0, 1.0]])
print(pol)


def distancia_entre_dos_punts(x1,y1,x2,y2):
    difX=x1-x2
    difY=y1-y2
    distancia=math.sqrt(difX*difX+difY*difY)
    return distancia

def perimetre(pol):
    num_vertexs = pol.shape[0]
    perim= 0
    for i in range(0, num_vertexs-1):
        perim=perim+distancia_entre_dos_punts(pol[i,0],pol[i,1],pol[i+1,0],pol[i+1,1])
    perim= perim+distancia_entre_dos_punts(pol[num_vertexs-1,0],pol[num_vertexs-1,1],pol[0,0],pol[0,1])
    return perim

print(perimetre(pol))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1