Hello everyone I hope Im not bothering you all again. I have just begun to use R and so Im not yet familiarized with it.. I ve got an assignment which consists in calculating the area of a circle given a certain radius and center using the monte carlo method, which means that I have to plot a circle given its parameters. Limit the area inside it...with as many sample points as possible...and all of this inside a nxn size window, which could be 1x1, or any size actually. I wonder how I could just limit the area inside the circle..that would be great help for me. Some of you have given me several suggestions about plotting a circle using some built-in functions but I wonder if its suits my problem since I have to somehow limit the circles inner area. thanks a lot __________________________________________________________________ Roxana Bravo ICQ#: 14040506 _________________________________________________________________ --------------------------------- O melhor e-mail gratuito da internet: 6MB de espaço, antivírus, acesso POP3, filtro contra spam. [[alternate HTML version deleted]]
You can look at the function ngon in package maptree to see how to generate a polygon definition of a circle (to whatever resolution you want). Then you can use the polygon clipping functions in gpclib to clip your points to a circle.> I ve got an assignment which consists in calculating the area of > a circle given a certain radius and center using the monte carlo > method, which means that I have to plot a circle given its parameters. > Limit the area inside it...with as many sample points as possible... > and all of this inside a nxn size window, which could be 1x1, or any > size actually. I wonder how I could just limit the area inside the > circle..that would be great help for me. Some of you have given me > several suggestions about plotting a circle using some built-in > functions but I wonder if its suits my problem since I have to > somehow limit the circles inner area.
Roxana Bravo <neoroxana at yahoo.com.br> writes:> I ve got an assignment which consists in calculating the area of a > circle given a certain radius and center using the monte carlo > method, which means that I have to plot a circle given its > parameters. Limit the area inside it...with as many sample points as > possible...and all of this inside a nxn size window, which could be > 1x1, or any size actually. I wonder how I could just limit the area > inside the circle..that would be great help for me. Some of you have > given me several suggestions about plotting a circle using some > built-in functions but I wonder if its suits my problem since I have > to somehow limit the circles inner area.At least you state that this is an assignment :-) It is close enough to something that I have been describing to a class that I will answer here and sent a copy to my class. One way to do this is to simulate a large number of points uniformly distributed in the rectangle [-1,1]x[-1,1] and check the distance of the point from the origin. The ratio of the number of points of distance less that 1 from the origin to the total number of points simulated is an estimate of the ratio of the area of the unit circle to the area of the rectangle (4, in this case). We expect the ratio to be close to pi/4. Interestingly, in my first simulation I got a ratio that is exactly pi/4 to 4 significant digits.> rdat = matrix(runif(10000 * 2, min = -1 , max = 1), nrow = 2) > sum(colSums(rdat * rdat) < 1)[1] 7854> pi/4[1] 0.7853982
Roxana, Ignoring R for the moment... I'm not sure I understand your nxn window thing, but: if you have a circle with center (A,B) and radius R, and you have a given point (X,Y), if you conceptually translate everything by (-A,-B), so now you have a circle with center (0,0) and radius R, and the point (X-A,Y-B). The given point (X,Y) is inside the original circle if sqrt((X-A)*(X-A)+(Y-B)*(Y-B)) < R. The given point (X,Y) is outside the original circle if sqrt((X-A)*(X-A)+(Y-B)*(Y-B)) > R. If sqrt((X-A)*(X-A)+(Y-B)*(Y-B)) = R, then it's exactly on the edge of the circle. If you generate many pseudo-random points (X,Y) uniformly within a box where: A-R < X < A+r and B-R < Y < B+r, then you just need to count how many satisfy sqrt((X-A)*(X-A)+(Y-B)*(Y-B)) < R and how many do not. Or should that be <= R ? I just got off an airplane, but it goes something like that. For extra credit, how many points (X,Y) do you need on average to calculate the area within some margin of error (delta)? douglas Roxana Bravo wrote:>Hello everyone > >I hope Im not bothering you all again. I have just begun to use R and so Im not yet familiarized with it.. > >I ve got an assignment which consists in calculating the area of a circle given a certain radius and center using the monte carlo method, which means that I have to plot a circle given its parameters. Limit the area inside it...with as many sample points as possible...and all of this inside a nxn size window, which could be 1x1, or any size actually. I wonder how I could just limit the area inside the circle..that would be great help for me. Some of you have given me several suggestions about plotting a circle using some built-in functions but I wonder if its suits my problem since I have to somehow limit the circles inner area. > >thanks a lot > >