Find area of two overlapping circles using monte carlo methodWhy use Monte-Carlo method?Solving non linear equations related to distanceMonte Carlo in MATLABMonte Carlo Method in PythonMonte Carlo Method crash?finding the intersection where multiple 3D parametric equations meetQuasi Monte Carlo in MatlabCalculating Variance of Monte Carlo EstimateSpeeding Monte Carlo in matlabMonte Carlo Method in R

Raspbian gcc does not know '.intel_syntax'?

What's so great about Shalantha's Delicate Disk?

Manager is asking me to eat breakfast from now on

What is the minimum wait before I may I re-enter the USA after a 90 day visit on the Visa B-2 Program?

Does a hash function have a Upper bound on input length?

Can two waves interfere head on?

Quickest way to move a line in a text file before another line in a text file?

What would be the effects of (relatively) widespread precognition on the stock market?

Conditional statement in a function for PS1 are not re-evalutated

Install suspension forks on non-suspension bike

A bicolour masyu

Why can't a country print its own money to spend it only abroad?

She told me that she HAS / HAD a gun

Found old paper shares of Motorola Inc that has since been broken up

What does this chess proverb mean?

3D cursor orientation

Why is the forgetful functor representable?

Does the Bracer of Flying Daggers really let a thief make 4 attacks per round?

Remove side menu(right side) from finder

What's a German word for »Sandbagger«?

I have a domain, static IP and many devices I'd like to access outside my house. How to route them?

A Real World Example for Divide and Conquer Method

Would using carbon dioxide as fuel work to reduce the greenhouse effect?

How does the Gameboy's memory bank switching work?



Find area of two overlapping circles using monte carlo method


Why use Monte-Carlo method?Solving non linear equations related to distanceMonte Carlo in MATLABMonte Carlo Method in PythonMonte Carlo Method crash?finding the intersection where multiple 3D parametric equations meetQuasi Monte Carlo in MatlabCalculating Variance of Monte Carlo EstimateSpeeding Monte Carlo in matlabMonte Carlo Method in R






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















Actually i have two intersecting circles as specified in the figure



i want to find the area of each part separately using Monte carlo method in Matlab .



The code doesn't draw the rectangle or the circles correctly so
i guess what is wrong is my calculation for the x and y and i am not much aware about the geometry equations for solving it so i need help about the equations.



enter image description here
this is my code so far :



n=1000;
%supposing that a rectangle will contain both circles so :
% the mid point of the distance between 2 circles will be (0,6)
% then by adding the radius of the left and right circles the total distance
% will be 27 , 11 from the left and 16 from the right
% width of rectangle = 24

x=27.*rand(n-1)-11;
y=24.*rand(n-1)+2;
count=0;

for i=1:n

if((x(i))^2+(y(i))^2<=25 && (x(i))^2+(y(i)-12)^2<=100)
count=count+1;
figure(2);
plot(x(i),y(i),'b+')
hold on

elseif(~(x(i))^2+(y(i))^2<=25 &&(x(i))^2+(y(i)-12)^2<=100)
figure(2);
plot(x(i),y(i),'y+')
hold on

else
figure(2);
plot(x(i),y(i),'r+')

end

end









share|improve this question
























  • What exactly is your question? The vague "I need help" does not explain what you want. Please describe what the code is doing, both correct and incorrect. Please describe what you want. The help center pages give lots off advice on how to write a good question. See also minimal reproducible example.

    – AdrianHHH
    Feb 28 '16 at 13:25











  • " i need help about that " was about the geometric equations ,, any ways i edited it , thanks for the notice

    – Suzy
    Feb 28 '16 at 13:34

















0















Actually i have two intersecting circles as specified in the figure



i want to find the area of each part separately using Monte carlo method in Matlab .



The code doesn't draw the rectangle or the circles correctly so
i guess what is wrong is my calculation for the x and y and i am not much aware about the geometry equations for solving it so i need help about the equations.



enter image description here
this is my code so far :



n=1000;
%supposing that a rectangle will contain both circles so :
% the mid point of the distance between 2 circles will be (0,6)
% then by adding the radius of the left and right circles the total distance
% will be 27 , 11 from the left and 16 from the right
% width of rectangle = 24

x=27.*rand(n-1)-11;
y=24.*rand(n-1)+2;
count=0;

for i=1:n

