Search code examples
python-3.xfunctionfor-loopmatharea

Herons formula in python3


Hello I am having trouble with my heron's formula and was wondering if you could take a look at it. I know I am a noob. However I appreciate your time looking at this code. I will also give you the prompt.

import math


#Part 1
def vert(k,n):
 x = math.cos(2*math.pi*k/n)
 y = math.sin(2*math.pi*k/n)
 return(x, y)

#Part 2
def vertices(n):
 vertic = []
 for k in range(n):
  vertic.append(vert(k,n))
 return(vertic)

## return a list of tuples


##Part 3
def dist(p1, p2):
 distance =  math.sqrt((p2[0]-p1[0])**2 + (p2[1]- p1[1])**2)
 return distance




#Part 4
def perimeter(vertic):
 perimeter = 0
 z = len(vertic)
 for i in range(z):
     w = (i + 1) % z
     currentdistance = dist(vertic[i], vertic[w])
     perimeter += currentdistance
 return perimeter



#Part 5
def heron(p1, p2, p3):
    side1 = dist(p1,p2)
    side2 = dist(p2,p3)
    side3 = dist(p3,p1)
    s = (side1+side2+side3)/2
    a = math.sqrt(s*(s-side1)*(s-side2)*(s-side3))
    return a

        #Part 6  
def area(vertic):
 calcarea = 0
 z = len(vertic)     
 p1 = v[0]
 p2 = v[1]
 p3 = v[2]
 a = dist(p1,p2)
 b = dist(p2,p3)
 c = dist(p3,p1)
 s = (a+b+c)/2
 z = len(v)
 for i in range(z):
   calcarea += heron(p1,p2,p3)
   calcarea = ((s*(s-a)*(s-b)*(s-c)) ** 0.5)   
 return calcarea

##Main program

ntest = 3
v = vertices(ntest)
n = 3
actualperimeter = perimeter(v)
actualarea = area(v)
for n in range(n,1004,100):
    r = (actualperimeter,actualarea)
    newval= (actualperimeter*actualperimeter)/(4.0*actualarea)
    print(n, actualperimeter, actualarea, newval)

The prompt is:::

write a function heron(p1, p2, p3) to find the area of the triangle with the given vertices. (Again p1, p2,and p3 are tuples representing (x,y) pairs)

write a function heron(p1, p2, p3) to find the area of the triangle with the given vertices. (Again p1, p2,and p3 are tuples representing (x,y) pairs)

SECOND TO LAST PART-write a function area(vertices(n)) which finds the area of a regular triangle given a list of its n vertices.

LAAST PART___write a main program that generates the vertices of regular polyons for n = 3, 103, 203, ... 1003 For each polygon, find and print n, perimeter, area, perimeter**2/(4.0*area)

I am just confused in regards to the output after you have more than a 3 sided polygon because we are suppose to obtain these results in our loop.

3 5.196152422706632 1.299038105676658 5.196152422706632
103 6.2822111395091325 3.139644590113289 3.1425672292376063
203 6.282934505126809 3.141091067504141 3.141843482675548
303 6.283072732486377 3.1413675078339525 3.1417052337290965
403 6.283121669116721 3.141465378624226 3.141656293393
503 6.283144457227955 3.1415109541646187 3.1416335042584804
603 6.283156882658976 3.1415358047799495 3.1416210784576846
703 6.283164394134183 3.141550827624372 3.1416135668230454
803 6.283169278529815 3.1415605963637927 3.141608682350027
903 6.28317263204446 3.1415673033654166 3.1416053287941073

my results are

3 5.196152422706632 1.299038105676658 5.196152422706632
103 5.196152422706632 1.299038105676658 5.196152422706632
203 5.196152422706632 1.299038105676658 5.196152422706632
303 5.196152422706632 1.299038105676658 5.196152422706632
403 5.196152422706632 1.299038105676658 5.196152422706632
503 5.196152422706632 1.299038105676658 5.196152422706632
603 5.196152422706632 1.299038105676658 5.196152422706632
703 5.196152422706632 1.299038105676658 5.196152422706632
803 5.196152422706632 1.299038105676658 5.196152422706632
903 5.196152422706632 1.299038105676658 5.196152422706632
1003 5.196152422706632 1.299038105676658 5.196152422706632

so I know there is in an issue within area or my herons.

Thank you for your time


Solution

  • You forgot to reclaculate vertices and values inside loop, so the initial vertice list was used ignoring n.

    Just change loop body:

    for n in range(3, 1004, 100):
        v = vertices(n)
        actualperimeter = perimeter(v)
        actualarea = area(v)
        r = (actualperimeter,actualarea)
        newval= (actualperimeter*actualperimeter)/(4.0*actualarea)
        print(n, actualperimeter, actualarea, newval)