I am trying to solve an exercise from Learning a Python Book. But, I think, I do not understand the concept of recursion. I wrote a function Recursively. Therefore, I know some aspects. But I donβt have enough experience. And I stopped learning programming for about a year.
Anyway, let me ask you the full question:
A polygon can be represented by a list of (x, y) pairs, where each pair is a tuple: [(x1, y1), (x2, y2), (x3, y3), ... (xn, yn)]. Write a recursive function to calculate the area of ββthe polygon. This can be achieved by trimming the triangle using the fact that the triangle with angles (x1, y1), (x2, y2), (x3, y3) has an area of ββ(x1y1 + x2y2 + x3y2 - y1x2 -y2x3 - y3x1) / 2.
Despite the fact that the question has already given the formula, I used a different formula. Because I did some research on the polygon area. And if you look at here , then a different formula.
And to describe my program step by step would be better to explain what I want. Ok, I had to declare global regions, due to recursion:
area = 0 x = [0] * 3 y = [0] * 3
And then I created a function recursively. As a result, this function always returns to zero. So my real problem is this:
def areaofpolygon(polygon, i): global area, x, y # My variables try: # I prefered using try statement from using if-else statements. So it is the easier I guess. x[i], y[i] = polygon[i] # X and Y coordinates from tuple area += (x[i]*y[i+1] - x[i+1]*y[i]) #My formula except IndexError: return area/2 areaofpolygon(polygon, i+1) # Here, this is my weird recursion
And my main function:
def main(): mypolygon = [(1,2), (2,5), (1,4)]
And here is my complete code without comment:
''' Created on Feb 24, 2012 @author: msarialp ''' area = 0 x = [0] * 3 y = [0] * 3 def areaofpolygon(polygon, i): global area, x, y try: x[i], y[i] = polygon[i] area += (x[i]*y[i+1] - x[i+1]*y[i]) except IndexError: return area/2 areaofpolygon(polygon, i+1) def main(): mypolygon = [(1,2), (2,5), (1,4)] print(areaofpolygon(mypolygon,0)) return 0 if __name__ == '__main__': main()
EDIT One
After reading your answers, I realized what was wrong with my code. So I decided to share the latest version of my program to get some other tips. Again, I had to declare global variables. How can I apply the function (lop_triangle) from senderle
area = 0 x = [0] * 3 y = [0] * 3
My function, which divides the tuple and gets the x and y coordinates.
def sides_of_polygon(polygon, i): global x, y try: x[i], y[i] = polygon[i] return sides_of_polygon(polygon, i+1) except IndexError: return x, y
My function calculates the polygon area (as before)
def area_of_polygon(x, y, i): global area try: area += x[i]*y[i+1] - x[i+1]*y[i] return area_of_polygon(x, y, i+1) except IndexError: return area/2.0
My main function ...
def main(): mypolygon = [(1,2), (2,5), (1,4)] dx, dy = sides_of_polygon(mypolygon, 0) print(area_of_polygon(dx,dy,0)) return 0 if __name__ == '__main__': main()
Please help me improve my code without giving a complete solution.
EDIT 2
After discussing with senderle, I realized where the problem is, and the senderle solution is better than mine, so I suggest you use it. Anyway, He helped me make the right code. And I had to change the formula again.
area += x[i]*y[(i+1) % 3] - x[(i+1) % 3]*y[i]
He also added for longer polygons 3 should be len (vertices). Thanks to everyone for their time.