Easy understanding Python solution


  • 0
    K

    To construct a square, we only need to make sure the distance between each point and the mid point is the same and not 0, and the angle of two adjacent point is 90 degree which means their outer product is 0.

     def dist(p1,p2):
                return float(p1[0]-p2[0])**2 + float(p1[1]-p2[1])**2
            def angle(p1,mid,p2):
                return ((p1[0]-mid[0])*(p2[0]-mid[0]) + (p1[1]-mid[1])*(p2[1]-mid[1])) == 0
            mid = [(p1[0]+p2[0]+p3[0]+p4[0])/float(4),(p1[1]+p2[1]+p3[1]+p4[1])/float(4)]
            return dist(p1,mid) == dist(p2,mid) == dist(p3,mid) == dist(p4,mid) and (angle(p1,mid,p2) or angle(p1,mid,p3)) and dist(p1,mid) != 0
    

    Please let me know if you have better solution, thanks!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.