这里的几何问题是指计算一系列几何图形中的距离,交点等问题。涉及到的题目见书上题3.22,题3.25,题3.29,题4.2,题4.6,题8.34,题9.12,题10.13,……。在此题目基础上,完成如下编程任务:
1、 通过设计实验的方法,利用随机函数,在一个10*10的正方形中,计算其内切三角形的面积。
注(思路):使用用随机函数实验求面积,计算三角形和正方形面积之比是多少,进不步求出三角形的面积。
正方形里面的三角形,有几种情形:
(1)三角形一条边正好重叠正方形一条边,另外一点在正方形对面一条边的中点;
(2)三角形一条边正好重叠正方形一条边,对面一点可以随意设置而构成的三角形;
(3)三点在正方形不同的三条边上,这三点随机产生。
用实验模拟方法求出三角形面积,实验方法就是统计10000个以上随机点落在三角形里面的数量,然后再试10万以上个随机点的情况,等等。
2、 计算上海到北京的直线距离(参考题4.2)。
按题10.13要求,完成判断两个矩形的相互关系。
瞎写的代码:
public class p2 {
public p2() {
}
public static double getValue() {
double value = Math.random() * 10.0D;
return value;
}
public static double getside(double x1, double y1, double x2, double y2) {
double side = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
return side;
}
public static boolean ifsidecoinside(double[][] points) {
double k1 = (points[1][1] - points[0][1]) / (points[1][0] - points[0][0]);
double k2 = (points[2][1] - points[1][1]) / (points[2][0] - points[1][0]);
double k3 = (points[2][1] - points[0][1]) / (points[2][0] - points[0][0]);
return k1 != k2 && k2 != k3 && k1 != k3;
}
public static double getarea(double[][] points) {
double side1 = getside(points[0][0], points[0][1], points[1][0], points[1][1]);
double side2 = getside(points[0][0], points[0][1], points[2][0], points[2][1]);
double side3 = getside(points[1][0], points[1][1], points[2][0], points[2][1]);
double p = (side1 + side2 + side3) / 2.0D;
double area = Math.sqrt(p * (p - side1) * (p - side2) * (p - side3));
return area;
}
public static void distance() {
double x1 = 31.41D;
double y1 = 121.49D;
double x2 = 40.22D;
double y2 = 116.23D;
double r = 6371.0D;
double radian_x1 = Math.toRadians(x1);
double radian_y1 = Math.toRadians(y1);
double radian_x2 = Math.toRadians(x2);
double radian_y2 = Math.toRadians(y2);
double distance = 6371.0D * Math.acos(Math.sin(radian_x1) * Math.sin(radian_x2) + Math.cos(radian_x1) * Math.cos(radian_x2) * Math.cos(radian_y1 - radian_y2));
System.out.println("上海与北京的直线距离为:" + distance + "km");
}
public static void main(String[] args) {
double[][] points = new double[3][2];
int n = (int)(Math.random() * 5.0D);
switch(n) {
case 0:
points[0][0] = 0.0D;
points[0][1] = 0.0D;
points[1][0] = 0.0D;
points[1][1] = 10.0D;
points[2][0] = 10.0D;
points[2][1] = 5.0D;
break;
case 1:
points[0][0] = 0.0D;
points[0][1] = 0.0D;
points[1][0] = 0.0D;
points[1][1] = 10.0D;
points[2][0] = 10.0D;
points[2][1] = getValue();
break;
case 2:
points[0][0] = 0.0D;
points[0][1] = getValue();
points[1][0] = getValue();
points[1][1] = 0.0D;
points[2][0] = 10.0D;
points[2][1] = getValue();
break;
case 3:
points[0][0] = 0.0D;
points[0][1] = getValue();
points[1][0] = getValue();
points[1][1] = 0.0D;
points[2][0] = getValue();
points[2][1] = 10.0D;
break;
case 4:
points[0][0] = 0.0D;
points[0][1] = getValue();
points[1][0] = getValue();
points[2][1] = 10.0D;
points[2][0] = 10.0D;
points[2][1] = getValue();
break;
case 5:
points[0][0] = getValue();
points[0][1] = getValue();
points[1][0] = getValue();
points[1][1] = 0.0D;
points[2][1] = 10.0D;
points[2][0] = 10.0D;
}
if (ifsidecoinside(points)) {
System.out.println("该三角形的面积是:" + getarea(points));
} else {
System.out.println("三点构不成三角形");
}
distance();
}
}