if((x(i))^2+(y(i))^2<=25 && (x(i))^2+(y(i)-12)^2<=100)
count=count+1;
figure(2);
plot(x(i),y(i),'b+')
hold on

elseif(~(x(i))^2+(y(i))^2<=25 &&(x(i))^2+(y(i)-12)^2<=100)
figure(2);
plot(x(i),y(i),'y+')
hold on

else
figure(2);
plot(x(i),y(i),'r+')

end

end









share|improve this question
























  • What exactly is your question? The vague "I need help" does not explain what you want. Please describe what the code is doing, both correct and incorrect. Please describe what you want. The help center pages give lots off advice on how to write a good question. See also minimal reproducible example.

    – AdrianHHH
    Feb 28 '16 at 13:25











  • " i need help about that " was about the geometric equations ,, any ways i edited it , thanks for the notice

    – Suzy
    Feb 28 '16 at 13:34













0












0








0


0






Actually i have two intersecting circles as specified in the figure



i want to find the area of each part separately using Monte carlo method in Matlab .



The code doesn't draw the rectangle or the circles correctly so
i guess what is wrong is my calculation for the x and y and i am not much aware about the geometry equations for solving it so i need help about the equations.



enter image description here
this is my code so far :



n=1000;
%supposing that a rectangle will contain both circles so :
% the mid point of the distance between 2 circles will be (0,6)
% then by adding the radius of the left and right circles the total distance
% will be 27 , 11 from the left and 16 from the right
% width of rectangle = 24

x=27.*rand(n-1)-11;
y=24.*rand(n-1)+2;
count=0;

for i=1:n

if((x(i))^2+(y(i))^2<=25 && (x(i))^2+(y(i)-12)^2<=100)
count=count+1;
figure(2);
plot(x(i),y(i),'b+')
hold on

elseif(~(x(i))^2+(y(i))^2<=25 &&(x(i))^2+(y(i)-12)^2<=100)
figure(2);
plot(x(i),y(i),'y+')
hold on

else
figure(2);
plot(x(i),y(i),'r+')

end

end









share|improve this question
















Actually i have two intersecting circles as specified in the figure



i want to find the area of each part separately using Monte carlo method in Matlab .



The code doesn't draw the rectangle or the circles correctly so
i guess what is wrong is my calculation for the x and y and i am not much aware about the geometry equations for solving it so i need help about the equations.



enter image description here
this is my code so far :



n=1000;
%supposing that a rectangle will contain both circles so :
% the mid point of the distance between 2 circles will be (0,6)
% then by adding the radius of the left and right circles the total distance
% will be 27 , 11 from the left and 16 from the right
% width of rectangle = 24

x=27.*rand(n-1)-11;
y=24.*rand(n-1)+2;
count=0;

for i=1:n

if((x(i))^2+(y(i))^2<=25 && (x(i))^2+(y(i)-12)^2<=100)
count=count+1;
figure(2);
plot(x(i),y(i),'b+')
hold on

elseif(~(x(i))^2+(y(i))^2<=25 &&(x(i))^2+(y(i)-12)^2<=100)
figure(2);
plot(x(i),y(i),'y+')
hold on

else
figure(2);
plot(x(i),y(i),'r+')

end

end






performance matlab montecarlo






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 28 '16 at 13:32







Suzy

















asked Feb 28 '16 at 13:00









SuzySuzy

499 bronze badges




499 bronze badges












  • What exactly is your question? The vague "I need help" does not explain what you want. Please describe what the code is doing, both correct and incorrect. Please describe what you want. The help center pages give lots off advice on how to write a good question. See also minimal reproducible example.

    – AdrianHHH
    Feb 28 '16 at 13:25











  • " i need help about that " was about the geometric equations ,, any ways i edited it , thanks for the notice

    – Suzy
    Feb 28 '16 at 13:34

















  • What exactly is your question? The vague "I need help" does not explain what you want. Please describe what the code is doing, both correct and incorrect. Please describe what you want. The help center pages give lots off advice on how to write a good question. See also minimal reproducible example.

    – AdrianHHH
    Feb 28 '16 at 13:25











  • " i need help about that " was about the geometric equations ,, any ways i edited it , thanks for the notice

    – Suzy
    Feb 28 '16 at 13:34
















What exactly is your question? The vague "I need help" does not explain what you want. Please describe what the code is doing, both correct and incorrect. Please describe what you want. The help center pages give lots off advice on how to write a good question. See also minimal reproducible example.

