d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java Help
Add Reply New Topic New Poll
Member
Posts: 32,450
Joined: Jul 9 2007
Gold: 15.00
Feb 20 2013 04:24am
i need help with some java/Learning Processing stuff
Let me know if you can help me
will pay up to 1000fg
Member
Posts: 15,988
Joined: Nov 12 2005
Gold: 4,399.00
Feb 20 2013 06:05am
Just post the problem.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 20 2013 12:12pm
Quote (dimon222 @ Feb 20 2013 07:05am)
Just post the problem.


this

and what is "Learning Processing"? are you referring to AI or what?
Member
Posts: 32,450
Joined: Jul 9 2007
Gold: 15.00
Feb 20 2013 06:18pm
Objective

The main objective of this assignment is to gain some practice with user defined methods for breaking a problem into smaller pieces and organizing your program.
In addition you will gain further practice with variables (local and global), expressions, and conditional statements. This is also your first assignment where you must write a program to complete a specific, narrowly defined task.


Program Specification

Ever wonder how anyone figures out the value of π, which is 3.1415...? You should recall that the area of a circle is π*r2. Let R be the ratio of the area of a circle to the area of a square in which that circle is inscribed. That gives us:

R= (π*r^2)/ ((2r)^2)

Solving this equation for pi gives: π = 4R

We can use simulation to estimate the ratio R of the area of a circle with diameter 1 to that of a square with sides equal to 1. Imagine drawing a circle inscribed in a square as shown here:



We are going to simulate dropping stones uniformly at random within the square. We can then estimate the ratio of the two areas by counting how many stones land within the circle compared to the total number of stones within the square. If that ratio is R, then an estimate of π is 4R.

For this assignment you will create such a simulation. Your "boss" has already completed the top level design and determined that the program should be implemented by creating three functions. Your job is to complete those three functions and combine them into a complete program.

At the heart of the algorithm is the method runOneTrial() which will simulate dropping one stone on the square and update two global variables, numTrials (the number of stones dropped in this simulation) and inCount (the number of stones that landed within the circle). The location and size of the circle is controlled by global variables circleRadius, circleX, and circleY. To perform its task, runOneTrial() will compute one random number x in the range circleX-circleRadus to circleX+circleRadius and another y in the range circleY-circleRadius to circleY+circleRadius. Think of (x, y) as the coordinates of where the stone landed within your square with sides of length 2*circleRadius centered at (circleX, circleY). If that point, (x, y), is also at most circleRadius units from the center of the circle then it is also within the circle.

To make your program modular, you will create a method isIn(float x, float y) that returns true if a stone dropped at (x, y) is also within the circle specified by the global variable circleX, circleY, and circleRadius and return false otherwise. This method should be called when needed by runOneTrial(). Conveniently, processing provides us a built in function, dist() that can be used to compute the distance between two points, just the value we need to know if our point (x, y) is within one circle radius of the center of the circle.

You will need to write a third function/method runTrials() that takes one integer specifying the number of trials to run. This function will call runOneTrial() repeatedly.

Finally, you will need to implement a draw() method that each frame runs some specified number of tests and displays the current estimate of pi along with the number of stones that have been dropped. Note that the method runOneTrial() will need to be modified to not only update the global counts numTrials and inCount, but should also simulate the dropping of the stone by drawing a point on the screen. The color of the point should be determined by whether the stone was determined to have landed in our out of the circle.


Getting Started

Write the method isIn(). Test it by calling it with mouseX, mouseY drawing a point at (mouseX, mouseY) in a color that is determined by the result returned from isIn(). You should see a circle inside of a square appear like rubbing a pencil on a piece of paper placed over a coin.
Write the method runOneTrial(). Test it by calling it from draw(). You should see points beginning to appear on the screen (although rather slowly). You should also print out numTrials and inCount and see that inCount is about 75% the size of numTrials.
Write the method runTrials() and call that from draw(). Experiment with how many trials to run each frame in order to have the simulation progress at a satisfactory rate.
Finish up by adding code to display the improving estimate and the number of trials that have been run. These should appear in sketch display not just in the console window (i.e. draw the text with text() rather than print it with println()). Note you will NOT want to redraw the entire background each frame. Doing so would erase all of the stones dropped during the previous frame. Instead, for the text area of the display you will need to "erase" the old text by first drawing an appropriately sized rectangle over the text area.
Member
Posts: 32,450
Joined: Jul 9 2007
Gold: 15.00
Feb 20 2013 06:24pm
Learning Processing means the program called Processing

Im just not sure how to draw that picture above and have dropping stones to fill that circle up


int px, py; // coordinates of the previous point in the plot
int overalInCircle; // number of times the stone landed "in the circle"
int numTrials; // number of times a stone was dropped
float estimate; // current estimate of pi

void setup() {
size(600,600);
PFont font = loadFont("ArialMT-48.vlw"); // used for the final display of the estimate
fill(0);
textFont(font, 32);

// draw a red line showing the "actual" value of PI
stroke(255,0,0);
line(0, scaleAndTranslate(PI), width, scaleAndTranslate(PI));
stroke(0); // use black for future drawing
}

/*
Scale and translate the estimate so that values are within the display,
that is generally between 0 and height.
*/
int scaleAndTranslate(float est) {
int precision = 1000; // bigger means more precision
float guess = 3.0; // should be close but on the low side
return height-int(precision*(est - guess));
}

/*
Each frame (until the display is filled), compute a refined estimate of pi and plot it's value
by drawing a line between (frameCount-1, previousEstimate) and (frameCount, currentEstimate),
where the estimates are scaled and translated to be between 0 and height.
Once the display is filled (frameCount exceeds width) display the final estimate.
*/
void draw() {

}

// perform numTrials simulated tosses and return the number that landed in the circle
int performTrials(int numTrials) {
return 0;
}

// Perform one trial.
// return true if the stoned landed "in the circle"
boolean isIn() {
return true;
}


this is what i have so far
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 20 2013 06:38pm
Quote
Im just not sure how to draw that picture above and have dropping stones to fill that circle up


there should be some sort of drawRectangle method to make the square. use equal lengths. 0,0 is top left iirc. if there is no drawRectangle, there should at least be a drawLine. just make 4 lines.

there should be some sort of drawOval for the circle. set the radius to be half the length of the square.

to drop stones, pick random (via java.util.Random) integers between 0 and the length of your square for both the x and y coordinates. increment all your variables to take that into account, add it to your list if it's an object, and draw it out.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll