How to Check If Four Points can Make a Valid Square (C++ and Jav

  • 时间:2020-09-28 16:28:51
  • 分类:网络文摘
  • 阅读:136 次
four-points-square How to Check If Four Points can Make a Valid Square (C++ and Java)? algorithms c / c++ geometry java math programming languages

four-points-square

Given the coordinates of four points in 2D space, return whether the four points could construct a square. The coordinate (x,y) of a point is represented by an integer array with two integers.

Example:
Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1]
Output: True

Note:

  • All the input integers are in the range [-10000, 10000].
  • A valid square has four equal sides with positive length and four equal angles (90-degree angles).
  • Input points have no order.

Given four points, we may brute force all four permutation with has O(1) complexity i.e. needs to check 4!=24 cases that if any of those make a square. However, the most elegant and straightforward solution is to compute the distances between each pair, and push the distances to a set. At the end, we just have to check if there are two different lengths and also need to exclude the zeros – which may indicate that at least two points are the same (duplicate).

For example, in the following Java, we create a hash set, and store all possible distances between pairs of points. The complexity is O(1) in terms of space and time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
    public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
        Set<Integer> d = new HashSet();
        d.add(dist(p1, p2));
        d.add(dist(p1, p3));
        d.add(dist(p1, p4));
        d.add(dist(p2, p3));
        d.add(dist(p2, p4));
        d.add(dist(p3, p4));
        return d.size() == 2 && (!d.contains(0));
    }
    
    private int dist(int[] p1, int[] p2) {
        return (int)Math.pow(p1[0] - p2[0], 2) + (int)Math.pow(p1[1] - p2[1], 2);
    }
}
class Solution {
    public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
        Set<Integer> d = new HashSet();
        d.add(dist(p1, p2));
        d.add(dist(p1, p3));
        d.add(dist(p1, p4));
        d.add(dist(p2, p3));
        d.add(dist(p2, p4));
        d.add(dist(p3, p4));
        return d.size() == 2 && (!d.contains(0));
    }
    
    private int dist(int[] p1, int[] p2) {
        return (int)Math.pow(p1[0] - p2[0], 2) + (int)Math.pow(p1[1] - p2[1], 2);
    }
}

The equivalent C++ implementation is below where we use unordered_set to store the unique distance values.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public:
    int dist(vector<int> &p1, vector<int> &p2) {
        return pow(p1[0] - p2[0], 2) + pow(p1[1] - p2[1], 2);
    }
       
    bool validSquare(vector<int>& p1, vector<int>& p2, vector<int>& p3, vector<int>& p4) {
        unordered_set<int> d;
        d.insert(dist(p1, p2));
        d.insert(dist(p1, p3));
        d.insert(dist(p1, p4));
        d.insert(dist(p2, p3));
        d.insert(dist(p2, p4));
        d.insert(dist(p3, p4));
        return d.size() == 2 && d.count(0) == 0;
    }
};
class Solution {
public:
    int dist(vector<int> &p1, vector<int> &p2) {
        return pow(p1[0] - p2[0], 2) + pow(p1[1] - p2[1], 2);
    }
       
    bool validSquare(vector<int>& p1, vector<int>& p2, vector<int>& p3, vector<int>& p4) {
        unordered_set<int> d;
        d.insert(dist(p1, p2));
        d.insert(dist(p1, p3));
        d.insert(dist(p1, p4));
        d.insert(dist(p2, p3));
        d.insert(dist(p2, p4));
        d.insert(dist(p3, p4));
        return d.size() == 2 && d.count(0) == 0;
    }
};

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
味道鲜美营养丰富的黑木耳最佳吃法  怎样食用萝卜可以治咳嗽使症状缓解  柚子营养价值高多吃对健康大有益处  美食“扬州炒饭”新标准公布了制作方法  推荐5大养胃食物帮助呵护你的“胃”  红薯营养丰富但空腹吃红薯需要谨慎  香蕉不能和哪些食物一起食用呢?  男性多吃香蕉有助于性功能疾病的康复  权威发布:常见的致癌食物你吃过几种  “最大份扬州炒饭”喂猪背后的浮躁心态 
评论列表
添加评论