– AdrianHHH
Feb 28 '16 at 13:25





What exactly is your question? The vague "I need help" does not explain what you want. Please describe what the code is doing, both correct and incorrect. Please describe what you want. The help center pages give lots off advice on how to write a good question. See also minimal reproducible example.

– AdrianHHH
Feb 28 '16 at 13:25













" i need help about that " was about the geometric equations ,, any ways i edited it , thanks for the notice

– Suzy
Feb 28 '16 at 13:34





" i need help about that " was about the geometric equations ,, any ways i edited it , thanks for the notice

– Suzy
Feb 28 '16 at 13:34












3 Answers
3






active

oldest

votes


















2














Here are the errors I found:



x = 27*rand(n,1)-5
y = 24*rand(n,1)-12


The rectangle extents were incorrect, and if you use rand(n-1) will give you a (n-1) by (n-1) matrix.



and



first If:



(x(i))^2+(y(i))^2<=25 && (x(i)-12)^2+(y(i))^2<=100


the center of the large circle is at x=12 not y=12



Second If:



~(x(i))^2+(y(i))^2<=25 &&(x(i)-12)^2+(y(i))^2<=100


This code can be improved by using logical indexing.



For example, using R, you could do (Matlab code is left as an excercise):



n = 10000
x = 27*runif(n)-5
y = 24*runif(n)-12
plot(x,y)

r = (x^2 + y^2)<=25 & ((x-12)^2 + y^2)<=100
g = (x^2 + y^2)<=25
b = ((x-12)^2 + y^2)<=100
points(x[g],y[g],col="green")
points(x[b],y[b],col="blue")
points(x[r],y[r],col="red")


which gives:



Colored regions






share|improve this answer

























  • Thanks alot Sir , that was really helpful :)

    – Suzy
    Feb 28 '16 at 17:36


















0














Here is my generic solution for any two circles (without any hardcoded value):



function [ P ] = circles_intersection_area( k1, k2, N )
%CIRCLES_INTERSECTION_AREA Summary...
% Adnan A.
x1 = k1(1);
y1 = k1(2);
r1 = k1(3);

x2 = k2(1);
y2 = k2(2);
r2 = k2(3);

if sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)) >= (r1 + r2)
% no intersection
P = 0;
return
end

% Wrapper rectangle config
a_min = x1 - r1 - 2*r2;
a_max = x1 + r1 + 2*r2;
b_min = y1 - r1 - 2*r2;
b_max = y1 + r1 + 2*r2;

% Monte Carlo algorithm
n = 0;
for i = 1:N
rand_x = unifrnd(a_min, a_max);
rand_y = unifrnd(b_min, b_max);

if sqrt((rand_x - x1)^2 + (rand_y - y1)^2) < r1 && sqrt((rand_x - x2)^2 + (rand_y - y2)^2) < r2
% is a point in the both of circles
n = n + 1;
plot(rand_x,rand_y, 'go-');
hold on;
else
plot(rand_x,rand_y, 'ko-');
hold on;
end
end

P = (a_max - a_min) * (b_max - b_min) * n / N;

end


Call it like: circles_intersection_area([-0.4,0,1], [0.4,0,1], 10000) where the first param is the first circle (x,y,r) and the second param is the second circle.






