Note that there are some explanatory texts on larger screens.

plurals
  1. POArea of a polygon (Recursively using Python)
    primarykey
    data
    text
    <p>I'm trying a solve an exercise from Exploring Python book. But, I guess I don't understand concept of the recursion. I've written some Recursively function. Therefore I know some of the aspects. But, I don't have enough experience. And I've stopped to study programming about one year.</p> <p>Anyway, let me give you the full question:</p> <blockquote> <p>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 compute the area of a polygon. This can be accomplished by “cutting off” a triangle, using the fact that a triangle with corners (x1, y1), (x2, y2), (x3, y3) has area (x1y1 + x2y2 + x3y2 – y1x2 –y2x3 – y3x1) / 2.</p> </blockquote> <p>Despite the fact that, the question already gave the formula, I used another formula. Because, I made some research about area of a polygon. And if you look at <a href="http://mathworld.wolfram.com/PolygonArea.html" rel="nofollow">here</a> the formula is different.</p> <p>And describing my program step by step would be better, in order to explain what I want. OK, I had to declare global scopes, because of recursion:</p> <pre><code>area = 0 x = [0] * 3 y = [0] * 3 </code></pre> <p>And then, I created a recursively function. Zero is always returned by this function as a result. So my real problem is this:</p> <pre><code>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 </code></pre> <p>And my main function:</p> <pre><code> def main(): mypolygon = [(1,2), (2,5), (1,4)] # I declared polygon as tuples # I called my function and started to count from zero, and the result will be prompted. print(areaofpolygon(mypolygon,0)) return 0 if __name__ == '__main__': main() </code></pre> <p>And here is my full code without comments:</p> <pre><code>''' 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() </code></pre> <p><strong>EDIT One</strong></p> <p>After reading your answers, I've understood what was wrong with my code. So I decided to share last version of my program in order to get some other helps. Again, I had to declare global variables. How can I apply ( lop_triangle ) function from senderle</p> <pre><code>area = 0 x = [0] * 3 y = [0] * 3 </code></pre> <p>My function that divides tuple and to get x and y coordinates.</p> <pre><code>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 </code></pre> <p>My function calculate area of polygon( Same as before ) </p> <pre><code>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 </code></pre> <p>My main function...</p> <pre><code>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() </code></pre> <p>Please help me to improve my code without giving full solution.</p> <p><strong>EDIT Two</strong></p> <p>After making discussion with senderle, I understood where is the problem and senderle's solution is better than mine so I suggest that you should use it. Anyway, He helped me to make my code correct.And I had to change my formula again.</p> <pre><code>area += x[i]*y[(i+1) % 3] - x[(i+1) % 3]*y[i] </code></pre> <p>He also added for longer polygons 3 must be len(vertices). Thanks everyone for their time.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload