Check If It Is a Straight Line

2019-10-21 10:35:33

问题描述:

问题求解:

    public boolean checkStraightLine(int[][] coordinates) {
        int n = coordinates.length;
        if (n == 2) return true;
        for (int i = 3; i < n; i++) {
            if (area(coordinates[0], coordinates[1], coordinates[i]) != 0) return false;
        }
        return true;
    }
    
    private int area(int[] p1, int[] p2, int[] p3) {
        return p1[0] * p2[1] + p1[1] * p3[0] + p2[0] * p3[1] - 
            p2[1] * p3[0] - p1[1] * p2[0] - p1[0] * p3[1];
    }

  

posted @ 2019-10-21 10:37  hyserendipity  阅读(280)  评论(0编辑  收藏  举报