题解 | 三角形面积

三角形面积

https://www.nowcoder.com/practice/52992a1ac2b842cc84d3fd3813b9566d

import java.util.*;

public class Main {
    static class Point {
        double x, y;
        
        Point(double a, double b) {
            x = a;
            y = b;
        }
    }
    
    static class Triangle {
        Point a, b, c;
        
        Triangle(Point a, Point b, Point c) {
            this.a = a;
            this.b = b;
            this.c = c;
        }
    }
    
    static double getArea(Triangle T) {
        // TODO: 计算三角形T的面积
        double x1 = T.a.x;
        double y1 = T.a.y;
        double x2 = T.b.x;
        double y2 = T.b.y;
        double x3 = T.c.x;
        double y3 = T.c.y;

        double A = y2 - y1;
        double B = x1 - x2;
        double C = x2*y1 - x1*y2;
        double H = Math.abs(A*x3 + B*y3 + C)/Math.sqrt(A*A+B*B);
        double w = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
        double s = H * w / 2.0;
        return s;
    }
    










































































































































































































    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int x, y;
        
        x = scanner.nextInt();
        y = scanner.nextInt();
        Point a = new Point(x, y);
        
        x = scanner.nextInt();
        y = scanner.nextInt();
        Point b = new Point(x, y);
        
        x = scanner.nextInt();
        y = scanner.nextInt();
        Point c = new Point(x, y);
        
        Triangle T = new Triangle(a, b, c);
        System.out.printf("%.2f%n", getArea(T));
        scanner.close();
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务