新农上路
- 积分
- 99
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2011-11-22
- 最后登录
- 1970-1-1
|
本帖最后由 yjyongz 于 2011-12-11 11:41 编辑
我想地太简单了么?
coder 发表于 2011-9-2 03:41 ![]()
这个是用你的 方法来做的。。
结果是可以的
概率是 圆面积 / 四方形面积
不知道对不对。。
- import java.awt.*;
- import java.applet.*;
- public class Uniform_Circle extends Applet{
- /**
- * @param args
- */
- Image myImage;
-
- public void method_one(){
- int times = 10000;
- int r = 100;
- while(times > 0){
- int x = (int)(Math.random()*100%101);
- int y = (int)(Math.random()*100%101);
- int result = (int)Math.sqrt(x * x + y * y);
- if(result <= r){
- int cases = (int)(Math.random()*4);
- switch(cases){
- case 0://1st section
- setPixel(myImage, x + 100,100 - y, new Color(0).blue);
- break;
- case 1://2nd section
- setPixel(myImage, 100 - x, 100 - y, new Color(0).blue);
- break;
- case 2://3rd section
- setPixel(myImage, 100 - x, y + 100, new Color(0).blue);
- break;
- case 3://4th section
- setPixel(myImage, x + 100, y + 100, new Color(0).blue);
- break;
- }
- //System.out.println(x + " " + y);
- }
- times--;
- }
- }
-
- public void init() {
- int x = 200 + 1;
- int y = 200 + 1;
- myImage = createImage(x, y);
- Graphics g = myImage.getGraphics();
- //g.drawLine(0, 0, x, 0);
- //g.drawLine(0, 0, 0, y);
- g.drawRect(0, 0, x -1 , y -1);
- method_one();
- }
- public void paint(Graphics g) {
- g.drawImage(myImage, 0, 0, this);
- }
- public void setPixel(Image image, int x, int y, Color color) {
- Graphics g = image.getGraphics();
- g.setColor(color);
- g.fillRect(x, y, 1, 1);
- g.dispose();
- }
- }
复制代码
![]() |
|