def polygonEnvelope(polygon): # 多边形的顶点序列 partList = polygon.partList # 两个顶点 rt = Point() lb = Point() rt.x = lb.x = partList[0][0].x rt.y = lb.y = partList[0][0].y for i in range(len(partList)): for k in range(len(partList[i])): if partList[i][k].x < lb.x: lb.x = partList[i][k].x if partList[i][k].x > rt.x: rt.x = partList[i][k].x if partList[i][k].y < lb.y: lb.y = partList[i][k].y if partList[i][k].y > rt.y: rt.y = partList[i][k].y envelope = Envelope() envelope.rtPoint = rt envelope.lbPoint = lb return envelope
def getEnvelopeGravity(envelopeList): # rtPoint = Point() lbPoint = Point() rtPoint.x = lbPoint.x = envelopeList[0].rtPoint.x rtPoint.y = lbPoint.y = envelopeList[0].lbPoint.y for envelope in envelopeList: rt = envelope.rtPoint lb = envelope.lbPoint if lb.x < lbPoint.x: lbPoint.x = lb.x if lb.y < lbPoint.y: lbPoint.y = lb.y if rt.x > rtPoint.x: rtPoint.x = rt.x if rt.y > rtPoint.y: rtPoint.y = rt.y envelope = Envelope() envelope.lbPoint = lbPoint envelope.rtPoint = rtPoint gravity = Point() gravity.x = (rtPoint.x + lbPoint.x) / 2 gravity.y = (rtPoint.y + lbPoint.y) / 2 return gravity, envelope pass