1、 通过设计实验的方法,利用随机函数,在一个10*10的正方形中,计算其内切三角形的面积。
注(思路):使用用随机函数实验求面积,计算三角形和正方形面积之比是多少,进不步求出三角形的面积。
正方形里面的三角形,有几种情形:
(1)三角形一条边正好重叠正方形一条边,另外一点在正方形对面一条边的中点;
(2)三角形一条边正好重叠正方形一条边,对面一点可以随意设置而构成的三角形;
(3)三点在正方形不同的三条边上,这三点随机产生。
用实验模拟方法求出三角形面积,实验方法就是统计10000个以上随机点落在三角形里面的数量,然后再试10万以上个随机点的情况,等等。
2、 计算上海到北京的直线距离
public class P2 {
public static void main(String[] args){
System.out.println(random_area_1());
System.out.println(random_area_2());
System.out.println(random_area_3());
System.out.println(distance());
}
//判断随机点是否在三角形内
public static boolean is_intri(double x,double y,Point pointA,Point pointB,Point pointC){
double x_1 = pointA.getX();
double y_1 = pointA.getY();
double x_2 = pointB.getX();
double y_2 = pointB.getY();
double x_3 = pointC.getX();
double y_3 = pointC.getY();
//random向量的x_y
double x1 = x-x_1;
double y1 = y-y_1;
//B向量的x_y
double x3 = x_2-x_1;
double y3 = y_2-y_1;
//C向量的x_y
double x2 = x_3-x_1;
double y2 = y_3-y_1;
//分母等于零时分类讨论,输出Fales
if((x2*y3-x3*y2) == 0||(x3*y2-x2*y3) == 0)
return false;
double u = (x1*y3-x3*y1)/(x2*y3-x3*y2);//u为random向量在B向量的分量占比
double v = (x1*y2-x2*y1)/(x3*y2-x2*y3);//v为random向量在c向量的分量占比
//若u >= 0 && v >= 0 && u + v <= 1时在三角形内
if(u >= 0 && v >= 0 && u + v <= 1) {
return true;
}else{
return false;
}
}
//三点在正方形不同的三条边上,这三点随机产生。
public static double random_area_3(){
Point pointrec_1 = new Point(0,0);
Point pointrec_2 = new Point(0,10);
Point pointrec_3 = new Point(10,0);
Point pointrec_4 = new Point(10,10);
int[] newarr = new int[3];//初始化数组存储选到的边
newarr = choice_line(4,3);//用整数随机数来去重复选边
Point point_A = new Point(0,0);
Point point_B = new Point(0,0);
Point point_C = new Point(0,0);
for(int i = 0;i<3;i++){
double u = 0;//u存储x轴上所取到的随机数
double v = 0;//v存储y轴上所取到的随机数
if(newarr[i] == 1){
u = MyRandom(0, 10);
v = 0;
}else if(newarr[i] == 2){
v = MyRandom(0, 10);
u = 10;
}else if(newarr[i] == 3){
u = MyRandom(0, 10);
v = 10;
}else if(newarr[i] == 4){
v = (10-MyRandom(0, 10));
u = 0;
}
// switch(newarr[i]) {
//
//
// case 1:
// u = MyRandom(0, 10);
// v = 0;
// break;
// case 2:
// v = MyRandom(0, 10);
// u = 10;
// break;
// case 3:
// u = MyRandom(0, 10);
// v = 10;
// break;
// case 4:
// v = MyRandom(0, 10);
// u = 0;
// break;
//
//
// }
//按照循环次数存储u,v
if (i == 0) {
point_A.SetX(u);
point_A.SetY(v);
} else if (i == 1) {
point_B.SetX(u);
point_B.SetY(v);
} else if (i == 2) {
point_C.SetX(u);
point_C.SetY(v);
}
}
Point pointRandom = new Point(0,0);
double sum_in = 0;
double sum_all = 0;
for(int i = 0;i < 100000;i++) {
double a = MyRandom(1,10);
double b = MyRandom(1,10);
if (is_intri(a, b, point_A, point_B, point_C)) {
sum_in++;
sum_all++;
} else {
sum_all++;
}
}
double rating = sum_in/sum_all;
double area =rating*10*10;
return area;
}
//三角形一条边正好重叠正方形一条边,对边一点可以随意设置而构成的三角形
public static double random_area_2(){
Point pointrec_1 = new Point(0,0);
Point pointrec_2 = new Point(0,10);
Point pointrec_3 = new Point(10,0);
Point pointrec_4 = new Point(10,10);
Point point_A = new Point(0,0);
Point point_B = new Point(0,10);
double x = MyRandom(0,10);
Point point_C = new Point(10,x);
Point pointRandom = new Point(0,0);
double sum_in = 0;
double sum_all = 0;
for(int i = 0;i < 100000;i++) {
double a = MyRandom(1,10);
double b = MyRandom(1,10);
if (is_intri(a, b, point_A, point_B, point_C)) {
sum_in++;
sum_all++;
} else {
sum_all++;
}
}
double rating = sum_in/sum_all;
double area =rating*10*10;
return area;
}
//三角形一条边正好重叠正方形一条边,另外一点在正方形对边一条边的中点
public static double random_area_1(){
Point pointrec_1 = new Point(0,0);
Point pointrec_2 = new Point(0,10);
Point pointrec_3 = new Point(10,0);
Point pointrec_4 = new Point(10,10);
Point point_A = new Point(0,0);
Point point_B = new Point(0,10);
Point point_C = new Point(10,5);
Point pointRandom = new Point(0,0);
double sum_in = 0;
double sum_all = 0;
for(int i = 0;i < 100000;i++) {
double a = MyRandom(1,10);
double b = MyRandom(1,10);
if (is_intri(a, b, point_A, point_B, point_C)) {
sum_in++;
sum_all++;
} else {
sum_all++;
}
}
double rating = sum_in/sum_all;
double area =rating*10*10;
return area;
}
//
public static double MyRandom(int n,int m){
//判断输入是否正确
if(n>m){
System.out.println("incorrect input!");
System.exit(0);
}
//使用真随机数函数
double number = n + (Math.random()*(m-n));
return number;
}
//用整数随机数来去重复选边
public static int[] choice_line(int x,int y){
int a_number;//存放生成的随机数
int b_number;//存放random中取到的数
int random[] = new int[x];
int arr[] = new int[y];
//将1-x存入长度为x的数组中
for(int i = 0;i < x;i++){
random[i] = i+1;
}
int o = 0;//计数器
while(o != y) {
a_number = intRandom(1, x);
b_number = random[a_number - 1];
if (b_number == 0) {
//取到random数组中值为零的跳过循环
continue;
} else {
//将取到的随机数存入arr数组中
arr[o] = a_number;
//将取过的数组的值变为零(做标记以去重复)
random[a_number - 1] = 0;
o++;
}
}
return arr;
}
//随机整数
public static int intRandom(int n, int m){
int number = (int)(n+(Math.random()*(m-n+1)));
return number;
}
//上海到北京的距离
public static double distance() {
double x_1 =31;
double y_1 =121;
double x_2 =40;
double y_2 =116;
double radianx_1 = Math.toRadians(x_1);
double radiany_1 = Math.toRadians(y_1);
double radianx_2 = Math.toRadians(x_2);
double radiany_2 = Math.toRadians(y_2);
final double r = 6371;
double dis =r * Math.acos(Math.sin(radianx_1) * Math.sin(radianx_2) + Math.cos(radianx_1) * Math.cos(radianx_2) * Math.cos(radiany_1 - radiany_2));
return dis;
}
}
class Point {
private double x;
private double y;
public Point(){x=0;y=0;}
public Point(double x,double y){
this.x=x;
this.y=y;
}
public void SetX(double u ){
x = u;
}
public void SetY(double v){
y = v;
}
public double getX(){
return x;
}
public double getY(){
return y;
}
}