Calculate Polygon Area

  • Well this is kinda different...


    Honestly, I don't remember the last time I actually posted a question.


    Perhaps consider it an expert-level challenge...


    I've been working for awhile now on vector-based geometry/trig and so forth. I'm very interested now in developing a solution to the following:


    Given pixel coordinates for an n-sided polygon (concave or convex) calculate the pixel area of the polygon.


    My problem is, I don't have time with my reg'lar job/duties to investigate this and find the solution quicly. I was hoping perhaps some of you smarter-than-the-average bunch may already know the answer and could bring it to me a little quicker.


    If no one should respond, I'll let you know when (or should I say "if") I figure it out... but unfortunately, it might be a few weeks before I can get to it.


    Point-in-Poly would be the next challenge, but let's just stick to area for now...

  • Re: Area Of A Polygon


    Aaron - I have posted the solution to finding the area of polygon here on OzGrid.... I had asked to find the angles created between the lines of a polygon and Andy Pope did this bit.... I followed up with a function to calculate the area of it.. (it doesnt matter how many points are on the polygon).... let me check to see if I can find the post....


    Ger

    _______________________________________________
    There are 10 types of people in the world. Those that understand Binary and those that dont. :P


    Why are Halloween and Christmas the same? Because Oct 31 = Dec 25... ;)

    _______________________________________________

  • Re: Area Of A Polygon


    Here you go Aaron...


    http://www.ozgrid.com/forum/showthread.php?t=48347


    Sorry - I called it a Freeform shape... but its the same difference...


    Ger

    _______________________________________________
    There are 10 types of people in the world. Those that understand Binary and those that dont. :P


    Why are Halloween and Christmas the same? Because Oct 31 = Dec 25... ;)

    _______________________________________________

  • Re: Area Of A Polygon


    Also, Take a look at Andy Popes web site for "point in an area"...


    http://www.andypope.info/fun/pointinarea.htm


    The area can be a polygon.


    To be fair, Andy Rocks! : D


    Ger

    _______________________________________________
    There are 10 types of people in the world. Those that understand Binary and those that dont. :P


    Why are Halloween and Christmas the same? Because Oct 31 = Dec 25... ;)

    _______________________________________________

  • Re: Area Of A Polygon


    Cool... I'll have to take a look at that.


    Does your formula handle both convex and concave polys?



    Looks like Andy's got the point-in-poly solution too...
    This should save me several days research time.

  • Re: Area Of A Polygon


    Yes - If I'm not mistaken, if the area is positive it is convex, and if the area is negative, it is concave.... but I could not swear to that.


    Ger

    _______________________________________________
    There are 10 types of people in the world. Those that understand Binary and those that dont. :P


    Why are Halloween and Christmas the same? Because Oct 31 = Dec 25... ;)

    _______________________________________________

  • Re: Area Of A Polygon


    And to add to that... I wasnt worried whether it was convex or not... so I ABS'ed the result in this line of code to ensure all areas were positive.... which you could remove.


    Code
    ActiveSheet.Cells(.Nodes.Count + 3, 1) = "Area = " & Abs(myArea) & " Units square"


    God, I hope it works : D I didnt do too much testing on it as it was my own pet project and it kinda fell through the cracks. The foundation should be right though. Remember, I only did the Area part... Andy did the hard work with Angles and lengths of the lines.


    Ger

    _______________________________________________
    There are 10 types of people in the world. Those that understand Binary and those that dont. :P


    Why are Halloween and Christmas the same? Because Oct 31 = Dec 25... ;)

    _______________________________________________

  • Re: Area Of A Polygon



    Some of that sounds a little fishy... off hand, not sure why a concave polygon would yield negative area, but I'll look at it.


    In any event, should at least give me a good starting point for the polygon tools I'd like to build.

  • Re: Area Of A Polygon


    I reviewed the workbook you referenced and my initial testing shows your area calculations are slightly off... but don't get too excited... yet.


    Even for caclulating simple polygons (a square) it's off by an unfortunately unacceptable margin (ie: a 5000sq area was out by 5-10 units) but I suspect that may have something to do with display precision vs. internal VBA calcs. You show area calculated to 3 dec points accuracy and the display precision for Andy's logic is single decimal, so maybe it's just your calc is using the more precise length values inside VBA? I haven't reviewed the code yet.


    My first crude tests for concave area are also encouraging... so it seems like whatever you did it's working in a generally correct fashion. The area calcs appear slightly off again, but not too bad. Again probably more an issue with my precision on creating the shapes vs the formula.


    I reviewed the referenced math link in your other thread and the +/- area issue had to do with the points of a convex polygon being arranged clockwise or counter-clockwise, that made more sense. The definition on that page for the formula only specifically excludes self-intersecting polygons so I assume it works for either convex or concave... just odd that they footnote the +/- issue as applying specifically to "convex" polys. Wonder if it's true for concave as well (I'm not a math major), or maybe the concave polys can be positive or negative and your method of always taking the absolute covers it?

  • Re: Calculate Polygon Area


    Thanks for posting back Aaron... I had noticed that if I rotated the Polygon say 45 degrees (or whatever), then the area changed by a couple of units alright... and this was definitely wrong, but like you indicated, I suspected this had more to do with how Excel was displaying / storing the numbers and I honestly didnt investigate it much further after that. The area reamins consistent when I MOVED the polygon (just dragged the whole darn thing to another location) so I was happy enough then that the formulas were correct -ish. Oddly, I cant remember testing it with a square and rotating it : D


    I'll take a look again tomorrow...


    Ger

    _______________________________________________
    There are 10 types of people in the world. Those that understand Binary and those that dont. :P


    Why are Halloween and Christmas the same? Because Oct 31 = Dec 25... ;)

    _______________________________________________

  • Re: Calculate Polygon Area


    Aaron, I updated the code in the thread when calculating the area of the polygon...


    http://www.ozgrid.com/forum/sh…d.php?p=322975#post322975


    You were right - there was some rounding errors when the numbers were being displayed in both the cells of the worksheet and the segment line lengths. These are now fixed (3 decimal places), but the area that the code was caluclating was correct (based on the numbers it had in the node points). So the formula would appearto be right :rolleyes:


    Take note though, that if you rotate a square (or rectangle) through about 45 degrees, the 90 degree corner on the shape becomes either 89.9 or 90.1... this effects the segment lengths and hence the area of the shape, so you will get a slightly different area for a rotated polygon. I am inclined to think this has more to do with how Excel rotates the image as opposed to an error in Andy's trig. calculations for caculating the angles that each line in the polygon makes. It could be fixed by a Round statement, but this is probably introducing a fix for something that is arguably not broken.


    Cheers
    Ger

    _______________________________________________
    There are 10 types of people in the world. Those that understand Binary and those that dont. :P


    Why are Halloween and Christmas the same? Because Oct 31 = Dec 25... ;)

    _______________________________________________

  • Re: Calculate Polygon Area


    I'll be playing with it later in the week...


    If I happen to come across anything that I think would resolve the rotation/accuracy issue, I'll post back.


    I appreciate your input.

  • Re: Area Of A Polygon


    Quote from tinyjack

    This thread might be of interest to you. The final code I posted works (I think), but I am sure there are better ways of doing it.


    http://www.xtremevbtalk.com/showthread.php?p=626774


    TJ


    Thanks TJ,


    At a glance it appears you're automating the task of bisecting the polygon into a series of triangles, which I assume you're using as the basis for calculating area then.


    I would like to first investigate Ger Plante's function since it does the calculation without creating triangles. I suspect the accuracy of the calculation will be influenced by the angle and length calcs. Breaking the poly into a series of smaller triangle polys may further compound that inaccuracy.


    However, your code may be useful later... since I was thinking about adding a feature to redraw a polygon as a series of triangles (or right triangles).


    Thanks,
    AB

  • Hi Ger - the link to the the post above appears no longer valid. are you able to share again? NB I think I have an old file by you named "shapes measure area 48347_Ger" but I think I may have broken part it. I wonder if that is still available. Many thanks in advance.:)

  • To calculate Polygon Area the formula can be represented by the expression:


    If the points (x, y) of the polygon are known, we can apply the previous equation using simple arithmetical calculations. In this example (taken from Wikipedia) we have 5 points, so, first we calculate the xi*yi+1 (i.e. first point: 3 * 11 = 33 etc.) and then the xi+1*yi (i.e. first point: 5 * 4 = 20 etc.) for each point. Note that for convenience purposes we repeated the coordinates of the first point at the end of the table.


    Next, we sum the xi*yi+1 (i.e. 207) and the xi+1*yi (i.e. 267). Finally, we take the absolute value of the difference divided by 2 (i.e. ABS(207 – 267)/2 = 60/2) and the result is the desired polygon area (i.e. 30).


    Regards,

    Rick Bale

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!