3

I have defined the unit square as a region in a matlab code and I've created ellipses of random sizes, all centered in the unit square.

However, just because they are centered in the unit square does not mean that all of their area is inside the unit square.

I want to consider only the area inside of the unit square, so I calculated to total area of each randomly generated ellipse, but I'm not sure how to subtract the area that is outside of the unit square.

Here is the part of my code that creates the ellipses and calculates their area:

a = rand(16,1);
totarea = zeros(1,16);

for i = 1:4:16

ellipse(a(i)/2,a(i+1)/2,a(i+2),a(i+3))
totarea(i) = pi*a(i)*a(i+1)/4;

end

b = find(totarea > 0);
totarea = totarea(b);

Here is a photo of what I have generated:

The large square is the unit square. Inside of that square is the region that I'm interested in. As you see, there are ellipses generated that overlay that square. I want to find the area of each ellipse that lies inside of the square.

The code that I produced above allows me to calculate the total area of each ellipse, but I need to subtract the area of the ellipses that occurs outside of the square and I'm not sure how to write a code that can do that.

Community
  • 1
  • 1
user132039
  • 99
  • 6
  • 1
    Not sure what you mean. Could you provide some figure/illustration (or link to one) of what you are trying to do. – Marcin Jun 25 '14 at 05:14
  • I updated the posting to include a figure with more detailed explanation – user132039 Jun 25 '14 at 06:04
  • First of all why does your function `ellipse` expect four inputs? normally an ellipse is defined by two variables... Then how did you define the "unit square"? What you could do to get the area of those ellipses is: You could say that each pixel is equal to x cm/mm/inch what ever unit you want to use. Then you could just compute the number of pixel which are inside your ellipse and inside your "unit square". This would not result in the exact area but would result in realistic data. Check out the ["Riemann sums"](http://en.wikipedia.org/wiki/Riemann_integral). – The Minion Jun 25 '14 at 06:29
  • 1
    relevant link: http://math.stackexchange.com/questions/261336/intersection-between-a-rectangle-and-a-circle?rq=1 – bla Jun 25 '14 at 07:21

1 Answers1

2

One way to approximate the intersection area is by discretization on the unit area. Let's assume each ellipse is represented by four parameters x0, y0, a and b such that

el( x0, y0, a, b ): ( (x-x0)/a )^2 + ( (y-y0)/b )^2 <= 1

Now we can estimate

h = 1e-3; % discretization accuracy
[x y] = meshgrid( 0:h:1, 0:h:1 ); % discretizing the unit square
el = ( (x-x0)/a ).^2 + ( (y-y0)/b ).^2 <= 1; % set all points of ellipse that are inside 
intersect_area = sum( el(:) ) * h * h; 

The smaller you pick h the more accurate your estimate would be, at the cost of memory consumption and runtime.

Shai
  • 111,146
  • 38
  • 238
  • 371
  • +1. A question though. Why do you square `h` in the last summation? Why not just multiply? I have an idea why but I'm not entierly sure. – kkuilla Jun 25 '14 at 07:53
  • @kkuilla `h^2` is the area of a single pixel w.r.t. the unit square. – Shai Jun 25 '14 at 08:17