def newLineInter(line1,line2):
     vec1=GeoFunc.lineToVec(line1)
     vec2=GeoFunc.lineToVec(line2)
     vec12_product=GeoFunc.crossProduct(vec1,vec2)
     Line1=LineString(line1)
     Line2=LineString(line2)
     inter={
         "length":0,
         "geom_type":None
     }
     # 只有平行才会有直线重叠
     if vec12_product==0:
         # copy避免影响原值
         new_line1=GeoFunc.copyPoly(line1)
         new_line2=GeoFunc.copyPoly(line2)
         if vec1[0]*vec2[0]<0 or vec1[1]*vec2[1]<0:
             new_line2=GeoFunc.reverseLine(new_line2)
         # 如果存在顶点相等,则选择其中一个
         if GeoFunc.almostEqual(new_line1[0],new_line2[0]) or GeoFunc.almostEqual(new_line1[1],new_line2[1]):
             inter["length"]=min(Line1.length,Line2.length)
             inter["geom_type"]='LineString'
             return inter
         # 排除只有顶点相交情况
         if GeoFunc.almostEqual(new_line1[0],new_line2[1]):
             inter["length"]=new_line2[1]
             inter["geom_type"]='Point'
             return inter
         if GeoFunc.almostEqual(new_line1[1],new_line2[0]):
             inter["length"]=new_line1[1]
             inter["geom_type"]='Point'
             return inter
         # 否则判断是否包含
         line1_contain_line2_pt0=GeoFunc.almostContain(new_line1,new_line2[0])
         line1_contain_line2_pt1=GeoFunc.almostContain(new_line1,new_line2[1])
         line2_contain_line1_pt0=GeoFunc.almostContain(new_line2,new_line1[0])
         line2_contain_line1_pt1=GeoFunc.almostContain(new_line2,new_line1[1])
         # Line1直接包含Line2
         if line1_contain_line2_pt0 and line1_contain_line2_pt1:
             inter["length"]=Line1.length
             inter["geom_type"]='LineString'
             return inter
         # Line2直接包含Line1
         if line1_contain_line2_pt0 and line1_contain_line2_pt1:
             inter["length"]=Line2.length
             inter["geom_type"]='LineString'
             return inter
         # 相互包含交点
         if line1_contain_line2_pt0 and line2_contain_line1_pt1:
             inter["length"]=LineString([line2[0],line1[1]]).length
             inter["geom_type"]='LineString'
             return inter
         if line1_contain_line2_pt1 and line2_contain_line1_pt0:
             inter["length"]=LineString([line2[1],line1[0]]).length
             inter["geom_type"]='LineString'
             return inter                
     return inter
    def intersection(line1,line2):
        # 如果可以直接计算出交点
        Line1=LineString(line1)
        Line2=LineString(line2)
        inter=Line1.intersection(Line2)
        if inter.is_empty==False:
            mapping_inter=mapping(inter)
            if mapping_inter["type"]=="LineString":
                inter_coor=mapping_inter["coordinates"][0]
            else:
                inter_coor=mapping_inter["coordinates"]
            return inter_coor

        # 对照所有顶点是否相同
        res=[]
        for pt1 in line1:
            for pt2 in line2:
                if GeoFunc.almostEqual(pt1,pt2)==True:
                    # print("pt1,pt2:",pt1,pt2)
                    res=pt1
        if res!=[]:
            return res

        # 计算是否存在almostContain
        for pt in line1:
            if GeoFunc.almostContain(line2,pt)==True:
                return pt
        for pt in line2:
            if GeoFunc.almostContain(line1,pt)==True:
                return pt
        return []
 def detectTouching(self):
     touch_edges=[]
     stationary_edges,sliding_edges=self.getAllEdges()
     for edge1 in stationary_edges:
         for edge2 in sliding_edges:
             inter=GeoFunc.intersection(edge1,edge2)
             if inter!=[]:
                 pt=[inter[0],inter[1]] # 交叉点
                 edge1_bound=(GeoFunc.almostEqual(edge1[0],pt) or GeoFunc.almostEqual(edge1[1],pt)) # 是否为边界
                 edge2_bound=(GeoFunc.almostEqual(edge2[0],pt) or GeoFunc.almostEqual(edge2[1],pt)) # 是否为边界
                 stationary_start=GeoFunc.almostEqual(edge1[0],pt) # 是否开始
                 orbiting_start=GeoFunc.almostEqual(edge2[0],pt) # 是否开始
                 touch_edges.append({
                     "edge1":edge1,
                     "edge2":edge2,
                     "vector1":self.edgeToVector(edge1),
                     "vector2":self.edgeToVector(edge2),
                     "edge1_bound":edge1_bound,
                     "edge2_bound":edge2_bound,
                     "stationary_start":stationary_start,
                     "orbiting_start":orbiting_start,
                     "pt":[inter[0],inter[1]],
                     "type":0
                 })
     return touch_edges 
 def detectExisting(self,vectors,judge_vector):
     for vector in vectors:
         if GeoFunc.almostEqual(vector,judge_vector):
             return True
     return False