0

I want to know its possible to calculate the area of an organic shape. The shape im trying to calculate looks something like this:

Imagine its drawn by CGPoints

Is there a special function for this? Im thinking maybe CoreImage or Quartz or maybe opengl.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Arbitur
  • 38,684
  • 22
  • 91
  • 128
  • 1
    In what sense is it "drawn by CGPoints"? Also, do you know how to integrate? –  Aug 10 '13 at 21:00
  • I mean that every corner is a point, it isnt drawn its just points, i just drew it so you guys can see what kindof shape im talking about, no i dont know how or what integrate is. – Arbitur Aug 10 '13 at 21:03
  • Oh i looked up what integrate is, I know how to do it, it isnt called that in my country :P – Arbitur Aug 10 '13 at 21:05

1 Answers1

1

If the boundary path consists only of straight line segments and does not intersect itself then you can use the following formula to compute the area of the enclosed region (from https://en.wikipedia.org/wiki/Polygon#Area_and_centroid):

CGPoint points[N]; 

CGFloat area = 0;
for (int i = 0; i < N; i++) {
    area += (points[i].x * points[(i+1) % N].y - points[(i+1) % N].x * points[i].y)/2.0;
}

where points[0], ... , points[N-1] are the starting points of the line segments in counter-clockwise order.

For more complicate path segments such as Bézier curves, you can subdivide each segment into small parts that can be approximated by line segments.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • What does the % do? some sort of division? – Arbitur Aug 10 '13 at 22:31
  • % is modulo, or remainder after integer division. – bobnoble Aug 10 '13 at 23:11
  • @Arbitur: One step of the summation accesses `points[i]` and `points[i+1]` which are the start and end point of the i-th line segment. For the last step (i = N-1) this should "wrap around" to `points[N-1]` and `points[0]`. The modulo operator is just a convenient way to get this "wrap around" because `N % N = 0`. If you look at the formula and the picture in that Wikipedia article then you will probably understand it. - But please let me know if you need more help! – Martin R Aug 11 '13 at 03:57
  • So 10 % 3 = 7 as I understand from bobnobles answer. – Arbitur Aug 11 '13 at 11:13
  • @Arbitur: 10 % 3 = 1 (the remainder if you divide 10 by 3), and 10 % 7 = 3. – Martin R Aug 11 '13 at 11:33
  • @Arbitur: The only important point of the modulo operator in this formula is that `i % N = i` for `i = 0, ..., N-1`, and `N % N = 0`, so that in the last step of the summation (i=N-1), `points[(i+1) % N] = points[0]`. The endpoint of the last path segment is the starting point of the first segment. - I hope that helps to understand the formula! – Martin R Aug 11 '13 at 15:11
  • Hm so it makes it calculate the area from the other direction? like when the index should be 0 its N and when it should be N its 0? – Arbitur Aug 11 '13 at 16:35
  • @Arbitur: The first segment is from `points[0]` to `points[1]`. The second segment is from `points[1]` to `points[2]`, ... and so on. Now the *last* segment is not from `points[N-1]` to `points[N]` but from `points[N-1]` to `points[0]`, and that is achieved by the modulo operator. – Martin R Aug 11 '13 at 16:41
  • 1
    I read some examples on the web and now I think Ive got it. 2%2=0 because 2 can only go in 2 1 time and leaves 0 to waste. 7%3=1 because 3 can only go in 7 2 times and leaves 1 to waste. – Arbitur Aug 11 '13 at 17:09