-
Notifications
You must be signed in to change notification settings - Fork 173
/
MonteCarloPiData.java
45 lines (34 loc) · 978 Bytes
/
MonteCarloPiData.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.util.LinkedList;
import java.awt.*;
public class MonteCarloPiData {
private Circle circle;
private LinkedList<Point> points;
private int insideCircle = 0;
public MonteCarloPiData(Circle circle){
this.circle = circle;
points = new LinkedList<Point>();
}
public Circle getCircle(){
return circle;
}
public int getPointsNumber(){
return points.size();
}
public Point getPoint(int i){
if(i < 0 || i >= points.size())
throw new IllegalArgumentException("out of bound in getPoint!");
return points.get(i);
}
public void addPoint(Point p){
points.add(p);
if(circle.contain(p))
insideCircle ++;
}
public double estimatePi(){
if(points.size() == 0)
return 0.0;
int circleArea = insideCircle;
int squareArea = points.size();
return (double)circleArea * 4 / squareArea;
}
}