首先,什么样的是四边形?
1.四个点
2.第一个点与第二个点相连,第二个点与第三个点相连,第三个点与第四个点相连,第四个点与第一个点相连
3.任意三个点不在一条直线上
4.第二个和第四个点在第一、三两点所成直线的两边
python代码如下:#-*-coding:utf8;-*-#qpy:3#qpy:console#point defineclass Point(): def __init__(self, row, col): self.Row = row self.Col = col#check if two points are linkeddef checklinked(p1, p2): if(p1.Row == p2.Row): return True if(p1.Col == p2.Col): return True if(p2.Row-p1.Row == p2.Col-p1.Col): return True return False#check if three points are on a linedef checkline(p1, p2, p3): if((p1.Row == p2.Row) & (p1.Row == p3.Row)): return True if((p1.Col == p2.Col) & (p1.Col == p3.Col)): return True if((p2.Row-p1.Row == p2.Col-p1.Col) & (p3.Row-p1.Row == p3.Col-p1.Col)): return True return False#check four points can be a 4def checkcross(p1, p2, p3,p4): if(p3.Col == p1.Col): if(((p4.Col-p1.Col)*(p2.Col-p1.Col)) <0): return True else: return False k=(p3.Row-p1.Row)/(p3.Col-p1.Col) b=(p3.Col*p1.Row-p1.Col*p3.Row)/(p3.Col-p1.Col) if((k*p2.Col+b-p2.Row)*(k*p4.Col+b-p4.Row) <0): return True else: return False #check four points can be a 4def check4(p1, p2, p3,p4): if(checklinked(p1, p2)): if(checklinked(p2, p3)): if(checklinked(p3, p4)): if(checklinked(p4, p1)): if(checkline(p1, p2, p3)): return False if(checkline(p1, p2, p4)): return False if(checkline(p1, p4, p3)): return False if(checkline(p4, p2, p3)): return False if(checkcross(p1, p2, p3, p4)): return True else: return False return False def calc4(N): count = 0 NN= N*N lst = list() for i in range(0, N): for j in range(0, N): p = Point(i, j) lst.append(p) for n1 in range(0, NN): for n2 in range(1, NN): for n3 in range(1, NN): for n4 in range(1, NN): if((n1+n2+n3+n4) >(NN-1)): continue p1 = lst[n1] p2 = lst[n1+n2] p3 = lst[n1+n2+n3] p4 = lst[n1+n2+n3+n4] if(check4(p1,p2,p3,p4)): count += 1 if(check4(p1,p2,p4,p3)): count += 1 return count print(“result:”)print(calc4(8))