实验二:几何问题及编程

这里的几何问题是指计算一系列几何图形中的距离,交点等问题。涉及到的题目见书上题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();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值