share|improve this answer






























    0














    Without using For loop.



     n = 100000;
    data = rand(2,n);
    data = data*2*30 - 30;
    x = data(1,:);
    y = data(2,:);
    plot(x,y,'ro');
    inside5 = find(x.^2 + y.^2 <=25);
    hold on
    plot (x(inside5),y(inside5),'bo');
    hold on
    inside12 = find(x.^2 + (y-12).^2<=144);
    plot (x(inside12),y(inside12),'g');
    hold on
    insidefinal1 = find(x.^2 + y.^2 <=25 & x.^2 + (y-12).^2>=144);
    insidefinal2 = find(x.^2 + y.^2 >=25 & x.^2 + (y-12).^2<=144);
    % plot(x(insidefinal1),y(insidefinal1),'bo');
    hold on
    % plot(x(insidefinal2),y(insidefinal2),'ro');
    insidefinal3 = find(x.^2 + y.^2 <=25 & x.^2 + (y-12).^2<=144);
    % plot(x(insidefinal3),y(insidefinal3),'ro');
    area1=(60^2)*(length(insidefinal1)/n);
    area3=(60^2)*(length(insidefinal2)/n);
    area2= (60^2)*(length(insidefinal3)/n);


    enter image description here






    share|improve this answer



























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f35682835%2ffind-area-of-two-overlapping-circles-using-monte-carlo-method%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      Here are the errors I found:



      x = 27*rand(n,1)-5
      y = 24*rand(n,1)-12


      The rectangle extents were incorrect, and if you use rand(n-1) will give you a (n-1) by (n-1) matrix.



      and



      first If:



      (x(i))^2+(y(i))^2<=25 && (x(i)-12)^2+(y(i))^2<=100


      the center of the large circle is at x=12 not y=12



      Second If:



      ~(x(i))^2+(y(i))^2<=25 &&(x(i)-12)^2+(y(i))^2<=100


      This code can be improved by using logical indexing.



      For example, using R, you could do (Matlab code is left as an excercise):



      n = 10000
      x = 27*runif(n)-5
      y = 24*runif(n)-12
      plot(x,y)

      r = (x^2 + y^2)<=25 & ((x-12)^2 + y^2)<=100
      g = (x^2 + y^2)<=25
      b = ((x-12)^2 + y^2)<=100
      points(x[g],y[g],col="green")
      points(x[b],y[b],col="blue")
      points(x[r],y[r],col="red")


      which gives:



      Colored regions






      share|improve this answer

























      • Thanks alot Sir , that was really helpful :)

        – Suzy
        Feb 28 '16 at 17:36















      2














      Here are the errors I found:



      x = 27*rand(n,1)-5
      y = 24*rand(n,1)-12


      The rectangle extents were incorrect, and if you use rand(n-1) will give you a (n-1) by (n-1) matrix.



      and



      first If:



      (x(i))^2+(y(i))^2<=25 && (x(i)-12)^2+(y(i))^2<=100


      the center of the large circle is at x=12 not y=12



      Second If:



      ~(x(i))^2+(y(i))^2<=25 &&(x(i)-12)^2+(y(i))^2<=100


      This code can be improved by using logical indexing.



      For example, using R, you could do (Matlab code is left as an excercise):



      n = 10000
      x = 27*runif(n)-5
      y = 24*runif(n)-12
      plot(x,y)

      r = (x^2 + y^2)<=25 & ((x-12)^2 + y^2)<=100
      g = (x^2 + y^2)<=25
      b = ((x-12)^2 + y^2)<=100
      points(x[g],y[g],col="green")
      points(x[b],y[b],col="blue")
      points(x[r],y[r],col="red")


      which gives:



      Colored regions






      share|improve this answer

























      • Thanks alot Sir , that was really helpful :)

        – Suzy
        Feb 28 '16 at 17:36













      2












      2








      2







      Here are the errors I found:



      x = 27*rand(n,1)-5
      y = 24*rand(n,1)-12


      The rectangle extents were incorrect, and if you use rand(n-1) will give you a (n-1) by (n-1) matrix.



      and



      first If:



      (x(i))^2+(y(i))^2<=25 && (x(i)-12)^2+(y(i))^2<=100


      the center of the large circle is at x=12 not y=12



      Second If:



      ~(x(i))^2+(y(i))^2<=25 &&(x(i)-12)^2+(y(i))^2<=100


      This code can be improved by using logical indexing.



      For example, using R, you could do (Matlab code is left as an excercise):



      n = 10000
      x = 27*runif(n)-5
      y = 24*runif(n)-12
      plot(x,y)

      r = (x^2 + y^2)<=25 & ((x-12)^2 + y^2)<=100
      g = (x^2 + y^2)<=25
      b = ((x-12)^2 + y^2)<=100
      points(x[g],y[g],col="green")
      points(x[b],y[b],col="blue")
      points(x[r],y[r],col="red")


      which gives:



      Colored regions






      share|improve this answer















      Here are the errors I found:



      x = 27*rand(n,1)-5
      y = 24*rand(n,1)-12


      The rectangle extents were incorrect, and if you use rand(n-1) will give you a (n-1) by (n-1) matrix.



      and



      first If:



      (x(i))^2+(y(i))^2<=25 && (x(i)-12)^2+(y(i))^2<=100


      the center of the large circle is at x=12 not y=12



      Second If:



      ~(x(i))^2+(y(i))^2<=25 &&(x(i)-12)^2+(y(i))^2<=100


      This code can be improved by using logical indexing.



      For example, using R, you could do (Matlab code is left as an excercise):



      n = 10000
      x = 27*runif(n)-5
      y = 24*runif(n)-12
      plot(x,y)

      r = (x^2 + y^2)<=25 & ((x-12)^2 + y^2)<=100
      g = (x^2 + y^2)<=25
      b = ((x-12)^2 + y^2)<=100
      points(x[g],y[g],col="green")
      points(x[b],y[b],col="blue")
      points(x[r],y[r],col="red")


      which gives:



      Colored regions







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Feb 28 '16 at 14:25

























      answered Feb 28 '16 at 14:12









      R. SchifiniR. Schifini

      7,3182 gold badges19 silver badges28 bronze badges




      7,3182 gold badges19 silver badges28 bronze badges












      • Thanks alot Sir , that was really helpful :)

        – Suzy
        Feb 28 '16 at 17:36

















      • Thanks alot Sir , that was really helpful :)

        – Suzy
        Feb 28 '16 at 17:36
















      Thanks alot Sir , that was really helpful :)

      – Suzy
      Feb 28 '16 at 17:36





      Thanks alot Sir , that was really helpful :)

      – Suzy
      Feb 28 '16 at 17:36













      0














      Here is my generic solution for any two circles (without any hardcoded value):



      function [ P ] = circles_intersection_area( k1, k2, N )
      %CIRCLES_INTERSECTION_AREA Summary...
      % Adnan A.
      x1 = k1(1);
      y1 = k1(2);
      r1 = k1(3);

      x2 = k2(1);
      y2 = k2(2);
      r2 = k2(3);

      if sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)) >= (r1 + r2)
      % no intersection
      P = 0;
      return
      end

      % Wrapper rectangle config
      a_min = x1 - r1 - 2*r2;
      a_max = x1 + r1 + 2*r2;
      b_min = y1 - r1 - 2*r2;
      b_max = y1 + r1 + 2*r2;

      % Monte Carlo algorithm
      n = 0;
      for i = 1:N
      rand_x = unifrnd(a_min, a_max);
      rand_y = unifrnd(b_min, b_max);

      if sqrt((rand_x - x1)^2 + (rand_y - y1)^2) < r1 && sqrt((rand_x - x2)^2 + (rand_y - y2)^2) < r2
      % is a point in the both of circles
      n = n + 1;
      plot(rand_x,rand_y, 'go-');
      hold on;
      else
      plot(rand_x,rand_y, 'ko-');
      hold on;
      end
      end

      P = (a_max - a_min) * (b_max - b_min) * n / N;

      end


      Call it like: circles_intersection_area([-0.4,0,1], [0.4,0,1], 10000) where the first param is the first circle (x,y,r) and the second param is the second circle.






      share|improve this answer



























        0














        Here is my generic solution for any two circles (without any hardcoded value):



        function [ P ] = circles_intersection_area( k1, k2, N )
        %CIRCLES_INTERSECTION_AREA Summary...
        % Adnan A.
        x1 = k1(1);
        y1 = k1(2);
        r1 = k1(3);

        x2 = k2(1);
        y2 = k2(2);
        r2 = k2(3);

        if sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)) >= (r1 + r2)
        % no intersection
        P = 0;
        return
        end

        % Wrapper rectangle config
        a_min = x1 - r1 - 2*r2;
        a_max = x1 + r1 + 2*r2;
        b_min = y1 - r1 - 2*r2;
        b_max = y1 + r1 + 2*r2;

        % Monte Carlo algorithm
        n = 0;
        for i = 1:N
        rand_x = unifrnd(a_min, a_max);
        rand_y = unifrnd(b_min, b_max);

        if sqrt((rand_x - x1)^2 + (rand_y - y1)^2) < r1 && sqrt((rand_x - x2)^2 + (rand_y - y2)^2) < r2
        % is a point in the both of circles
        n = n + 1;
        plot(rand_x,rand_y, 'go-');
        hold on;
        else
        plot(rand_x,rand_y, 'ko-');
        hold on;
        end
        end

        P = (a_max - a_min) * (b_max - b_min) * n / N;

        end


        Call it like: circles_intersection_area([-0.4,0,1], [0.4,0,1], 10000) where the first param is the first circle (x,y,r) and the second param is the second circle.






        share|improve this answer

























          0












          0








          0







          Here is my generic solution for any two circles (without any hardcoded value):



          function [ P ] = circles_intersection_area( k1, k2, N )
          %CIRCLES_INTERSECTION_AREA Summary...
          % Adnan A.
          x1 = k1(1);
          y1 = k1(2);
          r1 = k1(3);

          x2 = k2(1);
          y2 = k2(2);
          r2 = k2(3);

          if sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)) >= (r1 + r2)
          % no intersection
          P = 0;
          return
          end

          % Wrapper rectangle config
          a_min = x1 - r1 - 2*r2;
          a_max = x1 + r1 + 2*r2;
          b_min = y1 - r1 - 2*r2;
          b_max = y1 + r1 + 2*r2;

          % Monte Carlo algorithm
          n = 0;
          for i = 1:N
          rand_x = unifrnd(a_min, a_max);
          rand_y = unifrnd(b_min, b_max);

          if sqrt((rand_x - x1)^2 + (rand_y - y1)^2) < r1 && sqrt((rand_x - x2)^2 + (rand_y - y2)^2) < r2
          % is a point in the both of circles
          n = n + 1;
          plot(rand_x,rand_y, 'go-');
          hold on;
          else
          plot(rand_x,rand_y, 'ko-');
          hold on;
          end
          end

          P = (a_max - a_min) * (b_max - b_min) * n / N;

          end


          Call it like: circles_intersection_area([-0.4,0,1], [0.4,0,1], 10000) where the first param is the first circle (x,y,r) and the second param is the second circle.






          share|improve this answer













          Here is my generic solution for any two circles (without any hardcoded value):



          function [ P ] = circles_intersection_area( k1, k2, N )
          %CIRCLES_INTERSECTION_AREA Summary...
          % Adnan A.
          x1 = k1(1);
          y1 = k1(2);
          r1 = k1(3);

          x2 = k2(1);
          y2 = k2(2);
          r2 = k2(3);

          if sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)) >= (r1 + r2)
          % no intersection
          P = 0;
          return
          end

          % Wrapper rectangle config
          a_min = x1 - r1 - 2*r2;
          a_max = x1 + r1 + 2*r2;
          b_min = y1 - r1 - 2*r2;
          b_max = y1 + r1 + 2*r2;

          % Monte Carlo algorithm
          n = 0;
          for i = 1:N
          rand_x = unifrnd(a_min, a_max);
          rand_y = unifrnd(b_min, b_max);

          if sqrt((rand_x - x1)^2 + (rand_y - y1)^2) < r1 && sqrt((rand_x - x2)^2 + (rand_y - y2)^2) < r2
          % is a point in the both of circles
          n = n + 1;
          plot(rand_x,rand_y, 'go-');
          hold on;
          else
          plot(rand_x,rand_y, 'ko-');
          hold on;
          end
          end

          P = (a_max - a_min) * (b_max - b_min) * n / N;

          end


          Call it like: circles_intersection_area([-0.4,0,1], [0.4,0,1], 10000) where the first param is the first circle (x,y,r) and the second param is the second circle.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 29 '18 at 21:46









          adkladkl

          1952 silver badges13 bronze badges




          1952 silver badges13 bronze badges





















              0














              Without using For loop.



               n = 100000;
              data = rand(2,n);
              data = data*2*30 - 30;
              x = data(1,:);
              y = data(2,:);
              plot(x,y,'ro');
              inside5 = find(x.^2 + y.^2 <=25);
              hold on
              plot (x(inside5),y(inside5),'bo');
              hold on
              inside12 = find(x.^2 + (y-12).^2<=144);
              plot (x(inside12),y(inside12),'g');
              hold on
              insidefinal1 = find(x.^2 + y.^2 <=25 & x.^2 + (y-12).^2>=144);
              insidefinal2 = find(x.^2 + y.^2 >=25 & x.^2 + (y-12).^2<=144);
              % plot(x(insidefinal1),y(insidefinal1),'bo');
              hold on
              % plot(x(insidefinal2),y(insidefinal2),'ro');
              insidefinal3 = find(x.^2 + y.^2 <=25 & x.^2 + (y-12).^2<=144);
              % plot(x(insidefinal3),y(insidefinal3),'ro');
              area1=(60^2)*(length(insidefinal1)/n);
              area3=(60^2)*(length(insidefinal2)/n);
              area2= (60^2)*(length(insidefinal3)/n);


              enter image description here






              share|improve this answer





























                0














                Without using For loop.



                 n = 100000;
                data = rand(2,n);
                data = data*2*30 - 30;
                x = data(1,:);
                y = data(2,:);
                plot(x,y,'ro');
                inside5 = find(x.^2 + y.^2 <=25);
                hold on
                plot (x(inside5),y(inside5),'bo');
                hold on
                inside12 = find(x.^2 + (y-12).^2<=144);
                plot (x(inside12),y(inside12),'g');
                hold on
                insidefinal1 = find(x.^2 + y.^2 <=25 & x.^2 + (y-12).^2>=144);
                insidefinal2 = find(x.^2 + y.^2 >=25 & x.^2 + (y-12).^2<=144);
                % plot(x(insidefinal1),y(insidefinal1),'bo');
                hold on
                % plot(x(insidefinal2),y(insidefinal2),'ro');
                insidefinal3 = find(x.^2 + y.^2 <=25 & x.^2 + (y-12).^2<=144);
                % plot(x(insidefinal3),y(insidefinal3),'ro');
                area1=(60^2)*(length(insidefinal1)/n);
                area3=(60^2)*(length(insidefinal2)/n);
                area2= (60^2)*(length(insidefinal3)/n);


                enter image description here






                share|improve this answer



























                  0












                  0








                  0







                  Without using For loop.



                   n = 100000;
                  data = rand(2,n);
                  data = data*2*30 - 30;
                  x = data(1,:);
                  y = data(2,:);
                  plot(x,y,'ro');
                  inside5 = find(x.^2 + y.^2 <=25);
                  hold on
                  plot (x(inside5),y(inside5),'bo');
                  hold on
                  inside12 = find(x.^2 + (y-12).^2<=144);
                  plot (x(inside12),y(inside12),'g');
                  hold on
                  insidefinal1 = find(x.^2 + y.^2 <=25 & x.^2 + (y-12).^2>=144);
                  insidefinal2 = find(x.^2 + y.^2 >=25 & x.^2 + (y-12).^2<=144);
                  % plot(x(insidefinal1),y(insidefinal1),'bo');
                  hold on
                  % plot(x(insidefinal2),y(insidefinal2),'ro');
                  insidefinal3 = find(x.^2 + y.^2 <=25 & x.^2 + (y-12).^2<=144);
                  % plot(x(insidefinal3),y(insidefinal3),'ro');
                  area1=(60^2)*(length(insidefinal1)/n);
                  area3=(60^2)*(length(insidefinal2)/n);
                  area2= (60^2)*(length(insidefinal3)/n);


                  enter image description here






                  share|improve this answer















                  Without using For loop.



                   n = 100000;
                  data = rand(2,n);
                  data = data*2*30 - 30;
                  x = data(1,:);
                  y = data(2,:);
                  plot(x,y,'ro');
                  inside5 = find(x.^2 + y.^2 <=25);
                  hold on
                  plot (x(inside5),y(inside5),'bo');
                  hold on
                  inside12 = find(x.^2 + (y-12).^2<=144);
                  plot (x(inside12),y(inside12),'g');
                  hold on
                  insidefinal1 = find(x.^2 + y.^2 <=25 & x.^2 + (y-12).^2>=144);
                  insidefinal2 = find(x.^2 + y.^2 >=25 & x.^2 + (y-12).^2<=144);
                  % plot(x(insidefinal1),y(insidefinal1),'bo');
                  hold on
                  % plot(x(insidefinal2),y(insidefinal2),'ro');
                  insidefinal3 = find(x.^2 + y.^2 <=25 & x.^2 + (y-12).^2<=144);
                  % plot(x(insidefinal3),y(insidefinal3),'ro');
                  area1=(60^2)*(length(insidefinal1)/n);
                  area3=(60^2)*(length(insidefinal2)/n);
                  area2= (60^2)*(length(insidefinal3)/n);


                  enter image description here







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 26 at 14:01









                  Maciej Jureczko

                  1,3066 gold badges17 silver badges22 bronze badges




                  1,3066 gold badges17 silver badges22 bronze badges










                  answered Mar 26 at 12:32









                  Dai TranDai Tran

                  33 bronze badges




                  33 bronze badges



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f35682835%2ffind-area-of-two-overlapping-circles-using-monte-carlo-method%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                      Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript