巧解三角形或四边形面积

三角形边长分别为:a,b,c。

p=(a+b+c)/2。

海伦公式:三角形面积为:sqrt(p*(p-a)*(p-b)*(p-c))



import java.util.Scanner;

import java.util.*;


class Main {

    static double getArea(double a,double b,double c){

        double p=(a+b+c)/2.0;

        return Math.sqrt(p*(p-a)*(p-b)*(p-c));

    }

    public static void main(String[] args) {


        Scanner input = new Scanner(System.in);

        // code here

        int x1=input.nextInt();

        int y1=input.nextInt();

        int x2=input.nextInt();

        int y2=input.nextInt();

        int x3=input.nextInt();

        int y3=input.nextInt();

        int x4=input.nextInt();

        int y4=input.nextInt();

        double p12=Math.sqrt(Math.pow(x2-x1,2)+Math.pow(y2-y1,2));

        double p13=Math.sqrt(Math.pow(x3-x1,2)+Math.pow(y3-y1,2));

        double p14=Math.sqrt(Math.pow(x4-x1,2)+Math.pow(y4-y1,2));

        double p23=Math.sqrt(Math.pow(x3-x2,2)+Math.pow(y3-y2,2));

        double p24=Math.sqrt(Math.pow(x4-x2,2)+Math.pow(y4-y2,2));

        double p34=Math.sqrt(Math.pow(x4-x3,2)+Math.pow(y4-y3,2));

        double area123=getArea(p12,p13,p23);

        double area124=getArea(p12,p14,p24);

        double area134=getArea(p13,p14,p34);

        double area234=getArea(p23,p24,p34);

        System.out.println(String.format("%.2f",(area123+area124+area134+area234)/2.0));

        input.close();

    }

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容