We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Particularly, we want to find the area of the circle centered in origin. Points in its perimeter are expressed as x * x + y * y = r * r, being r, the radius, and (x, y) a point of the circumference. Our known area is the graphic window of processing, then we'll translate the coordinate axis to the center of the circle. On that origin we'll draw a circle of radius r. Then we'll "throw" N points, and we'll count how many of them "land" inside the circle. (storing the number of points in a variable called "C") According to Monte Carlo: Area of the circle = (C / N) * (area of the graphic window). Then PI = (N / C * r * r) * (area of the graphic window).
The objective is to build a function that returns the apprx value of PI, given 3 parameters:
If the simulation is drawn, there must be a circle in the center of the graphic window, and random points, it they land inside the circle, they must be BLUE, otherwise, they'll be RED.
Can someone please help me do this?
Answers
Sure. What code do you already have? Post it here. Make sure you format it for the forums. This is basically a requirement for anyone to help you.
https://forum.processing.org/two/discussion/15473/readme-how-to-format-code-and-text
Okay. The first problem that jumps out at me is that you have an if statement that doesn't have a condition in it. What condition must be true for a random point to be inside a circle? Specifically, what can you say about the distance (
dist()
) between the point and the circle's center (even more specifically, in relation to the circle's radius)?Draw a diagram if you have to, and work out what that condition should be.
The second problem that I see is that you are not doing all your drawing in draw(). Move the ellipse that you are drawing from setup() to draw().
Third, how many random points are you making? Just one for now, sure, but how many are you going to need? You have a variable to track that, so you should make use of it.
Attempt to add these changes to your code and post the resulting sketch for more help.
Cool, it's possible! But I think your math is wrong... If A is the area of the sketch and E is the area of the circle, then E is PI * R * R and A is W * H. If the points in the circle is C and the total number of points is N, then you'd expect E : A = C : N. So Pi is... well, you can work it out.
Thank you so much for your help so far. This is the code I've worked out.
TfGuy44 Could you please post your code so that I see what I have to do? If I have any doubt I'll ask you
No. What part of your assignment is unclear to you?
You've already been shown enough of my code.
You don't need a while block. But you do need a loop of some kind.
The thing is that we've only seen while, if, else if... etc... so anything out of that is unknown to me.
You need a variable to keep track of the number that fall in the circle.
I suggest you use the float data type for randX and randY rather than int. Using whole numbers may distort your Pi approximation.
I was using integers because points are supposed to be int data type, or am I wrong?
You can use round() on the
float
datatype if you wanna: ~O)https://Processing.org/reference/round_.html
But AFAIK, all pixels are round() internally before being rendered. :-j
A pixel position is represented by 2 integer values (horizontal and vertical position) but the problem you are trying to solve, the value of PI, is a floating point number type. The method
point(x, y)
will accept float variables as well as ints e.g.point(2.1, 70.9)
is a valid statment in Processing.My sketch took about 400,000 random points to estimate Pi to 3 decimal places.
You can write the loop you need using a while loop.
The circle should just fit inside the square so the radius should be
width/2
so add a line to setup to set the radius correctly.You are using translate to move the drawing origin before drawing the ellipse which according to line 14 is centred at [0,0] so the dist calculation should be
dist(randX, randY, 0, 0)
Change the data type of randX and randY to float so
Make these changes and add a variable to count the number of points generated and another to count those inside the circle and all you have left is calculate Pi.
Make sure you meet the other requirements of your assignment too! You should be doing all this inside a function that returns the approximate value. And that function needs to take the three parameters mentioned. Can you make the drawing conditional on the boolean?
First, I don't know what c++; does inside the loop. Secondly, if I declare the radius inside setup it won't be recognized by draw(). I don't know how to make a function that counts the amount of points either
The problem here is that our professor explains processing with very easy examples, but then, our tasks look like this, and most of my classmates have already given up on this subject (we just started using Processing)
And btw, thank you all so much for your help
I didn't say declare
radius
inside setup I said set its value. You have already declared and initialisedradius
to 200 in line 2. Change to a declarationint radius; // declaration
an initialise in setup with
radius = width/2; // initialise after the size() statement
c++
simply increments the value inc
by 1I've tried to fix the thing about coloring the points inside the circle, but i don't know what's wrong. Also I don't know how to create the function to count points.
Look at my last post you got line 6 wrong and line 15 should be
ellipse(0,0,2*radius,w*radius);
What's w??
Nevermind, it's another 2. My doubt still remains in how to create a function that counts the points inside the circle.
And points spawning inside the circle are black instead of blue... I'm quite confused to be honest. Besides that, thanks for your help so far.
Use stroke not fill to determine the colour used by point()
I think you would do well to realize that you should not be generating the points one at a time - You should not just add a new point every time draw() runs.
Instead, you should write a new function that is going to calculate the approximation. This function can start with just one input: How many points the approximation should use.
Try editing this code (is based off what you already have!) to generate a lot of points.
I also don't really get what my professor means by a boolean variable that determines if the simulation must be drawn. But step by step, now I need to count the points inside the circle, those are the ones which dist(randX, randY, 0, 0) < radius), but I don't know how to continue from here. Thank you so much!!
Right, so you only need to work out the value for c now, which is the number of (BLUE!) points inside the circle. So you need a counter variable. Start counting at 0, and every time you draw a blue point (what code does that?), also increase the counter (
counter++;
which is the same ascounter = counter + 1;
). After all the points are drawn, since the counter will only have increased when you drew blue - and not red - points, the counter will have counted how many blue points there are... which is c, the value you need!Ok, I added an if, inside the while loop. This is the code right now,
So you want the counter variable,
c
, to be set to 0 every time the loop runs? Nope! You want to define the variable BEFORE the loop, and then only increment it (and NOT REDEFINE IT!) inside the conditional that... you already had!Basically, move line 22 to before the loop. This defines your counter variable, and it starts at 0. Then get rid of lines 23 to 25. You are already checking that condition on line 13. Just increment the counter inside that block. Put it right after you have drawn the blue point.
Also, don't use
c
and alsoC
. They are DIFFERENT VARIABLES and it's SUPER F%^&*ING CONFUSING. Just have one variable calledc
. Orcounter
. Ornumber_of_points_inside_circle
. Pick a name that you can understand what it's used for and use it.Alright, that's true, by defining the variable inside the loop, it'd be set to 0 every time it runs. I've done it. Now I need a variable to store the value of c++, so that I can use it inside piApprox, but if I declare it inside the if, i won't be able to use it outside that block, should I declare it globally?
You can still use the value of
c
on line 26! But make sure you usec
(lowercase), notC
(uppercase)! See how confusing it is when you mix cases in variables? Stick to variables inlowercase_only
.Now I'm using lowercase c, and it says that the result is 0.0...
Please, according to your advice everything should work out, but it doesn't. Please check it out please.
are we sure the formula in line 39 is good?
What does this line you've added do??
you haven't answered my question:
Please do. Where does the formula come from, what does it mean, how does it work?
Your question:
You asked
I haven't added the line. I changed it. Look at
map()
in the reference to find out ;-)Somehow I managed to make it work. the formula in 39 is correct, and it's been provided by my professor. Let me explain. Area of the circle/area of the square(graphic window) = blue points/total points
Expressed the way we want in processing, it translates to:
(Pi * radius * radius)/(width * height) = c/n
Isolating Pi, we get:
Pi = (c * width * height)/(n * radius * radius) My code looks like this right now.
Variables' names are now in Spanish, I'm from Spain. There's one thing missing to complete the assignment
The objective is to build a function that returns the apprx value of PI, given 3 parameters:
when you use text() for output, PI is visible when you use this:
(with println I see a ? instead of PI)
The other stuff you can solve alone I think
I don't really know what I have to do with what he says about using a boolean variable that determines if the simulation must be drawn or not.
you want to pass more parameter to the function:
float aproximaPi( int n ) {
becomes
OK? Change the call of aproximaPi accordingly. Use r.
and then use this boolean
to switch on/off this:
I have to go now.
See you
Thank you so much for your help. This task is now finished, and all the objectives set by my professor have been completed. Thank you to everyone who contributed to make this possible, and thank you for giving me step by step solutions, that would help me understand and do things by myself.
Oh no, no, no. You don't just get to say everything is good and then leave. POST YOUR CODE. The final version. That works. So we can see it.
Neat! Now that you have it working, I can post my version:
a few errors in the code of MiguelNavarro
Did you try different values for radius, e.g. 100? Because a lot of your code assumes radius is always width/2. E.g. in your line 44 above is width and height which is wrong. Also lines 28 and 29.
You need to use ctrl-t to auto-format